-
Notifications
You must be signed in to change notification settings - Fork 5.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Uncatchable Exception when response stream is closed #23058
Comments
I looked at this again. The exception is coming from the doing things with the const handler = (_request: Request): Response => {
const stream = new TextEncoderStream();
setInterval(() => {
const writer = stream.writable.getWriter();
writer.write("data\n").catch((e) => {
console.log("Error: ", e);
}).finally(() => {
writer.releaseLock();
});
}, 1000);
return new Response(stream.readable, {
status: 200,
});
};
console.log(`HTTP server running. Access it at: http://localhost:8080/`);
Deno.serve({ port: 8000 }, handler); This solution doesn't stop the interval, but I'll leave that as an exercise for the reader :). |
I'm seeing this uncatchable exception in other codebases. Am I correct in understanding that the program will crash if a client walks away while there is a writer locked onto the response stream? |
If you're using a We should definitely improve this error message to help point to the source of the error, however. Note that locking and unlocking the stream is a bit unnecessary -- you should be able to just get the writer once and keep writing to it. |
IIRC I didn't realize that the @mmastrac is right, you don't need to lock and unlock continually, although if others need the writer than you may need to do this. This isn't best design, but the point was that it appeared to be uncatchable, but in the end it wasn't (which is why I closed this). I have seen other cases of uncatchable exceptions though (mostly around web sockets) but that was 2+ years ago and I think they have been resolved now. |
Ok, I was able to track down and fix this uncaught exception within my own codebase ( |
Version: Deno 1.41.3
In a request handler, I created a stream to write data back to the response. If the client closes the connection, the next time the writer is access, an exception is thrown and server crashes. Unfortunately I can't find any way to catch the exception as it's likely happening under the covers. Even if I add a close listener to the writer, it will throw.
If I then try to reach this Endpoint via something like curl:
It will return the streamed data. If I kill the curl process (ctr+c), the stream is closed and the server dies with the following:
Any thoughts on how to catch this exception. It happens on a Mac, I haven't tested it on other platforms.
The text was updated successfully, but these errors were encountered: