-
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,async_hooks: keep resource object alive from socket #30196
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,98 @@ | ||
// Flags: --expose-gc --expose-internals | ||
'use strict'; | ||
const common = require('../common'); | ||
const http = require('http'); | ||
const async_hooks = require('async_hooks'); | ||
const makeDuplexPair = require('../common/duplexpair'); | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/30122 | ||
// When a domain is attached to an http Agent’s ReusedHandle object, that | ||
// domain should be kept alive through the ReusedHandle and that in turn | ||
// through the actual underlying handle. | ||
|
||
// Consistency check: There is a ReusedHandle being used, and it emits events. | ||
// We also use this async hook to manually trigger GC just before the domain’s | ||
// own `before` hook runs, in order to reproduce the bug above (the ReusedHandle | ||
// being collected and the domain with it while the handle is still alive). | ||
const checkInitCalled = common.mustCall(); | ||
const checkBeforeCalled = common.mustCallAtLeast(); | ||
let reusedHandleId; | ||
async_hooks.createHook({ | ||
init(id, type, triggerId, resource) { | ||
if (resource.constructor.name === 'ReusedHandle') { | ||
reusedHandleId = id; | ||
checkInitCalled(); | ||
} | ||
}, | ||
before(id) { | ||
if (id === reusedHandleId) { | ||
global.gc(); | ||
checkBeforeCalled(); | ||
} | ||
} | ||
}).enable(); | ||
|
||
// We use a DuplexPair rather than TLS sockets to keep the domain from being | ||
// attached to too many objects that use strong references (timers, the network | ||
// socket handle, etc.) and wrap the client side in a JSStreamSocket so we don’t | ||
// have to implement the whole _handle API ourselves. | ||
const { serverSide, clientSide } = makeDuplexPair(); | ||
const JSStreamSocket = require('internal/js_stream_socket'); | ||
const wrappedClientSide = new JSStreamSocket(clientSide); | ||
|
||
// Consistency check: We use asyncReset exactly once. | ||
wrappedClientSide._handle.asyncReset = | ||
common.mustCall(wrappedClientSide._handle.asyncReset); | ||
|
||
// Dummy server implementation, could be any server for this test... | ||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.writeHead(200, { | ||
'Content-Type': 'text/plain' | ||
}); | ||
res.end('Hello, world!'); | ||
}, 2)); | ||
server.emit('connection', serverSide); | ||
|
||
// HTTP Agent that only returns the fake connection. | ||
class TestAgent extends http.Agent { | ||
createConnection = common.mustCall(() => wrappedClientSide) | ||
} | ||
const agent = new TestAgent({ keepAlive: true, maxSockets: 1 }); | ||
|
||
function makeRequest(cb) { | ||
const req = http.request({ agent }, common.mustCall((res) => { | ||
res.resume(); | ||
res.on('end', cb); | ||
})); | ||
req.end(''); | ||
} | ||
|
||
// The actual test starts here: | ||
|
||
const domain = require('domain'); | ||
// Create the domain in question and a dummy “noDomain” domain that we use to | ||
// avoid attaching new async resources to the original domain. | ||
const d = domain.create(); | ||
const noDomain = domain.create(); | ||
|
||
d.run(common.mustCall(() => { | ||
// Create a first request only so that we can get a “re-used” socket later. | ||
makeRequest(common.mustCall(() => { | ||
// Schedule the second request. | ||
setImmediate(common.mustCall(() => { | ||
makeRequest(common.mustCall(() => { | ||
// The `setImmediate()` is run inside of `noDomain` so that it doesn’t | ||
// keep the actual target domain alive unnecessarily. | ||
noDomain.run(common.mustCall(() => { | ||
setImmediate(common.mustCall(() => { | ||
// This emits an async event on the reused socket, so it should | ||
// run the domain’s `before` hooks. | ||
// This should *not* throw an error because the domain was garbage | ||
// collected too early. | ||
serverSide.end(); | ||
})); | ||
})); | ||
})); | ||
})); | ||
})); | ||
})); |
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.
Is it ok that on second reuse of the
handle
the binding to the firstReusedHandle
is removed?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.
@Flarna That only happens together with a
destroy
emitted before and a new ID assigned, so I’m pretty confident it’s safe to discard the firstReusedHandle
at that point