Skip to content

fix(ai-chat): close chat stream on socket disconnect#1487

Merged
whoiskatrin merged 1 commit into
mainfrom
fix/ai-chat-recovery-stuck-stream
May 9, 2026
Merged

fix(ai-chat): close chat stream on socket disconnect#1487
whoiskatrin merged 1 commit into
mainfrom
fix/ai-chat-recovery-stuck-stream

Conversation

@whoiskatrin
Copy link
Copy Markdown
Contributor

@whoiskatrin whoiskatrin commented May 8, 2026

Summary

Fixes #1485.

When an AIChatAgent turn is interrupted by a Worker/socket restart, chatRecovery can complete the recovered continuation under a new server-generated request id. The browser-side WebSocketChatTransport.sendMessages() stream for the original request id was only closed by a matching done: true frame. If the socket closed before that terminal frame arrived, the local stream stayed pending indefinitely, leaving AI SDK useChat / useAgentChat stuck in status === "streaming" even after the recovered continuation finished.

This PR closes the original local sendMessages() stream when the underlying socket closes, cleaning up its active request id so the AI SDK is not left waiting on an impossible terminal frame.

Changes

  • Add a close event listener in WebSocketChatTransport.sendMessages() that closes and cleans up the local stream.
  • Add a focused regression test for the socket-close-before-done lifecycle.
  • Add a patch changeset for @cloudflare/ai-chat.

Verification

  • cd packages/ai-chat && npx vitest --run src/tests/ws-transport-resume.test.ts
  • npm run check was run;

Notes

The fix is intentionally narrow: socket close means the original local stream can no longer receive its matching terminal frame on that socket. Closing it allows the AI SDK request state to settle while recovery/resume paths continue to handle any recovered stream under the new request id.

@changeset-bot
Copy link
Copy Markdown

changeset-bot Bot commented May 8, 2026

🦋 Changeset detected

Latest commit: 31deea1

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@cloudflare/ai-chat Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 8, 2026

Open in StackBlitz

agents

npm i https://pkg.pr.new/agents@1487

@cloudflare/ai-chat

npm i https://pkg.pr.new/@cloudflare/ai-chat@1487

@cloudflare/codemode

npm i https://pkg.pr.new/@cloudflare/codemode@1487

hono-agents

npm i https://pkg.pr.new/hono-agents@1487

@cloudflare/shell

npm i https://pkg.pr.new/@cloudflare/shell@1487

@cloudflare/think

npm i https://pkg.pr.new/@cloudflare/think@1487

@cloudflare/voice

npm i https://pkg.pr.new/@cloudflare/voice@1487

@cloudflare/worker-bundler

npm i https://pkg.pr.new/@cloudflare/worker-bundler@1487

commit: 31deea1

Copy link
Copy Markdown
Contributor

@devin-ai-integration devin-ai-integration Bot left a comment

Choose a reason for hiding this comment

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

✅ Devin Review: No Issues Found

Devin Review analyzed this PR and found no potential bugs to report.

View in Devin Review to see 2 additional findings.

Open in Devin Review

@whoiskatrin whoiskatrin marked this pull request as ready for review May 8, 2026 21:47
@whoiskatrin whoiskatrin merged commit 752e25a into main May 9, 2026
4 checks passed
@whoiskatrin whoiskatrin deleted the fix/ai-chat-recovery-stuck-stream branch May 9, 2026 13:55
@github-actions github-actions Bot mentioned this pull request May 9, 2026
threepointone added a commit that referenced this pull request May 11, 2026
* fix(voice): harden Workers AI STT turn handling

Follow up on PR #1458 by preserving Flux turn transcripts across lifecycle events and using model-detected speech start for low-latency barge-in.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(chat): harden stream resume negotiation close races

Follow-up to PR #1463: route stream-resume negotiation sends through close-safe helpers so WebSocket close races do not crash resume handling in think and ai-chat.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(voice): parse raw NDJSON text streams

Follow-up to PR #1462: make the voice text stream parser honor its documented NDJSON support while preserving SSE parsing for AI text streams.

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(agents): defer recovered agent-tool finish hooks (#1476)

Co-authored-by: Cursor <cursoragent@cursor.com>

* test(voice): cover useVoiceAgent enabled lifecycle (#1478)

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(ai-chat): close resumed streams on disconnect (#1487)

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(voice): invalidate playback on client interrupt (#1458)

Co-authored-by: Cursor <cursoragent@cursor.com>

* fix(voice): invalidate playback when ending calls (#1458)

Co-authored-by: Cursor <cursoragent@cursor.com>

* Run deferred finish hooks after successful startup

Ensure recovered agent-tool finish hooks are only executed after a successful user onStart. Await _runDeferredAgentToolFinishHooks inside the onStart flow so deferred finishes are skipped when startup fails. Add a test and helper (reconcileCompletedChildWithFailedStartupForTest) to verify finish hooks are not run on failed startup and to cover lifecycle ordering and event emission.

---------

Co-authored-by: Cursor <cursoragent@cursor.com>
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.

useAgentChat stays stuck streaming after chatRecovery continues under a new request id

1 participant