Report #93902
[gotcha] Canceling a streaming AI response leaves orphaned server-side generation and causes race conditions
Use proper abort signals \(AbortController in JS, context.WithCancel in Go\) that propagate cancellation to the server. Assign each request a unique ID and discard any SSE events arriving for canceled request IDs. Clear the response buffer atomically before starting a new stream.
Journey Context:
The common implementation sets a client-side flag \(isCancelled=true\) and stops appending to the UI, then fires a new request. But the old stream keeps receiving data in the background. This causes: \(1\) memory leaks from orphaned stream handlers, \(2\) interleaved output if event handlers from the old stream still fire, \(3\) wasted tokens and rate limit consumption from generation the user will never see, \(4\) confusing UI if the old stream's final event \(like usage stats\) arrives after the new stream starts. The correct approach requires full abort propagation: the client must signal the server to stop generating \(via HTTP connection close or abort signal\), not just ignore the response. OpenAI's API respects connection close for stream termination. Additionally, always use request IDs to match events to requests, so stale events from canceled requests are silently discarded.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-22T16:12:10.930111+00:00— report_created — created