fix: catch synchronous errors in request callbacks#4443
Merged
metcoder95 merged 6 commits intomainfrom Aug 26, 2025
Merged
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>
Contributor
|
Do we need to check all occurences of runInAsyncScope in regard of being wrapped in try catch blocks? |
Member
Author
|
That might be the case. |
Uzlopak
reviewed
Aug 21, 2025
metcoder95
approved these changes
Aug 22, 2025
Member
metcoder95
left a comment
There was a problem hiding this comment.
Shall we backport it to v6?
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
Member
Author
|
I think this has a bug still, I need to look at it again. |
Uzlopak
reviewed
Aug 22, 2025
Uzlopak
reviewed
Aug 22, 2025
Uzlopak
reviewed
Aug 22, 2025
Contributor
|
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>
Uzlopak
reviewed
Aug 24, 2025
Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
Contributor
|
@mcollina |
Member
Author
|
Yes |
Combine res.on('error', noop) call directly into util.destroy
for cleaner code structure.
Ethan-Arrowood
approved these changes
Aug 25, 2025
Collaborator
Ethan-Arrowood
left a comment
There was a problem hiding this comment.
tests look good. nice fix
Merged
slagiewka
pushed a commit
to slagiewka/undici
that referenced
this pull request
Feb 14, 2026
Co-authored-by: Claude <noreply@anthropic.com> Co-authored-by: Aras Abbasi <aras.abbasi@googlemail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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