Repository navigation
Description
Out of scope
No trust matching, approval-marker, byte-budget, policy-refresh or authority changes. No retry that can duplicate an upstream request. Do not weaken cancellation or classify this production race as a flake.
Context
Follow-up to #3609, introduced in PR #4270's HTTP egress lifecycle. Normal consecutive requests to one origin can receive 502 when the HTTP agent lends a stale socket before it processes an origin close. The original completion-destroy hypothesis below is historical evidence and is corrected by the deterministic probe at the end of this issue. The lifecycle aborts its controller at completion (packages/ui-sdk/src/server/egress-operation.ts:34); the HTTP abort listener calls outgoing.destroy (packages/ui-sdk/src/server/egress-proxy.ts:580). The response-close callback also destroys that request.
Candidate run 44896042526, unit (6), job 136822171856 failed the unchanged test shared Network card with real FULL writer excludes only peer reservations (independent none) in packages/ui-server/tests/origin-trust.test.ts:190: expected [200,200], received [200,502].
Keyless local repetition of that exact single test reproduced the same assertion at both commits: bcbc515 gave 145 pass / 5 fail across 150 repetitions; parent edf1a3b gave 149 pass / 1 fail across 150. An external transport-error preload on the parent gave 149 pass / 1 fail again. Neither CPU burners nor an installed-native runtime was needed. The fixture uses an in-memory trust double with read/revision/subscribe and never calls live.refresh, so PR #4405 does not cause this failure.
A separate event-order probe at the parent failed on repetition 76. Its retained JSON-lines trace records logical object identities and monotonic event times, without request content or credentials:
- First upstream request 1509 uses socket 1510; it receives 200 at 13913.797 ms, completes its response at 13914.049 ms, and emits request close at 13914.091 ms.
- Proxy response close at 13914.157 ms runs lifecycle completion. The controller aborts at 13914.182 ms; the HTTP abort listener invokes outgoing.destroy at 13914.203 ms. The response-close listener invokes destroy again at 13914.361 ms.
- Second upstream request 1514, path /again, receives the same socket 1510 at 13914.868 ms and emits finish at 13915.268 ms.
- That second request reports socket hang up / ECONNRESET at 13915.489 ms. Only afterward does upstreamFailed abort the second operation and return 502.
The evidence consists of the hosted job log, uninstrumented head/parent repetition logs, and the retained external-preload JSON-lines event-order trace. Both source checkouts were clean; no failed CI job was rerun. Routing triage identifies this as a real production socket-ownership race, independent of #4405. The later controlled probe below corrects its specific mechanism.
Scope
Prevent stale upstream pooled-socket reuse between independent HTTP operations. A deterministic FIN-order probe corrects the original late-destroy hypothesis; the approved production fix is option (a), explicit agent:false, implemented in draft PR #4409. Keep trust fences, accounting and in-flight cancellation intact.
Acceptance criteria
- Prevent the stale pooled socket from failing a subsequent independent operation, using an explicitly non-pooled upstream connection or a separately approved narrowly bounded retry.
- Preserve deterministic real socket/FIN ordering proof: the old path must reuse the exact socket and produce 502, while the repaired path succeeds; add a restored mutation.
- In-flight cancellation still destroys its owned transport promptly with the existing cancellation evidence.
- Never replay non-idempotent requests or treat no response as proof that no request bytes reached the origin.
- Meter every actual application-byte send exactly once and run the final trust/authority fence for every send; any retry needs its own charged, fenced attempt.
- Keep the FULL-writer peer and independent-input regression assertions unchanged.
Verification
Run relevant egress proxy/lifecycle/FULL-writer tests with real loopback sockets, the deterministic reuse ordering, cancellation and a named mutation. Preserve failing-first and exact-head restored receipts. No installed-native Claude execution is required locally.
Contract impact
No wire/API shape change. Preserve successful dispatch/contacted facts, existing refusal classifications and append-only provider history. Include a patch changeset for the affected shipped package.
Root-cause correction and deterministic proof
The earlier completion-destroy attribution was too strong. The captured first ClientRequest was already destroyed before lifecycle completion; the installed runtime's destroy() returns immediately in that state. A controlled real maxSockets:1/keepAlive reuse test that delays completion until the peer owns the socket passes on current code. Suppressing destroy() entirely for completed responses still reproduced the original [200,502] failure after 264 passing repetitions. Those observations rule out claiming that late destroy alone caused the recorded failure.
The reproducible mechanism is a stale pooled socket: the origin ends a persistent connection without a Connection: close response header; the agent lends it before the end notification has been processed, and the next request receives ECONNRESET before any response. A deterministic raw-TCP-origin test now forces that ordering. It sends a real FIN after response completion, holds only the local end notification, confirms the next request gets the exact same Socket with reusedSocket=true, waits for its request write, and delivers the retained end event. It fails 20/20 repetitions at STALE_UPSTREAM_SOCKET_NEVER_BREAKS_NEXT_OPERATION (expected 200, received 502), without a timeout or fabricated transport error. In-flight cancellation and late-completion reuse controls still pass. Product code has not changed.
Revised acceptance criteria
- Prevent the stale pooled socket from failing a subsequent independent operation, either by an explicitly non-pooled upstream connection per operation or a separately approved narrowly bounded retry. No transport from an earlier operation may be reused accidentally.
- Retain the deterministic real socket/FIN ordering test and a mutation. The test must prove actual reuse on the old path and normal success on the repaired path; a timeout or module-load failure is not proof.
- Keep in-flight cancellation effective and preserve outcome/refusal evidence.
- Never replay a non-idempotent request; never infer that no bytes reached the origin merely because no response arrived.
- Egress accounting must cover each actual application-byte send exactly once and must not skip the final trust/authority fence. A retry would need its own correctly charged, fenced send.
- The real FULL-writer peer and independent-input variants retain their existing assertions.
Recommendation before implementation
Prefer an explicit non-pooled upstream connection (agent:false). The proxy already sends Connection: close, so this makes agent behavior match its declared transport behavior. In two 500-request keyless loopback rounds against an origin honoring close, current pooling plus Connection: close and agent:false both created 500 connections and had zero failures. Totals were 111.9/56.3 ms for current behavior and 79.5/46.6 ms for agent:false. This microbenchmark does not predict remote latency.
For comparison, an intentionally persistent client/origin used one connection for 500 requests, totaling 26.4/14.1 ms; disabling that pooling adds connection setup (potentially a TCP round trip remotely). The proxy's ordinary HTTP path is cleartext; CONNECT already has a per-operation socket. No extra application head/body is sent, no replay occurs, the existing byte meters run once, and every operation retains its final admission fence. A retry is less suitable: ClientRequest exposes writes and response receipt, not proof that an origin acknowledged no request bytes. A retry also adds actual head bytes and requires explicit per-attempt accounting and revalidation. Option (a) was approved and is implemented in draft PR #4409 at 0b52682; independent review and hosted qualification remain required.
2026-11-21 correction: replaced the late-destroy acceptance criteria with the proven stale-pool mechanism and recorded the pre-implementation recommendation; no production fix is claimed.
Implementation candidate — 2026-11-21
Draft PR #4409 at 0b52682 applies agent:false at the single HTTP upstream constructor, with no retry or meter/fence change. CONNECT already owns its socket; adjacent Fetch relays use separate transports. The final deterministic fixture fails before the fix and when the guard is removed, then passes restored. The requested FULL-writer test gives 296 pass / 4 fail before and 300 pass / 0 fail after. Exact-head focused verification gives 122 pass / 0 fail / 548 assertions; check:pr passes all gates and 670 fast invariants. Typecheck and full build pass. The same CONTRACT commit includes the SDK patch changeset and documented non-pooling behavior. No installed-native Claude test ran locally. The branch is handed back as draft for independent review; no confirmation is requested. #3609 remains open.
Activity
schlessera commentedon Nov 21, 2026
Root-cause correction and deterministic proof
The earlier completion-destroy attribution was too strong. The captured first ClientRequest was already destroyed before lifecycle completion; the installed runtime's destroy() returns immediately in that state. A controlled real maxSockets:1/keepAlive reuse test that delays completion until the peer owns the socket passes on current code. Suppressing destroy() entirely for completed responses still reproduced the original [200,502] failure after 264 passing repetitions. Those observations rule out claiming that late destroy alone caused the recorded failure.
The reproducible mechanism is a stale pooled socket: the origin ends a persistent connection without a Connection: close response header; the agent lends it before the end notification has been processed, and the next request receives ECONNRESET before any response. A deterministic raw-TCP-origin test now forces that ordering. It sends a real FIN after response completion, holds only the local end notification, confirms the next request gets the exact same Socket with reusedSocket=true, waits for its request write, and delivers the retained end event. It fails 20/20 repetitions at STALE_UPSTREAM_SOCKET_NEVER_BREAKS_NEXT_OPERATION (expected 200, received 502), without a timeout or fabricated transport error. In-flight cancellation and late-completion reuse controls still pass. Product code has not changed.
Revised acceptance criteria
Recommendation before implementation
Prefer an explicit non-pooled upstream connection (agent:false). The proxy already sends Connection: close, so this makes agent behavior match its declared transport behavior. In two 500-request keyless loopback rounds against an origin honoring close, current pooling plus Connection: close and agent:false both created 500 connections and had zero failures. Totals were 111.9/56.3 ms for current behavior and 79.5/46.6 ms for agent:false. This microbenchmark does not predict remote latency.
For comparison, an intentionally persistent client/origin used one connection for 500 requests, totaling 26.4/14.1 ms; disabling that pooling adds connection setup (potentially a TCP round trip remotely). The proxy's ordinary HTTP path is cleartext; CONNECT already has a per-operation socket. No extra application head/body is sent, no replay occurs, the existing byte meters run once, and every operation retains its final admission fence. A retry is less suitable: ClientRequest exposes writes and response receipt, not proof that an origin acknowledged no request bytes. A retry also adds actual head bytes and requires explicit per-attempt accounting and revalidation. Implementation awaits the routing decision.