-
-
Notifications
You must be signed in to change notification settings - Fork 34.6k
net: defer synchronous destroy calls in internalConnect #61658
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
base: main
Are you sure you want to change the base?
net: defer synchronous destroy calls in internalConnect #61658
Conversation
Defer socket.destroy() calls in internalConnect and internalConnectMultiple to the next tick. This ensures that error handlers have a chance to be set up before errors are emitted, particularly important when using http.request with a custom lookup function that returns synchronously. Previously, if a synchronous lookup function returned an IP that triggered an immediate error (e.g., via blockList), the error would be emitted before the HTTP client had set up its error handler (which happens via process.nextTick in onSocket). This caused unhandled 'error' events. Fixes: nodejs#48771
|
Review requested:
|
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61658 +/- ##
==========================================
- Coverage 89.74% 89.72% -0.03%
==========================================
Files 674 674
Lines 204360 204368 +8
Branches 39265 39270 +5
==========================================
- Hits 183412 183375 -37
- Misses 13251 13303 +52
+ Partials 7697 7690 -7
🚀 New features to boost your workflow:
|
pimterry
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.
This looks good to me, nice fix, thanks @RajeshKumar11! 👍
jazelly
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.
nice fix! lgtm
Defer socket.destroy() calls in internalConnect and internalConnectMultiple to the next tick. This ensures that error handlers have a chance to be set up before errors are emitted, particularly important when using http.request with a custom lookup function that returns synchronously.
Problem
When using
http.request()with a custom synchronous lookup function that returns an IP triggering an immediate error (e.g., via blockList), the error was emitted before the HTTP client had set up its error handler. The HTTP client sets up the error handler viaprocess.nextTickinonSocket(), but the connection error occurred synchronously duringnet.createConnection().This caused unhandled 'error' events that could not be caught:
Solution
Defer
socket.destroy()calls to the next tick in:internalConnect()- bind errors, blockList errors, and connect errorsinternalConnectMultiple()- timeout and aggregate errorsThis gives callers (especially the HTTP client) time to set up error handlers before errors are emitted.
Testing
Added
test/parallel/test-http-request-lookup-error-catchable.jsthat verifies errors are catchable when using http.request with a synchronous custom lookup and blockList.Fixes: #48771
Refs: #51038