-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
Cannot call write after a stream was destroyed #69
Comments
Hmm, I thought that the close listener above the loop should suffice, but apparently not. graphql-sse/src/use/express.ts Lines 96 to 101 in f33ccc2
I am trying to write a failing test now and then I'll proceed with fixing the bug. Thanks for reporting! |
I am struggling a bit with the failing test, can you share more details on when this happens? Or even better, a repro? |
@michelalbers any luck with the repro? |
Unable to reproduce. Added some tests too. |
Same error:
|
A repro would be very helpful as I am unable to create one myself. |
I'm unable to reproduce it every time as well, but the following code seems unsafe in certain situation:
Error: Cannot call write after a stream was destroyed It looks like the response stream/object was destroyed (for whatever reason), but the promise callback is not canceled. Also, I put the subscription handler in a try/catch block, as the docs specifies, but the node process crash with an unhandled exception :
|
The write loop should break as soon as the connection closes (see #69 (comment)). This line takes care of it: graphql-sse/src/use/express.ts Line 96 in 256b590
I am suspicious that |
Lets assume express actually is late in reporting the closed connection event. Isn't that enough reason to check the |
Also I suppose that setting a handler on a 'close' event put it on a Macrotask queue (Node Event Loop), while setting a promise callback put it on a Microtask queue. Because of the fact that Microtask queue is consulted more frequently by design, we can meet a situation when the "for loop" handler is called before the "once" one. |
Both of your points make perfect sense @groundmuffin, I'll make the adjustments. Thanks! |
🎉 This issue has been resolved in version 2.2.2 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@michelalbers, @groundmuffin the v2.2.2 includes a fix, would be great if you can test it out and report if the issue persists. Thank you for reporting and advising! |
It's still happening
|
Wow, then the |
…tead of `closed` before writing to response Closes #69
## [2.2.3](v2.2.2...v2.2.3) (2023-08-23) ### Bug Fixes * **use/http,use/http2,use/express,use/fastify:** Check `writable` instead of `closed` before writing to response ([3c71f69](3c71f69)), closes [#69](#69)
🎉 This issue has been resolved in version 2.2.3 🎉 The release is available on: Your semantic-release bot 📦🚀 |
@groundmuffin, @michelalbers can you try out v2.2.3? I use the |
Screenshot
Expected Behaviour
Do not write to the stream after it is closed / destroyed
Actual Behaviour
Writes to stream after it was destroyed which results in an error.
Debug Information
This bug resulted when using the
use/express
handler created with acreateHandler
call. According to the code there is no check in place wether the stream was closed in the meantime.Further Information
Maybe introduce a variable
let cancelled = false;
and set it to true once the stream was closed. Check forcancelled
before trying to do a.write()
.The text was updated successfully, but these errors were encountered: