net: defer synchronous destroy calls in internalConnect#61658
net: defer synchronous destroy calls in internalConnect#61658nodejs-github-bot merged 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
|
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:
|
There was a problem hiding this comment.
This looks good to me, nice fix, thanks @RajeshKumar11! 👍
|
Landed in d8c00ad |
|
This is wrong and is just hiding an issue in the http client. destroy will already defer emitting error. |
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: #48771 PR-URL: #61658 Refs: #51038 Reviewed-By: Tim Perry <pimterry@gmail.com> Reviewed-By: Jason Zhang <xzha4350@gmail.com>
|
@ronag good point, nice catch. Helpfully you've also left a TODO documenting what I think is the real underlying issue inline as well 😃 Lines 948 to 949 in 0777dcc I think that is exactly what's happening here:
Checking the test behaviour in more detail, that makes sense: in this new test, @RajeshKumar11 do you want to take a look and open another PR to improve this fix? The issue is real and this fix does work for the given test, but it would be better to fix in underlying problem directly instead (cleaner, and will fix other bugs too, e.g. any other kinds of next-tick errors like from non-socket streams or similar used in createConnection). I would guess that we just want to pull the existing request error handler setup to earlier in the process, but it depends what happens in the tests when you do that. At a glance that looks safe but it needs checking and might run into issues, looks like there are some details about how the ordering of agent connections & events need to be handled maybe. Alternatively if there's a very good reason, we could add separate error handling there, just for this case specifically, and then remove it again when onSocketNT fires. Give it a go, feel free to reach out if you run into issues. |
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