You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
If a client closes the connection, it should be handled quietly
Additional information
TypeError [ERR_INVALID_STATE]: Invalid state: Controller is already closed
at ReadableStreamDefaultController.enqueue (node:internal/webstreams/readablestream:1074:13)
at Timeout.pushRandomData (/home/magne/workspace/repro/hono-streaming/src/index.ts:38:20)
at listOnTimeout (node:internal/timers:573:17)
at process.processTimers (node:internal/timers:514:7) {
code: 'ERR_INVALID_STATE'
}
I've been looking into this issue and have some thoughts on the optimal solution.
First, I was able to reproduce and fix the issue in my own code by adding proper error handling around the stream controller operations:
functionpushRandomData(){// Check if already closed with a flagif(isClosed)return;// Generate and enqueue datacontroller.enqueue(data);// Schedule next pushtimeoutId=setTimeout(pushRandomData,50);}// Add the cancel callback to ReadableStreamcancel(){isClosed=true;},// Or listen for client disconnectionc.req.raw.signal.addEventListener('abort',()=>{isClosed=true;});
While this works, I believe Hono should provide a safety net at the framework level to prevent server crashes when streams are closed unexpectedly. The ideal solution would:
Prevent crashes: Catch "controller already closed" errors at the framework level to prevent the entire server from crashing
Provide warnings: Log warnings instead of crashing so developers are aware of the issue
Signal disconnections: Make it easier for developers to detect when clients disconnect so they can clean up resources (timers, database connections, etc.)
This approach maintains a good separation of concerns:
This would improve developer experience while still encouraging proper resource management. Even if Hono gracefully handles closed stream errors, developers should still implement proper cleanup to avoid wasting server resources on disconnected clients.
Would the Hono team consider implementing this kind of safety mechanism in the core framework?
This is not a hono core issue. This is because this problem occurs even without using hono.
import{serve}from'@hono/node-server'constapp={fetch: ()=>{constrandomDataStream=newReadableStream({//...})returnnewResponse(randomDataStream)}}serve({fetch: app.fetch,port: 3000},(info)=>{console.log(`Server is running on http://localhost:${info.port}`)})
So, hono should not handle this.
It's my opinion. It would be ideal if the JS runtime handled it. For example, if you run it on Wrangler/workerd and Cloudflare, it does not throw an error and does not crash.
In deno serve, cancel() is called in the same way as in node-server, and an error occurs in queue().
In bun and wrangler, cancel() is not called.
Based on these situations, I think it would be better not to call cancel() in node-server either, but I think this is not a bug, but rather a question of which specification is more convenient.
Activity
nitedani commentedon Apr 15, 2025
I've been looking into this issue and have some thoughts on the optimal solution.
First, I was able to reproduce and fix the issue in my own code by adding proper error handling around the stream controller operations:
While this works, I believe Hono should provide a safety net at the framework level to prevent server crashes when streams are closed unexpectedly. The ideal solution would:
This approach maintains a good separation of concerns:
This would improve developer experience while still encouraging proper resource management. Even if Hono gracefully handles closed stream errors, developers should still implement proper cleanup to avoid wasting server resources on disconnected clients.
Would the Hono team consider implementing this kind of safety mechanism in the core framework?
yusukebe commentedon Apr 26, 2025
@magne4000 @nitedani
This is not a
honocore issue. This is because this problem occurs even without usinghono.So,
honoshould not handle this.It's my opinion. It would be ideal if the JS runtime handled it. For example, if you run it on Wrangler/workerd and Cloudflare, it does not throw an error and does not crash.
Anyway, this is not a bug.
magne4000 commentedon Apr 28, 2025
I would argue that this is a
@hono/node-serverbug then.yusukebe commentedon Apr 28, 2025
Okay. I'll transfer this issue to the
@hono/node-serverrepo.usualoma commentedon Jul 16, 2025
In
deno serve,cancel()is called in the same way as in node-server, and an error occurs inqueue().In bun and wrangler,
cancel()is not called.Based on these situations, I think it would be better not to call
cancel()in node-server either, but I think this is not a bug, but rather a question of which specification is more convenient.