fix(http): limit connection setup and enable TCP keepalive for HTTP clients #27247
All Vouch review confidence checks passed
Residual risk 0.20 = Change Risk (0.50) × (1 − Review Confidence (0.60)).
Review depth and reviewer attention state are adequate.
Model 1 · Change Risk (XGBoost)
Score: 0.50 · 625 lines diff across 7 files
Model 2 · Review Depth (DistilBERT)
Depth Score: 0.17 · 1 substantive comment(s)
Model 3 · Review Timing & Attention
Session adequacy: 100% · Reviewer familiarity: 35%
Bedrock Claude 3 · Risk Narration
(disconnected)
Groq Risk Narration
AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
Model 1 · Change Risk Attribution
SHAP Weights
Diff Size
diff_size
0.385
Shallow Review Depth
shallow_review_depth
0.289
Real PR Conversation Timeline
1 comments
"Review active on #27247"
general
weight: 0.35
pront
Summary
HTTP client based sinks can leak sockets that are stuck in the TLS handshake, until the process runs out of file descriptors (#27245).
When a request needs a connection, hyper-util's legacy client races a pool checkout against a new connect. If the checkout wins after the connect has started, the unfinished connect is spawned into the background so that it can finish and be pooled. Nothing bounds that background task: the request timeout no longer applies to it, the connector has no connect timeout by default, and client sockets have no TCP keepalive. If the handshake stalls, for example because the peer's FIN/RST was lost on a lossy path, the socket stays open until Vector exits.
vector-httpalready supportsConnectionOptions::connect_timeout, which covers the TCP connect, the TLS handshake and a proxyCONNECTtunnel, but theHttpClient::newthat sinks use did not set it. This PR:HttpClient::new(src/http/client.rs) limit connection setup toDEFAULT_CONNECT_TIMEOUT(65s). This is a little above the default sink request timeout (60s), so with default settings a connect made for a request is still ended by the request timeout first, as today. If both timers were 60s they could expire on the same tick and the request would fail with a connect error instead ofElapsed, which takes a different retry path.HttpClient::new_with_optionsis unchanged, so callers that pass their own options behave as before.vector-http'shttps_connector, with the curl CLI defaults: probes after 60s idle, every 60s, closing after 9 unanswered probes (about 600s to detect a dead peer). The kernel then closes a connection whose peer silently went away. This applies to everyvector-httpclient.User-visible changes:
HttpClient::newwhoserequest.timeout_secsis above 65 now have a 65s limit on connection setup.Not changed here, possible follow-ups:
Http2Client(thevectorsink, thegcp_pubsubsource) gets keepalive but no connect timeout. HTTP/2 connections are shared, so the background-connect race is much rarer there.References
Closes: #27245
Vector configuration
The reproduction in #27245: a TLS server that answers the first connection and accepts but never answers the handshake of later ones, with ~170ms RTT added via
netem, and anhttpsink withrequest.timeout_secs: 5.How did you test this PR?
cargo test -p vector-http --lib -- connector::tests: all pass.-
connect_timeout_bounds_a_stalled_tls_handshakenow also checks that the client closes the stalled connection: after the ClientHello the server must read EOF within 5s. Before, it only checked that the request failed withTimedOut.- New
client_connections_enable_tcp_keepalivechecksSO_KEEPALIVEon a connection made byhttps_connector, and on Linux also the idle time, interval and probe count.- Mutation check: with
set_keepaliveorset_keepalive_intervalremoved, and with the connect spawned into a background task before the timeout (so the request fails but the socket stays open), the tests fail, at the keepalive assertions and at "the client should close the stalled connection". Removingset_keepalive_retriesis not caught on Linux, because the kernel default is also 9.http::transport_tests::new_client_limits_a_stalled_connect_by_default: a client fromHttpClient::newwith no request timeout, against a server that never answers the handshake; after advancing the paused clock pastDEFAULT_CONNECT_TIMEOUT, the request fails withTimedOut. WithHttpClient::newreverted toConnectionOptions::default(), the test fails.cargo test -p vector --lib --no-default-features -- http::: 66 passed.cargo clippy -p vector-http --all-targets -- -D warningsandcargo clippy -p vector --lib --tests --no-default-features -- -D warningsare clean, andrustfmt --checkpasses on the changed files. I did not run the full-featuremake check-clippylocally (not enough memory on my machine).Does this PR include user facing changes?
no-changeloglabel to this PR.🤖 Generated with Claude Code