-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
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
fix(node): prevent crash on stream error #9533
Conversation
🦋 Changeset detectedLatest commit: 9d64ede The changes in this PR will be included in the next version bump. 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 |
reader.cancel(); | ||
// Cancelling the reader may reject not just because of | ||
// an error in the ReadableStream's cancel callback, but | ||
// also because of an error anywhere in the stream. | ||
reader.cancel().catch(err => { | ||
logger.error(`There was an uncaught error in the middle of the stream while rendering ${res.req.url}.`); | ||
console.error(err); | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the fix is here, the rest is clerical
} catch (err: any) { | ||
console.error(err?.stack || err?.message || String(err)); | ||
res.write('Internal server error'); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This could now potentially swallow errors without logging. Should keep the logging here as before?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
That would end up logging twice, because stream errors are also thrown on cancelling the reader.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I guess I was worried that res.on('close'
isn't always guaranteed to be called, which means reader.cancel()
may not be called. So it feels safer to log it in this catch block instead. Maybe we can do reader.cancel().catch(() => {})
instead since we know the error should already be logged here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't res.end()
below this catch block guarantee that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True. In that case, maybe we should leave a comment here instead about why we're swallowing the error (also remove the (err: any)
part)
Changes
Testing
node packages/integrations/node/test/fixtures/errors/dist/server/entry.mjs
curl -i localhost:4321/in-stream
Docs
Does not affect usage