Bound the TLS handshake with connectTimeoutMs by yspbwx2010 · Pull Request #25 · mcpplibs/tinyhttps
Repository navigation
Bound the TLS handshake with connectTimeoutMs - #25
Merged
Sunrisepeak merged 4 commits into mcpplibs:master from yspbwx2010:fix/tls-handshake-timeout Oct 9, 2026
Merged
Bound the TLS handshake with connectTimeoutMs #25 Sunrisepeak merged 4 commits into mcpplibs:master from yspbwx2010:fix/tls-handshake-timeout
Conversation
yspbwx2010 commented Oct 9, 2026 · edited by Sunrisepeak Loading
Copy link Copy Markdown
Contributor
The TLS handshake has no time limit. Once the TCP connection is up the socket is blocking, and bio_recv calls recv without waiting first unless a stop token is attached, so a server, or an https:// proxy, that accepts the connection and never sends its ServerHello holds send, send_stream and download_to_file for good when the caller passed no token. Passing a token (#23 ) lets the caller end the call, but nothing ends it by itself.
This puts the handshake under connectTimeoutMs. It gives up once that many milliseconds have passed since the handshake began, and statusText is TLS handshake timed out. It covers the handshake with the target, with an https:// proxy, and with the target inside an https:// proxy's tunnel. The limit is on the handshake as a whole, so a peer that sends a byte at a time does not hold it open.
I used connectTimeoutMs and not a new setting because the name says it is for getting a connection up, the proxy's replies already wait on it, and a caller who set it low wants a connection to a dead host to fail quickly whether the host drops the SYN or swallows the ClientHello. The README now says what it covers.
Behaviour change
A handshake slower than connectTimeoutMs (10 s by default) now fails where it used to complete. Each step of setting up a connection (the TCP connect, a proxy's reply, a handshake) has connectTimeoutMs to itself, so the whole can take several times that. A caller who set it below the time a handshake takes to a distant server will now see those handshakes fail.
API
bool TlsSocket::connect_over(Socket&&, const char* host, bool verifySsl, int handshakeTimeoutMs = -1); bool TlsSocket::connect_over(std::unique_ptr<TlsSocket>, const char* host, bool verifySsl, int handshakeTimeoutMs = -1);
A negative handshakeTimeoutMs waits without limit, which is what the two overloads did. TlsSocket::connect already took a timeout, for the TCP connect, and now passes it on to the handshake. Socket gains set_deadline, deadline_hit and wait_before_recv. A call to connect_over by name compiles unchanged; a pointer to it needs the new parameter in its type, as the pointers to send did in 0.3.4. Nothing else in the public interface changes.
How it works
Socket holds an optional deadline, set by setup_tls just before mbedtls_ssl_handshake and cleared right after it. bio_recv already waited ahead of recv when a token is attached; that wait is now Socket::wait_before_recv, which waits for the deadline or for the token, or both, and is the same wait_readable as before. When that wait ends because of the deadline, the socket records it, and setup_tls reads the record instead of looking at the clock again, so a poll that returns a little early cannot turn the timeout into the generic failure. With neither a token nor a deadline it returns at once and bio_recv goes straight to recv, so a connection that is up pays one check of an empty std::optional.
Like the token, the deadline lives on the socket at the bottom, which inside an https:// proxy's tunnel is the one beneath the proxy's session. A handshake that fails because of the deadline is reported as the timeout instead of the generic TLS handshake failed: NET - Reading information from the socket failed. If the token was stopped, the call still ends as cancelled.
Signals during the handshake (maintainer commit 3013b98 )
With the deadline set, the handshake polls before every recv where it used to block in recv, and poll ends with EINTR on a signal even when the handler was installed with SA_RESTART. The first version took any wait that ended without a readable socket for the deadline, so such a signal ended the handshake at once as TLS handshake timed out. wait_before_recv now waits again for the time that is left, and the deadline is hit only when the clock has passed it. ASignalDuringTheHandshakeIsNotATimeout signals the handshaking thread every 100 ms under a 1000 ms timeout; before the fix it ended after 100 ms. The move operations carry deadline_hit_ with deadline_.
CI (maintainer commit bf99dbd )
- The runner image win22/20261004.326 brings Git for Windows 2.56, whose bundle is under
ucrt64/and no longermingw64/. The openkal job on Windows asks git forhttp.sslcainfoand searches the install after that, instead of naming the path. test_handshake_timeoutjoins the Windows job's hermetic tests, so the deadline is run throughWSAPoll.
Not covered
- A handshake write that blocks.
bio_sendcalls a blockingsend; the ClientHello is a few hundred bytes into an empty buffer, so I do not expect this to bite in the handshake, and I did not try to provoke it. - The same blocking
sendwhen a request is written. The README saysreadTimeoutMsbounds "the total wait on a blocked write", but on a blocking socketmbedtls_ssl_writedoes not returnWANT_WRITE, so the wait inwrite_allis never reached. I read this from the code and did not measure it. It deserves its own change (a non-blocking send) rather than a line in this one. - Name resolution, as in 0.3.4 -- cancel a request in flight with a std::stop_token #23 . Above openkal I did not run the new test; the wait is the
pollthe token already uses there.
Tests
tests/test_handshake_timeout.cpp, eight cases, with a stop token only in the one that checks the two together. The timeout is 300 ms and the read timeout 30 s, except in the two cases that complete a real handshake, which use 1000 ms so that a slow machine does not fail them. The peers hold on for 5 s and then drop the connection, so a handshake without a deadline fails the case, by its message and by the time it took, and does not hang the suite.
sendandsend_streamagainst a listener that reads the ClientHello and says nothing: status 0,TLS handshake timed out, within 3 s (it takes about 300 ms).- The same with a stop token that is never stopped, which takes the waits in slices.
- An https:// proxy that never completes its handshake: the message names the proxy and the timeout.
- The target's handshake inside an https:// proxy's tunnel, and inside an http:// proxy's, after the proxy has answered the CONNECT and gone quiet.
- A peer that sends a record header and then one byte every 100 ms: the handshake still ends after the 300 ms, so the limit is on the handshake and not on each read.
- After the handshake the deadline is gone: a read on a connection that was opened with a 1000 ms timeout waits 1600 ms for a byte and gets it.
Each check was removed in turn in a copy of the tree and the file run again. With the wait not using the deadline (the token wait kept), seven of the eight fail (all but the last). With the deadline restarting for every read, only the one-byte case fails. With the deadline left set after the handshake, only the last fails. With connect not passing its timeout on, the three direct cases, the https proxy case and the one-byte case fail. With open_connection not passing it to connect_over, the two tunnel cases fail. With the deadline set on the proxy's own socket instead of the one beneath it, the https tunnel case fails. With the timeout reported as a generic handshake failure, seven fail on the message.
test_framing, test_pool, test_proxy, test_tls_verify and test_ca_store pass unchanged, and so does test_cancel apart from its three-route reuse cases, which my stand-in for the gtest macros cannot compile (compat.gtest was not available offline here). test_download and test_resolver need the network and were not run. Linux, glibc, LLVM 22 with libc++; a run in CI is still worth waiting for. Not run on macOS, Windows or above openkal.
Bound the TLS handshake with connectTimeoutMs
cfdab98
After the TCP connect the socket is blocking and bio_recv called recv without waiting unless a stop token was attached, so a server or an https:// proxy that accepted the connection and never sent its ServerHello held send(), send_stream() and download_to_file() for good. setup_tls now sets a deadline on the socket at the bottom for the duration of the handshake. bio_recv waits against it (and against the token, if any) through Socket::wait_before_recv, and a handshake that runs out of time fails with "TLS handshake timed out". The deadline covers the handshake with the target, with an https:// proxy and with the target inside an https:// proxy's tunnel. connect_over takes the timeout as a trailing argument; -1, the default, waits without limit.yspbwx2010 mentioned this pull request Oct 9, 2026 Add
extraCaFile: trust extra CA certificates on top of the default store
#24
Merged
yspbwx2010 commented Oct 9, 2026
Copy link Copy Markdown
Contributor
Author
|
The Windows-on-Windows red here is the same runner change I described in #24 : the workflow copies |
fix(socket): a signal during the handshake is not the deadline
3013b98
wait_before_recv took any wait that ended without a readable socket for the deadline. Without a stop token that wait is one poll, and a signal ends poll with EINTR even when its handler was installed with SA_RESTART, which restarts the blocking recv the wait stands in front of. A host with such a handler (a profiler's SIGPROF, a timer) saw handshakes fail as "TLS handshake timed out" long before the time was up; on master the recv restarted and the handshake went on. A wait that ends early is now waited again for the time that is left, and the deadline is hit only when the clock has passed it. The new case signals the handshaking thread every 100 ms under a 1000 ms timeout; before this change it ended after 100 ms. The move operations carry deadline_hit_ with deadline_.
ci: find Git for Windows's CA bundle, and run test_handshake_timeout ...
bf99dbd
...on Windows The runner image win22/20261004.326 (Git for Windows 2.56) has no mingw64/etc/ssl/certs/ca-bundle.crt, so the openkal job on Windows failed at the cp before the program ran, on every branch. Git is asked for its http.sslcainfo, and the install searched for a ca-bundle.crt after that. The handshake deadline waits through poll_fd, which is WSAPoll under Windows Sockets; the hermetic list now runs its test there.
CHANGELOG: TlsSocket::connect bounds the handshake with the timeout i...
97a0657
...t already tookSunrisepeak merged commit
9d4315e
into
mcpplibs : master
Oct 9, 2026
10 checks passed
Sunrisepeak
added a commit
to yspbwx2010/tinyhttps
that referenced
this pull request
Oct 9, 2026
Merge master (handshake deadline, mcpplibs#25) into the extra CA branch
17afbc5
# Conflicts: # .github/workflows/ci.yml # CHANGELOG.mdSunrisepeak added a commit that referenced this pull request Oct 9, 2026
0.3.5 --- the TLS handshake has a time limit, and extra CA certificat...
6153cdd
...es can be trusted The handshake is bounded by connectTimeoutMs (#25), and HttpClientConfig::extraCaFile adds a PEM file of roots to the default store (#24). A patch release: "^0.3.1" resolves to it. The plan, with the review of both PRs and the CI failure that Git for Windows 2.56 caused, is in .agents/docs/exec-plan-0.3.5.md.Sunrisepeak mentioned this pull request Oct 9, 2026 tinyhttps 0.3.5 mcpplibs/mcpp-index#517 Merged Sign up for free to join this conversation on GitHub . Already have an account? Sign in to comment