-
-
Notifications
You must be signed in to change notification settings - Fork 689
fix: catch synchronous errors in request callbacks #4443
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
Conversation
When a synchronous error is thrown in a request callback after the underlying stream is destroyed, the error was being swallowed and becoming an uncaught exception. This fix adds proper error handling in RequestHandler.onHeaders() to catch these errors, destroy the response stream cleanly, and re-throw the error to be handled by the existing error handling chain. Fixes the case where user code throws synchronously in request callbacks when streams are already destroyed, ensuring errors are properly propagated through the abort mechanism. 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
|
Do we need to check all occurences of runInAsyncScope in regard of being wrapped in try catch blocks? |
|
That might be the case. |
metcoder95
left a comment
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.
Shall we backport it to v6?
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
|
I think this has a bug still, I need to look at it again. |
|
Fixed the linting |
…andler When a synchronous error is thrown in a request callback, it should propagate to the global uncaughtException handler rather than being caught and causing the process to abort. This change uses process.nextTick() to re-throw the error, allowing it to reach the uncaughtException handler as expected. - Modified error handling in RequestHandler.onHeaders() to use nextTick - Updated tests to verify errors reach uncaughtException handler - Fixed flaky stats test expectation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <noreply@anthropic.com> Signed-off-by: Matteo Collina <hello@matteocollina.com>
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
|
@mcollina |
|
Yes |
Uzlopak
left a comment
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.
LGTM
Combine res.on('error', noop) call directly into util.destroy
for cleaner code structure.
Uzlopak
left a comment
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.
LGTM
Ethan-Arrowood
left a comment
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.
tests look good. nice fix
Summary
Rationale
Previously, when a synchronous error was thrown in a request callback after the underlying stream was destroyed, the error would be swallowed and become an uncaught exception. This happened because the
RequestHandler.onHeaders()method called user callbacks viarunInAsyncScope()without wrapping it in a try-catch block.Changes
Bug Fixes
RequestHandler.onHeaders()Request.onHeaders()Implementation Details
The fix ensures that:
Request.onHeaders→this.abort(err))Test Plan
🤖 Generated with Claude Code