-
Notifications
You must be signed in to change notification settings - Fork 29.7k
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
http: set async_id_symbol to any socket set #14389
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { OutgoingMessage } = require('http'); | ||
const { Writable } = require('stream'); | ||
const assert = require('assert'); | ||
|
||
// check that OutgoingMessage can be used without a proper Socket | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you link to the issues in the test files? I think that would be helpful for people looking at these tests at a later point. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
// Fixes: https://github.com/nodejs/node/issues/14386 | ||
// Fixes: https://github.com/nodejs/node/issues/14381 | ||
|
||
class Response extends OutgoingMessage { | ||
constructor() { | ||
super({ method: 'GET', httpVersionMajor: 1, httpVersionMinor: 1 }); | ||
} | ||
|
||
_implicitHeader() {} | ||
} | ||
|
||
const res = new Response(); | ||
const ws = new Writable({ | ||
write: common.mustCall((chunk, encoding, callback) => { | ||
assert(chunk.toString().match(/hello world/)); | ||
setImmediate(callback); | ||
}) | ||
}); | ||
|
||
res.socket = ws; | ||
ws._httpMessage = res; | ||
res.connection = ws; | ||
|
||
res.end('hello world'); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const { ServerResponse } = require('http'); | ||
const { Writable } = require('stream'); | ||
const assert = require('assert'); | ||
|
||
// check that ServerResponse can be used without a proper Socket | ||
// Fixes: https://github.com/nodejs/node/issues/14386 | ||
// Fixes: https://github.com/nodejs/node/issues/14381 | ||
|
||
const res = new ServerResponse({ | ||
method: 'GET', | ||
httpVersionMajor: 1, | ||
httpVersionMinor: 1 | ||
}); | ||
|
||
const ws = new Writable({ | ||
write: common.mustCall((chunk, encoding, callback) => { | ||
assert(chunk.toString().match(/hello world/)); | ||
setImmediate(callback); | ||
}) | ||
}); | ||
|
||
res.assignSocket(ws); | ||
|
||
res.end('hello world'); |
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.
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.
So as we wrote in #14386 (comment)
This might not be enough. If the socket is being reused, then it might hold a
socket[async_id_symbol]
but it might be stale.[I'm not completely awake yet, but] you might want to test
socket[async_id_symbol] != socket._handle.getAsyncId()
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 that is a completely different problem, this is built to be a stop-gap fixes that avoid crashing node 8 everywhere during unit tests.
The line below sets the type to
'not a socket'
, which is probably not correct in that case.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 is an issue I've been wresting with for the last month while I was working on #14208. The problem with assigning an
async_id
to theOutgoingMessage
is becauseOutgoingMessage
isn't actually asynchronous in any way, so it will never emit abefore()
/after()
/destroy()
. So we're only assigning anasync_id
as a stop gap.There are two long term solutions that I could implement quickly:
Assign the
this[async_id_symbol] = newUid()
in theOutgoingMessage
constructor. Then use allow passing custom async_id to node::AsyncWrap::AsyncWrap #14208 to propagate that value to the socket when it's assigned. This will require detecting if thesocket
will call thedestroy()
callback, or if it needs to be queued to be called in the'finish'
event. For this to happen I'll need sign-off on said PR.I've also considered and experimented with assigning error codes to the
async_id_symbol
/trigger_id_symbol
that can be safely handled. In this case I'd assignUV_ENODATA
in the constructor since thesocket
hasn't been created yet, then theasync_id_symbol
can be assigned when thesocket
has been created. This has the drawback of breaking async call graphs, but is easier to implement and doesn't require the additional PR.I'm going to step out and clear my head. Give me feedback and I can get a fix posted.
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.
@trevnorris I really like 1, and I do not really understand 2. I would prefer releasing something asap as a stop-gap solution (maybe today or tomorrow), and work on a proper fix. #14208 seems a semver-minor change, but maybe I am misreading it.
Do you think that will fix #14368 as well?
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.
👍 on (2)
IMHO if we can placate all the assertion while
async_hooks
are disabled that would be best.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.
@refack I'll start working on (2). It's a quicker solution and should address a broader scope of issues.
@mcollina (1) is a more long term solution for more specific cases and will probably require more testing to make sure it's done correctly. I'll get on that once the referenced PR lands. (2) is a way to mitigate these errors and allow bad values to flow through without crashing the process while allowing users of
async_hooks
to know when bad values appear