Skip to content

fix(hono): resolve "ReadableStream is locked" error in AI SDK#384

Merged
HugoRCD merged 1 commit into
HugoRCD:mainfrom
nadaniels:fix/hono-readable-stream-is-locked-error
Jun 13, 2026
Merged

fix(hono): resolve "ReadableStream is locked" error in AI SDK#384
HugoRCD merged 1 commit into
HugoRCD:mainfrom
nadaniels:fix/hono-readable-stream-is-locked-error

Conversation

@nadaniels

@nadaniels nadaniels commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

🔗 Linked issue

Closes #382

📚 Description

Using createUIMessageStreamResponse or createAgentUIStreamResponse from the Vercel AI SDK inside a Hono route would throw ERR_INVALID_STATE: ReadableStream is locked when running under @hono/node-server.

Root cause: The middleware called createObservedBody(c.res.body) (which calls body.getReader(), locking the stream) and then relied on Hono's compose to update c.res with the wrapped response via the middleware return value. However, Hono skips that update when context.finalized is already true — which is always the case after a route handler returns a Response. This left c.res pointing at the original response whose body was now locked, so @hono/node-server's subsequent response.body.getReader() call threw.

Fix: Explicitly assign c.res = await finishResponse(c.res, ...) instead of returning the wrapped response, so c.res is always updated regardless of context.finalized.

Added additional regression tests

Summary by CodeRabbit

Release Notes

  • Bug Fixes

    • Fixed a critical streaming response issue in Hono integration that caused "ReadableStream is locked" errors when using Vercel AI SDK streaming helpers.
  • Tests

    • Added comprehensive test coverage for streaming responses in Hono integration, including Server-Sent Events (SSE) behavior and context handling validation.

@vercel

vercel Bot commented Jun 12, 2026

Copy link
Copy Markdown

@nadaniels is attempting to deploy a commit to the HRCD Projects Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the bug Something isn't working label Jun 12, 2026
@github-actions

github-actions Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Thank you for following the naming conventions! 🙏

@coderabbitai

coderabbitai Bot commented Jun 12, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro Plus

Run ID: 167bf827-e4d0-4016-81e1-afe8beb1827d

📥 Commits

Reviewing files that changed from the base of the PR and between 963e7c7 and e2a33c9.

📒 Files selected for processing (3)
  • .changeset/salty-apes-find.md
  • packages/evlog/src/hono/index.ts
  • packages/evlog/test/frameworks/hono.test.ts

📝 Walkthrough

Walkthrough

This PR fixes the ERR_INVALID_STATE: ReadableStream is locked error that occurs when Hono middleware is used with Vercel AI SDK streaming responses. The fix explicitly reassigns c.res to the finished response in deferred-emission paths, and includes new streaming-focused tests to verify the behavior.

Changes

Streaming ReadableStream Lock Fix

Layer / File(s) Summary
Deferred-response reassignment for streaming
packages/evlog/src/hono/index.ts, .changeset/salty-apes-find.md
The Hono middleware now directly assigns c.res = await finishResponse(c.res, {...}) in deferred-emission paths instead of creating a new Response wrapper, ensuring the finished response and its unlocked body stream state are always set on the context. The changeset documents this as a patch fix for issue #382.
Streaming response test validation
packages/evlog/test/frameworks/hono.test.ts
New test suite verifies that streaming text/event-stream responses do not lock the response body before caller reads, and that evlog correctly defers calling drain until the stream closes while capturing mid-stream context updates.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~12 minutes

Possibly related PRs

  • HugoRCD/evlog#367: The retrieved PR implements streaming-aware deferred "wide-event" emission across integrations (including Hono) via shouldDeferEmitForResponse/finishResponse plumbing, and the main PR's Hono-specific change reassigns c.res = await finishResponse(...) to make that deferred/finished response handling work correctly for streaming (preventing the locked-body race).

Suggested labels

bug

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly and concisely summarizes the main fix: resolving the ReadableStream locked error in Hono with AI SDK integration.
Description check ✅ Passed The PR description comprehensively covers the linked issue, root cause analysis, the implemented fix, and mentions regression tests; it follows the template structure with linked issue and detailed description sections.
Linked Issues check ✅ Passed The PR directly addresses all requirements from #382: prevents the middleware from locking the response body and ensures compatibility with Hono/@hono/node-server when streaming AI SDK responses.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the bug fix: changeset entry, Hono middleware fix, and related regression tests; no unrelated modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Warning

There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure.

🔧 ESLint

If the error stems from missing dependencies, add them to the package.json file. For unrecoverable errors (e.g., due to private dependencies), disable the tool in the CodeRabbit configuration.

ESLint install timed out. The project may have too many dependencies for the sandbox.


Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@HugoRCD HugoRCD merged commit 6eb0957 into HugoRCD:main Jun 13, 2026
15 of 17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[bug] Hono middleware + AI SDK's createUIMessageStreamResponse crashes request

2 participants