Skip to content

fix(binding-http): client connection reuse per origin/authority + per-connection exchange/queue scope - #1812

Merged
jfallows merged 6 commits into
developfrom
fix/1811-binding-http-connection-reuse
Jun 2, 2026
Merged

jfallows merged 6 commits into
developfrom
fix/1811-binding-http-connection-reuse

Conversation

@jfallows

@jfallows jfallows commented Jun 2, 2026

Copy link
Copy Markdown
Contributor

Description

General binding-http client connection-management fixes, extracted from the
examples/mcp.proxy branch (#1793)
so they can land on develop independently. The
defect surfaced via the MCP proxy's shared sys:http_client fanning out to multiple
upstream authorities, but it is general to binding-http: any single http client binding
that routes to more than one :authority is affected.

Fixes #1811.

Changes

  • Reuse client connections per origin / :authority via a canServe coalescing seam on
    HttpClient (previously supplyClient() reused any idle connection by HTTP version + idle
    state only, never matching :authority, so a request could be sent over a connection to a
    different host).
  • Scope exchanges + the request queue per connection (moved from HttpClientPool down
    into HttpClient). Stream ids are per-connection (1, 3, 5 …), so the pool-wide map/queue
    collided across concurrent authorities and stalled a request.
  • Pin HTTP/2 stream-id scope per connection.
  • Distribute concurrent same-origin requests across connections (tiered supplyClient:
    prefer-ready / new-under-limit / at-limit-fallback).
  • Guard a null exchange when decoding HTTP/1.1 response headers in a teardown race.

Testing

./mvnw verify -pl runtime/binding-http — rfc7230/rfc7540 client + server
connection-management ITs green, including new regression scenarios:

  • requests.different.authorities — sequential requests to two authorities through one
    client binding must open two connections (old: response2 never arrives).
  • concurrent.requests.different.authorities — two concurrent cross-authority exchanges
    coexist (old: TestTimedOut; new: passes).
  • concurrent.requests.same.authority — concurrent same-origin requests distributed.

🤖 Generated with Claude Code

jfallows and others added 6 commits June 2, 2026 15:07
…e headers

A response arriving on a kept-alive HTTP/1.1 connection after its exchange was
torn down (e.g. the application reset the response) left client.exchange null;
onDecodeHttp11Headers then dereferenced it, throwing inside the worker
(AgentTerminationException) and crash-looping the engine. HTTP/2 already
tolerates a frame for an unknown stream. Guard the HTTP/1.1 response-headers
decode the same way: when there is no exchange to receive the response, the
connection is desynchronized, so ignore the remainder and clean up the network.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 7b5a863)
HttpClientPool is keyed only by resolvedId, and supplyClient() selected a
reusable connection by HTTP version and idle/reply state without matching the
request :authority. When one http client binding fans out to multiple
authorities (e.g. the MCP proxy routing several south mcp clients through a
shared sys:http_client), an idle connection established to authority A could be
reused to send a request for authority B, so B's request was answered by A.

Track the connected authority on HttpClient and require it to match in
supplyClient(); create a new connection per authority otherwise.

Adds a failing-first IT (requests.different.authorities) driving two sequential
requests with distinct authorities through one client binding and asserting two
distinct upstream connections.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 43750d8)
…ction

The per-authority connection reuse fix (43750d8) made supplyClient() filter
by :authority, so one HttpClientPool (keyed only by resolvedId) now hosts
several HttpClient connections for different authorities. The pool, however,
kept a single exchanges map and a single request queue
(httpQueueSlot/flushNext/availableSlotSize), both keyed by streamId — yet
streamId comes from HttpClient.nextStreamId() (maxClientStreamId), which is
per-connection and restarts at 1 for every client.

Two concurrent requests to different authorities therefore both allocate
streamId 1: the later exchanges.put() evicts the earlier exchange, and the
pool-wide flushNext() resolves a queued request via exchanges.get(streamId) to
the wrong (clobbered) exchange, so one request stalls indefinitely. This
surfaced as a hang when the MCP proxy hydrated tools/prompts/resources across
three upstream authorities through one shared sys:http_client.

Move the exchanges map and the request queue down onto HttpClient so they are
scoped per connection, matching the per-connection streamId space.

Adds a failing-first IT (concurrent.requests.different.authorities) driving two
concurrent requests with distinct authorities through one client binding, with
the server holding both requests open before responding; it times out against
the previous code and passes with the fix.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 71d754a)
…ent authorities

Adds an HTTP/2 counterpart to the rfc7230 concurrent.requests.different.authorities
regression: two concurrent requests to distinct authorities each open their own h2
connection in one shared pool, the server holds both requests open (cross-await)
before responding, and both responses arrive on wire stream id 1.

Before the per-connection exchanges/queue fix this collides in the pool-wide map
(both connections' stream 1 alias the same entry) and times out; with the fix each
connection owns its stream-id space and both exchanges complete.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit d9964d2)
…Serve seam

Connection reuse eligibility was an inline exact match on :authority (host:port)
only, with :scheme ignored. Capture :scheme alongside :authority and route the
selection in supplyClient() through a canServe(scheme, authority) predicate on
HttpClient, so reuse is keyed by origin (scheme + host + port) per RFC 6454.

canServe also carries a serviceableNames set (empty for now) as the seam for
HTTP/2 connection coalescing: a request may later reuse an h2 connection whose
TLS certificate covers the requested name, even when the authority differs.
Populating serviceableNames from certificate SANs surfaced by the tls binding,
and refining the match to host-vs-SAN, is a purely additive follow-on; HTTP/1.1
never consults SANs. Today both branches reduce to an exact origin match, so
behaviour is unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit 5d6d506)
…connections

supplyClient() reused a still-connecting connection for any further request to
the same origin (the !replyOpened clause), so two concurrent requests to one
authority piled onto a single HTTP/1.1 connection and the second stalled behind
the first instead of opening a second connection.

Make connection selection tiered: (1) prefer a connection ready to take the
request now — an HTTP/2 connection (multiplexes), a connecting connection on an
h2-capable pool (will multiplex), or an idle HTTP/1.1 connection with nothing
queued; (2) otherwise open a new connection while under maximum.connections;
(3) only when at the limit, fall back to the previous reuse semantics and queue
behind a busy connection. HTTP/2 multiplexing, h2c, idle reuse, and at-limit
queueing (pool size 1) are all unchanged; only concurrent same-origin HTTP/1.1
requests under available capacity now fan out across connections.

Adds a failing-first IT (concurrent.requests.same.authority): two concurrent
requests to one authority with the server holding both open before responding;
it times out against the previous code and passes once the requests distribute.

Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
(cherry picked from commit cc3a740)

@jfallows jfallows left a comment

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

LGTM

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.

binding-http: client connection reuse per origin/authority + per-connection exchange/queue scope

1 participant