net: defer synchronous destroy calls in internalConnect#61658
Open
RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
Open
net: defer synchronous destroy calls in internalConnect#61658RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
RajeshKumar11 wants to merge 1 commit intonodejs:mainfrom
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
Collaborator
|
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
approved these changes
Feb 3, 2026
Member
pimterry
left a comment
There was a problem hiding this comment.
This looks good to me, nice fix, thanks @RajeshKumar11! 👍
Collaborator
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.
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