-
-
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
Open
RajeshKumar11
wants to merge
2
commits into
nodejs:main
Choose a base branch
from
RajeshKumar11:fix/http-client-socket-error-handler
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
http: attach error handler to socket synchronously in onSocket #61770
RajeshKumar11
wants to merge
2
commits into
nodejs:main
from
RajeshKumar11:fix/http-client-socket-error-handler
+27
−20
Conversation
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
Collaborator
|
Review requested:
|
Member
|
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.73% -0.01%
==========================================
Files 675 675
Lines 204648 204655 +7
Branches 39330 39327 -3
==========================================
+ Hits 183651 183657 +6
- Misses 13282 13285 +3
+ Partials 7715 7713 -2
🚀 New features to boost your workflow:
|
3f0a317 to
cc09fb0
Compare
Contributor
Author
cc09fb0 to
ab1a46b
Compare
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. tickOnSocket removes and re-adds the listener after the parser is set up to avoid duplicates. 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.
ab1a46b to
341f0f3
Compare
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
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