Fix server goroutine leak: idle timeout on bridged streams#372
Merged
0pcom merged 1 commit intoskycoin:developfrom Apr 7, 2026
Merged
Fix server goroutine leak: idle timeout on bridged streams#3720pcom merged 1 commit intoskycoin:developfrom
0pcom merged 1 commit intoskycoin:developfrom
Conversation
Bridged streams (bidirectional copy between two clients through the server) blocked forever on io.Copy Read when one side disconnected without cleanly closing the connection. This caused goroutines to accumulate — observed as 55K+ stuck goroutines in production. Added idleTimeoutConn wrapper that resets a per-operation deadline on each Read/Write. If no data flows for 5 minutes, the deadline fires, io.Copy returns an error, CopyReadWriteCloser closes both streams, and the goroutine exits. The timeout resets on each successful read/write, so active streams are not affected. Only truly idle/dead streams are cleaned up.
0pcom
added a commit
to 0pcom/dmsg
that referenced
this pull request
Apr 8, 2026
forwardRequest opens a stream to the destination and reads the response. If the destination accepts but never responds, readObject blocks forever — observed as 2.4K+ stuck goroutines per server, growing rapidly after restart. The idle timeout fix (PR skycoin#372) only covered the bridge phase (CopyReadWriteCloser). This adds HandshakeTimeout to the forwardRequest handshake read, matching the client-side behavior.
0pcom
added a commit
that referenced
this pull request
Apr 8, 2026
…374) forwardRequest opens a stream to the destination and reads the response. If the destination accepts but never responds, readObject blocks forever — observed as 2.4K+ stuck goroutines per server, growing rapidly after restart. The idle timeout fix (PR #372) only covered the bridge phase (CopyReadWriteCloser). This adds HandshakeTimeout to the forwardRequest handshake read, matching the client-side behavior.
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
ServerSession.bridgeStreamRoot Cause
CopyReadWriteCloserblocks onio.Copy(Read)when one client disconnects without cleanly closing (network failure without TCP RST). The other side's read blocks forever, leaking the goroutine, yamux stream, and semaphore slot.Observed in production: 55,697 goroutines stuck in
ServerSession.forwardRequest→readObject→yamux.Stream.Readon a single dmsg-server.Fix
idleTimeoutConnwrapper resets a deadline on eachRead/Write. If no data flows for 5 minutes (both directions idle), the deadline fires,io.Copyreturns,CopyReadWriteClosercloses both streams, and the goroutine exits.Active streams are not affected — the timeout resets on every data transfer.
Test plan