Skip to content

connect: bound post-connect blocking connects by their own start - #23528

Closed
zipadoodlez wants to merge 2 commits into
curl:masterfrom
zipadoodlez:fix/connect-timeout-secondary
Closed

zipadoodlez wants to merge 2 commits into
curl:masterfrom
zipadoodlez:fix/connect-timeout-secondary

Conversation

@zipadoodlez

@zipadoodlez zipadoodlez commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

Supersedes #23506.

This PR has two commits: the fix and the test.

Problem

curl drops the connect timeout when a transfer reaches MSTATE_DO. A
blocking connect can then wait forever. The TLS handshake of an FTPS
data connection is one example.

The original patch in #23506 stops the hang. But it starts the timer
when the blocking wait starts. CURLOPT_CONNECTTIMEOUT.md says the
connection phase includes the name resolve and all protocol handshakes
until the connection is established. For a passive FTPS data connection,
the TCP connect and the wait for the 150 response happen before the
blocking wait. The original patch does not count them.

Change

  • Record when Curl_conn_connect() first sees each socket.
  • Use that time for the blocking connect timeout after MSTATE_DO.
  • Clear the time when the socket's filter chain is discarded. A new
    data connection then gets a new timeout.
  • Move the connect timeout default and the expired timeout rule into
    one helper. timeleft_now_ms() uses the same helper.

Test

STALLDATA is a new mode in the FTP test server. It accepts the data
connection and sends no data. Test 1792 waits one second before the 150
response. Then the data TLS handshake stalls.

The test uses --max-time 5 as a safety bound. A postcheck reads
time_total. It fails when the value is 2.8 seconds or more. This
rejects a total-timeout exit and a timer that starts after the 150
response.

The test uses --max-time 5 as a safety bound. A postcheck reads
time_total and time_appconnect, subtracts the control connection
setup, and fails when the remaining time is 2.8 seconds or more. This
rejects a total-timeout exit and a timer that starts after the 150
response, without counting a slow control connection.

Results

Binary time_total result
this branch 2.017s pass
#23506 3.018s fail
master 5.000s fail

FTP/FTPS tests 400, 401, 402, 405, 1112, 1790, and 1792 pass.

Credit

The original patch is by @1rhino2 in #23506.

Open question

Should the timer start at the first Curl_conn_connect() call for the
socket? That call covers the DNS, TCP, and handshake work in the filter
chain. It does not cover FTP setup before that call. A protocol-level
timestamp is needed for an earlier start.

Before this change, curl had no connect timeout after MSTATE_DO. A
blocking connect could then wait forever. The FTPS data connection TLS
handshake is one example.

Curl_conn_connect() now records the first call for each socket. It uses
that time for the blocking connect timeout after MSTATE_DO. When the
filter chain is discarded, curl clears the time. A new data connection
then gets a new timeout.

The change also moves the connect timeout default and the expired
timeout rule into one helper. timeleft_now_ms() uses the same helper.

Co-authored-by: Rhino <195818135+1rhino2@users.noreply.github.com>
@testclutch

Copy link
Copy Markdown

Analysis of PR #23528 at 9ce9d890:

Test ../../tests/http/test_02_download.py::TestDownload::test_02_06_download_many_parallel[h3] failed, which has NOT been flaky recently, so there could be a real issue in this PR. Note that this test has failed in 2 different CI jobs (the link just goes to one of them).

Generated by Testclutch

@1rhino2

1rhino2 commented Oct 21, 2026 •

Copy link
Copy Markdown
Contributor

ran this branch against the stall fixture i used for the report (implicit tls, 150 delayed 1.5s, data socket accepted and never answered) and the lib side does what the description says. the trace has the first connect call on the data socket at .341 and the connect timeout at .343 two seconds later, 2.06s total. im fine with this landing instead of #23506.

the test is the problem tho. 1792 fails here 5 out of 5 runs with time_total between 3.44 and 3.53, and the fix is not what eats that time. the control connection through stunnel takes 1.2 to 1.3s to come up on this box (time_appconnect 1.25 and 1.34 in two runs) before EPSV is even sent, and the traced run still shows the timeout firing 2.00s after the first data connect call. the postcheck counts that setup time inside time_total, so a slow runner fails a working fix.

something like --write-out '%{time_total} %{time_appconnect}' and subtracting in test1792.pl gives 2.19s here and still 3+ on #23506 and on master. time_pretransfer is no good for this btw, it only gets stamped on DID or COMPLETED so on a timeout it equals time_total.

the CM quiche red is a 502 from nghttpx on stream 41 of the parallel h3 test with curl exiting 0, not this change.

The test server has a new STALLDATA mode. It accepts the data
connection and sends no data. The test waits one second before the 150
response. Then the data TLS handshake stalls.

The test uses --max-time 5 as a safety bound. A postcheck reads
time_total and time_appconnect. It subtracts the control connection's
TLS setup and fails when the remaining time is 2.8 seconds or more.
The fix stops at the 2 second connect timeout. The original patch
starts the timer after the 150 response. It stops at 3 seconds. A
total timeout stops at 5 seconds.
@zipadoodlez
zipadoodlez force-pushed the fix/connect-timeout-secondary branch from 9ce9d89 to b9db11d Compare October 22, 2026 06:56
@zipadoodlez

Copy link
Copy Markdown
Contributor Author

good catch on the test. I implemented your recommendation. agreed on the quiche red not being related to this pr.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The timeout calculation and timestamp lifecycle are consistent, with targeted regression coverage and no unresolved blocking findings.

No open issues 🎉

What changed in this PR

This PR bounds blocking connections after the normal connect phase using each socket’s original connection start time. This prevents stalled FTPS data handshakes from waiting indefinitely and includes time spent waiting for the server’s 150 response.

Changes:

  • Track per-socket connection start times and reset them when filter chains are discarded.
  • Share connect-timeout calculation between normal and post-connect timeout checks.
  • Add an FTPS regression test with a silent data connection and elapsed-time validation.
File Description
tests/​test1792.pl Validate elapsed time after subtracting control-connection setup.
tests/​Makefile.am Include the new postcheck script in distribution files.
tests/​ftpserver.pl Add STALLDATA mode to hold data connections silent.
tests/​data/​test1792 Test that the data connect timeout includes the 150-response wait.
tests/​data/​Makefile.am Register test 1792.
lib/​urldata.h Store per-socket connection start timestamps.
lib/​connect.c Apply start-based timeout bounds to blocking connections.
lib/​cfilters.c Reset timestamp validity when discarding socket filter chains.

Review effort: Balanced

@bagder bagder closed this in ae993d2 Oct 22, 2026
@bagder

bagder commented Oct 22, 2026

Copy link
Copy Markdown
Member

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Development

Successfully merging this pull request may close these issues.

5 participants