MtProtoKit: fix pre-auth handshake reads could hang forever#181
Open
mvsrcdst wants to merge 1 commit into
Open
MtProtoKit: fix pre-auth handshake reads could hang forever#181mvsrcdst wants to merge 1 commit into
mvsrcdst wants to merge 1 commit into
Conversation
3 tasks
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.
Problem
If a proxy accepts the TCP connection but never responds during the handshake (proxy hangs, doesn't drop the connection, just never answers), the app hangs indefinitely with no error, no timeout, no way to recover except killing the app.
Root cause:
-[MTTcpConnection socket:shouldTimeoutReadWithTag:elapsed:bytesDone:]unconditionally returned-1.0, and everyreadDataToLength:withTimeout: call in the file passed-1as the timeout -GCDAsyncSocketnever even creates a timer for timeout < 0, so the delegate method is never invoked at all. That's correct for ordinary post-handshake MTProto traffic - upstream deliberately moved timeout enforcement up toMTRequestMessageServicerequest-level watchdog so a raw socket read timeout doesn't kill a slow-but-alive transfer. But that watchdog only arms once there's a queued MTRequest, i.e. only after the handshake has already completed. So it doesn't cover the SOCKS5 pre-auth handshake reads (MTTcpSocksReceiveHelloResponse..., MTTcpSocksLogin, MTTcpSocksRequest, MTTcpSocksReceiveAuthResponse, MTTcpSocksReceiveBindAddr...), which run before anyMTRequestexists.Fix
MTRequestMessageServicewatchdog handles that.Impact
A hang proxy now fails the handshake after 15s instead of hanging forever - app can report the error and retry/fall back instead of appearing frozen.
Note
Part of tracking issue: #182
Testing