Fix: Pass timeout to SOCKS5 handshake to prevent hanging #1055
+160
−66
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 a critical issue where
SOCKSProxyignores the configuredconnecttimeout during the SOCKS5 handshake.Previously, the timeout was used to establish the initial TCP connection, but it was not passed to the
_init_socks5_connectionfunction. As a result, if a proxy server accepted the TCP connection but failed to send data (e.g., a "blackhole" server), the client would hang indefinitely (or until the OS socket timeout) regardless of the user's configuredtimeout.The Fix
_init_socks5_connectionin both_sync/socks_proxy.pyand_async/socks_proxy.pyto accept atimeoutargument.timeoutexplicitly to allstream.read()andstream.write()calls during the authentication and connection negotiation phases.Socks5Connection.handle_request(and its async counterpart) to extract the connect timeout fromrequest.extensionsand pass it to the initialization function.Reproduction
I verified this fix using a local reproduction script that simulates a hanging SOCKS5 proxy.
TimeoutExceptionafter 2s.Checklist