Fix goroutine leaks in proxy testing code - #227
Merged
Conversation
Run the ssproxy and main package test suites under go.uber.org/goleak via TestMain, guarding the actual proxy-testing code paths (the SOCKS listener and relay goroutines) against goroutine leaks. goleak surfaced two real leaks that are also fixed here: - IsIPInfoOffline used the default HTTP transport, leaking a persistent connection's readLoop goroutine into the pool. It now uses a dedicated transport with keep-alives disabled and closes idle connections. - ListenForOneConnection dialed the upstream server with an unbounded net.Dial, so the goroutine outlived a cancelled/timed-out request. It now takes a context and dials with DialContext; GetShadowsocksProxyDetails cancels that context on return. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPGSpvYdaeVxKXXkoJERsH
Name the files after what they contain (goleak setup) rather than the generic main_test.go. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01VPGSpvYdaeVxKXXkoJERsH
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
This PR fixes goroutine leaks in the shadowsocks proxy testing code by properly managing the lifecycle of goroutines started during proxy operations. The changes ensure that goroutines are cancelled when requests timeout or are cancelled, and HTTP connections are properly cleaned up.
Key Changes
go.uber.org/goleaktest harness in both root andssproxypackages to detect goroutine leaks during testingListenForOneConnectionto accept acontext.Contextparameter that bounds the upstream server dial operation, ensuring the goroutine terminates when the context is cancelledIsIPInfoOfflineto explicitly disable HTTP keep-alives and defer closing idle connections on the transport, preventing connection pooling from holding resourcesListenForOneConnectionto require a context as the first parameter and updated the call site inGetShadowsocksProxyDetailsto pass a cancellable contextImplementation Details
ListenForOneConnectionis created withcontext.WithCancel()and deferred cancelled inGetShadowsocksProxyDetails, ensuring cleanup after the proxy test completesnet.Dialer.DialContext()instead ofnet.Dial()to respect the provided context's cancellationhttps://claude.ai/code/session_01VPGSpvYdaeVxKXXkoJERsH