Skip to content

Dispose socketless tunnel streams on connection shutdown - #3361

Merged
mgravell merged 1 commit into
mainfrom
fix/dispose-socketless-tunnel-stream
Nov 17, 2026
Merged

mgravell merged 1 commit into
mainfrom
fix/dispose-socketless-tunnel-stream

Conversation

@mgravell

Copy link
Copy Markdown
Member

Problem

PhysicalConnection.Shutdown() disposes the socket and the (experimental, SER009) transport, but never _ioStream. For a socket-backed stream that is harmless — killing the socket tears the whole stack down — but a socketless tunnel (GetSocketConnectEndpointAsync → null, with BeforeAuthenticateAsync supplying the stream) has no socket: the stream is the connection's only handle, and it leaked on shutdown, leaving the tunnel's underlying transport alive indefinitely.

Observed in practice while evaluating an external native transport behind a tunnel: its engine's DisposeAsync waits for every published connection to be released, and the leaked stream deadlocked it forever.

Fix

Shutdown() now disposes _ioStream (after the socket/transport teardown). The field deliberately stays set after disposal, mirroring the _transportAdopted pattern: RecordConnectionFailed reads it to attribute the failure, and nulling it would change that attribution.

Test

TunnelStreamDisposeTests.SocketlessTunnelStreamIsDisposedOnShutdown wraps the in-process server's own (socketless) tunnel so every stream it hands out is disposal-tracked, connects, disposes the multiplexer, and asserts each stream was disposed. Red without the fix, green with it.

PhysicalConnection.Shutdown() disposed the socket and the (experimental)
transport, but never _ioStream. For a socket-backed stream that is
harmless - killing the socket tears the whole stack down - but a
socketless tunnel (GetSocketConnectEndpointAsync -> null, with
BeforeAuthenticateAsync supplying the stream) has no socket: the stream
is the connection's only handle, and it leaked on shutdown, leaving the
tunnel's underlying transport alive indefinitely.

Observed in practice against an external transport whose engine
disposal waits for every published connection to be released: the
leaked stream deadlocked the engine's DisposeAsync forever.

The field deliberately stays set after disposal (mirroring
_transportAdopted): RecordConnectionFailed reads it to attribute the
failure.
@mgravell
mgravell force-pushed the fix/dispose-socketless-tunnel-stream branch from 379e425 to d9f4661 Compare November 17, 2026 15:33
@mgravell
mgravell marked this pull request as ready for review November 17, 2026 15:35
@mgravell
mgravell merged commit f797d55 into main Nov 17, 2026
4 checks passed
@mgravell
mgravell deleted the fix/dispose-socketless-tunnel-stream branch November 17, 2026 15:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant