-
-
Notifications
You must be signed in to change notification settings - Fork 34.7k
http: attach error handler to socket synchronously in onSocket #61770
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?
http: attach error handler to socket synchronously in onSocket #61770
Conversation
|
Review requested:
|
|
Can you revert the other change as part of this PR? |
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #61770 +/- ##
==========================================
+ Coverage 89.73% 89.76% +0.02%
==========================================
Files 675 675
Lines 204648 204674 +26
Branches 39330 39339 +9
==========================================
+ Hits 183651 183720 +69
+ Misses 13282 13231 -51
- Partials 7715 7723 +8
🚀 New features to boost your workflow:
|
3f0a317 to
cc09fb0
Compare
ab1a46b to
341f0f3
Compare
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.
Looks good in general, I think there's just a couple of changes we should make before we merge this to clear things up a bit.
lib/_http_client.js
Outdated
| // Use socket directly as req.socket may not be assigned yet (e.g. when | ||
| // the error is emitted before onSocketNT runs). | ||
| (req.socket || socket)._hadError = true; |
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.
I think we could simplify this to just
| // Use socket directly as req.socket may not be assigned yet (e.g. when | |
| // the error is emitted before onSocketNT runs). | |
| (req.socket || socket)._hadError = true; | |
| socket._hadError = true; |
I've just had a look, and everywhere that socket._httpMessage is set to a request, req.socket is set at the same time, so if req is set they're always equivalent, and setting this on the socket in the socket error handler makes perfect sense to me conceptually.
(It looks like they could diverge if req were null - that logic is less clear - but that doesn't matter within this if)
lib/_http_client.js
Outdated
| // Remove the early error listener attached in onSocket (if any) before | ||
| // re-adding it here to avoid duplicate listeners. | ||
| socket.removeListener('error', socketErrorListener); | ||
| socket.on('error', socketErrorListener); |
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.
I think we can drop both these lines these entirely:
- This method (
tickOnSocket) is only called fromonSocketNT onSocketNTis only called fromonSocket- This socket error handler will always have been set exactly once already, so this doesn't really do anything.
Anything I'm missing? This would make sense if the handlers were different, or maybe if there were other code paths through here, but I don't see any.
Between onSocket and onSocketNT, the socket had no error handler, meaning any errors emitted during that window (e.g. from a blocklist check or custom lookup) would be unhandled even if the user had set up a request error handler. Fix this by attaching socketErrorListener synchronously in onSocket, setting socket._httpMessage so the listener can forward errors to the request. The _destroy path in onSocketNT is also guarded to prevent double-firing if socketErrorListener already emitted the error. Fixes: nodejs#48771 Refs: nodejs#61658
This reverts commit d8c00ad.
341f0f3 to
7d78891
Compare
|
Thanks @pimterry for the review! Both suggestions applied:
Updated the commit message as well to reflect the simpler approach. |
Summary
Fixes the underlying issue identified in #61658 (already merged) where socket errors could be unhandled even when a user had set up a request error handler.
Root Cause
Between
onSocketandonSocketNT, the socket had no error handler. If an error was emitted during this window (e.g. from a blocklist check or custom lookup function), it would be unhandled even if the user hadreq.on('error', ...)set up.This happened because:
createConnectionreturns the socket synchronouslyprocess.nextTick(ininternal/streams/destroy)onSocketNTis also deferred viaprocess.nextTick(to set upsocketErrorListener)nextTickfires beforeonSocketNT'snextTick, so no handler is registeredFix
Attach
socketErrorListenersynchronously inonSocket, before theprocess.nextTick(onSocketNT, ...)call. This requires:socket._httpMessage = thisearly (needed bysocketErrorListenerto find the request)tickOnSocketto avoid duplicates_destroyinonSocketNTagainst double-firing ifsocketErrorListeneralready emitted the error(req.socket || socket)._hadErrorsincereq.socketmay not be assigned yet at early error timeTesting
test/parallel/test-http-request-lookup-error-catchable.js(from net: defer synchronous destroy calls in internalConnect #61658)test/parallel/test-http-*.jstests passRefs: #48771
Refs: #61658