Skip to content

fix(connpool): a request lost to an upstream's idle close goes out again - #574

Merged
7schmiede merged 2 commits into
mainfrom
fix/connpool-stale
Nov 18, 2026
Merged

7schmiede merged 2 commits into
mainfrom
fix/connpool-stale

Conversation

@7schmiede

Copy link
Copy Markdown
Member

What and why

Since #556 and #559, connections to a static forward's target, a public link's service and a daemon are kept (devstack_node::connpool), with no bound on how long one sits idle. A request written on a kept connection just as the upstream closed it for its keep-alive timeout (Node.js after 5 s) failed with "connection closed before message completed": hyper gives the request back only when it noticed the close first. The static server, the link router and the service proxy answered 502. Found in a code review before 0.49.4 (documented internally).

  • Retry once on a new connection when a request on a kept connection was handed back unsent, or the upstream closed the connection before any answer (closed_unanswered), for requests safe to send twice (replay: idempotent methods without a body). A request with a body, or one that failed on a new connection, is not sent twice.
  • Idle bound: a connection idle 4 s or more is not reused (below Node.js's 5 s; for Kestrel or nginx upstreams it only costs a new connection after a pause; forward: reuse connections to the target (keep-alive); a long run exhausts ephemeral ports on Windows #556's TIME_WAIT problem came from busy connections).
  • One pool for the class: the link router (its own copy of the old logic) and the daemon's service proxy (a hand-written pool with the same flaw) now go through connpool::Pool; draining an instance forgets its port as before. The hub's daemon_send resends a repeatable request at once when a reused connection was closed unanswered.

Verification

Bug class stale-pooled-connection
Red connpool::tests::a_kept_connection_the_upstream_closes_is_retried_on_a_new_one: Err("connection closed before message completed") instead of Ok(200) (a test upstream that closes on the next request).
Wrong implementations retrying regardless of method and body: a_request_with_a_body_or_not_idempotent_is_not_sent_twice fails; retrying on a new connection too: a_request_on_a_new_connection_is_not_sent_twice fails.
Green cargo test -p devstack-node, -p devstack-daemon, -p devstack-hub pass; clippy clean.
Siblings Static forward (already on Pool::send), link router and service proxy moved onto it, hub patched. One-shot connections (tls.rs, daemon_request_once, node handshakes) reuse nothing. ureq's own pools not checked.

🤖 Generated with Claude Code

7schmiede and others added 2 commits November 18, 2026 14:36
Since #556 and #559 the static server's forwards, the public link router
and the hub's way to a daemon keep idle HTTP/1.1 connections, with no
bound on how long. An upstream closes a connection idle for its
keep-alive timeout (Node.js 5 s, gunicorn 2 s); a request written on it
just as the close arrives fails although the upstream never answered.
hyper hands such a request back unsent only when it noticed the close
first; otherwise it reports "connection closed before message
completed", `Pool::send` gave up, and the proxy answered 502.

`connpool::Pool::send` now sends a request that failed on a kept
connection once more on a new one when hyper hands it back unsent, or
when the upstream closed the connection before any answer and the
request can be repeated: an idempotent method (RFC 9110 §9.2.2) without a
body, as browsers and Go's net/http do. A request with a body, a POST, or
one that failed on a new connection is not sent twice. A connection idle
for 4 s or longer is not reused at all, below Node.js's 5 s, so the race
is rare where it is most common.

The link router and the service proxy (`devstack-daemon`, `proxy.rs`),
which kept its own pool with the same flaw, now send through
`Pool::send`; the proxy forgets a drained instance's connections with
`Pool::forget`. The hub's `daemon_send` resends a repeatable request at
once on such a close instead of after its 100 ms pause.

Bug-Class: stale-pooled-connection

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@7schmiede
7schmiede merged commit cb132b6 into main Nov 18, 2026
14 checks passed
@7schmiede
7schmiede deleted the fix/connpool-stale branch November 18, 2026 14:07
@7schmiede 7schmiede mentioned this pull request Nov 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant