Skip to content
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

Merged
merged 3 commits into from
Dec 29, 2023
Merged

Conversation

lilnasy
Copy link
Contributor

@lilnasy lilnasy commented Dec 27, 2023

Changes

  • Fix for the server crashing on a promise rejection within the stream.

Testing

  • node packages/integrations/node/test/fixtures/errors/dist/server/entry.mjs
  • curl -i localhost:4321/in-stream

Docs

Does not affect usage

Copy link

changeset-bot bot commented Dec 27, 2023

🦋 Changeset detected

Latest 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

@github-actions github-actions bot added the pkg: integration Related to any renderer integration (scope) label Dec 27, 2023
Comment on lines -82 to +90
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);
});
Copy link
Contributor Author

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

@github-actions github-actions bot added the pr: docs A PR that includes documentation for review label Dec 27, 2023
Comment on lines 97 to 98
} catch (err: any) {
console.error(err?.stack || err?.message || String(err));
res.write('Internal server error');
Copy link
Member

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?

Copy link
Contributor Author

@lilnasy lilnasy Dec 29, 2023

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.

Copy link
Member

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?

Copy link
Contributor Author

@lilnasy lilnasy Dec 29, 2023

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?

Copy link
Member

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)

@lilnasy lilnasy merged commit 48f47b5 into withastro:main Dec 29, 2023
13 checks passed
@lilnasy lilnasy deleted the node-fix branch December 29, 2023 15:47
@astrobot-houston astrobot-houston mentioned this pull request Dec 29, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: integration Related to any renderer integration (scope) pr: docs A PR that includes documentation for review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants