Fix ephemeral port leak: pass context to ClientSession.DialStream#373
Merged
0pcom merged 6 commits intoskycoin:developfrom Apr 7, 2026
Merged
Fix ephemeral port leak: pass context to ClientSession.DialStream#3730pcom merged 6 commits intoskycoin:developfrom
0pcom merged 6 commits intoskycoin:developfrom
Conversation
ClientSession.DialStream didn't accept a context, so when the caller's deadline expired, the blocked readResponse kept the ephemeral port reserved until HandshakeTimeout fired (20-30s per server). With 6 servers tried sequentially, a single failed dial could hold ports for minutes. Now DialStream accepts context and spawns a goroutine that closes the stream when the context is cancelled, immediately interrupting any blocked read/write and freeing the ephemeral port. Also: Client.DialStream now passes ctx to all ClientSession.DialStream calls (phases 0-3) and checks ctx.Err() between phases.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
ClientSession.DialStreamnow acceptscontext.ContextClient.DialStreampasses its context to allClientSession.DialStreamcallsRoot Cause
ClientSession.DialStreamdidn't accept a context. When the caller's deadline expired (e.g., 30s dial timeout in the setup-node),readResponsekept blocking on the yamux stream read untilHandshakeTimeoutfired. The ephemeral port reserved bywriteRequeststayed allocated the entire time.With 6 DMSG servers tried sequentially per dial attempt, and many concurrent route setup requests, the ~16K ephemeral port space exhausted within minutes.
Fix
A goroutine watches the context and calls
dStr.Close()when cancelled. This interrupts the blockedreadResponse, triggers the defer that frees the port, and returns immediately. The port is freed in milliseconds instead of waiting up to HandshakeTimeout.Test plan