OpenAI stream producers survive cancellation and WebSocket request sends lack a deadline
Problem
The OpenAI Responses runtime has two related lifecycle gaps that can make a stalled request difficult to cancel and can leave the next request blocked behind the previous producer:
Provider::completestarts a detached producer withtokio::spawn, then returns a bareReceiverStream. Dropping the consumer does not cancel the producer's pending transport/retry work. A channel send eventually notices closure, but a producer waiting for a socket, a mutex, an idle stream, or retry backoff need not reach that send promptly.- Fresh and persistent WebSocket request
send/flush waits are not wrapped in a deadline. The response-read clocks start only after the request is sent. A peer that stops reading can therefore stall before those clocks apply.
These are source-level findings on current upstream a61c38ee9, not a claim that any particular remote-provider outage or large conversation is fully explained by them.
Relevant code
crates/jcode-provider-openai-runtime/src/openai_provider_impl.rs: producer spawn, retry loop, and returnedReceiverStream.crates/jcode-provider-openai-runtime/src/openai_stream_runtime.rs: persistent-state mutex ownership, persistent request send, fresh request send, and response-read deadlines.crates/jcode-provider-core/src/attempt_tracker.rs: attempt-output forwarder lifecycle, which must not obscure closure of the original consumer.
The persistent continuation holds the state lock across the request. A cancellation fix must invalidate an interrupted response chain before handing off that lock. Clearing the slot later, after unlocking, can accidentally erase a replacement connection belonging to a newer request.
Synthetic reproduction / required regression coverage
Use dummy credentials and loopback/mock transports only:
- Accept a synthetic WebSocket request, then never emit a response. Drop the public
EventStreamconsumer. Assert the producer promptly terminates, the persistent slot is not reused, and a subsequent request is not stuck behind the abandoned one. - Cancel a producer waiting to acquire another request's persistent-state lock. Assert it does not clear or corrupt the owner's connection.
- Use a synthetic pending sink to block request send/flush. Assert the existing configured transport deadline bounds that phase, without imposing an unrelated short limit on legitimate silent reasoning.
- Cancel during retry backoff or an idle attempt-output forwarder. Assert those tasks do not survive the original consumer.
- Preserve completed-response reuse and normal output/error forwarding.
Baseline reproduction
Synthetic regressions against the unmodified production implementation reproduced:
- Consumer-drop and forwarder-guard-drop failures at the bounded 100 ms assertion.
- An aborted in-flight continuation handing off
Somestale socket state instead of invalidating it. - A canceled producer waiting for the persistent-state lock retaining ownership beyond a 1 second bound.
These bounds are test acceptance checks, not proposed production generation limits. The public-stream reconnection and pending-send/flush cases are also part of the regression plan.
Regression results and the exact fix will be supplied in the linked PR. This issue intentionally contains no session transcripts, account identifiers, real prompts, filesystem paths, request bodies, credentials, encrypted reasoning, or user logs.
Expected behavior
- Consumer cancellation promptly stops abandoned transport, persistent-mutex, retry/backoff, and forwarding work. An already-started shared OAuth refresh/credential commit remains a separate transaction boundary.
- Interrupted persistent conversations are invalidated under the same lock, without clearing an unrelated healthy or replacement state.
- WebSocket send/flush, connection, and response waits are all bounded by appropriate existing timeout policies.
- Normal long-running reasoning, explicit continuation, healthy persistent reuse, and retry rollback semantics remain supported.
- Cancellation must not interrupt an already-started OAuth refresh transaction between token rotation, credential persistence, and in-memory commit. That shared transaction needs cancellation-safe ownership, with synthetic gated regression coverage and no live credentials.
Scope and related work
This is a focused provider-lifecycle fix, not a general worker watchdog or a blanket ban on empty continuation messages.
- #620 addressed an earlier first-byte timeout class. These findings concern the current OpenAI detached producer and WebSocket send/cancellation boundary.
- #1352 / #1362 address swarm stop quiescence and queued worker execution. A provider producer surviving
EventStreamdrop is a separate layer. - #1703 requests general stalled-worker monitoring. These bounded lifecycle fixes can complement that work without implementing automatic worker replacement.
- #1757 tracks the separate OAuth refresh HTTP deadline gap and rotation-commit safety. This issue does not claim to bound that existing authentication transaction.
Thanks for the careful source-level analysis. The fix is in #1758. It changes cancellation and credential-commit behavior in the OpenAI runtime, so it needs maintainer review rather than an automated fix. Related: #1757, #1759.
--- — Jcode agent (automated triage), on behalf of @1jehuang