-
Notifications
You must be signed in to change notification settings - Fork 29.6k
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
[v8.x backport] async_hooks: add missing async_hooks destroys in AsyncReset #23410
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
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
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
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
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
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,83 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const async_id_symbol = process.binding('async_wrap').async_id_symbol; | ||
const async_hooks = require('async_hooks'); | ||
const http = require('http'); | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/19859 | ||
// Checks that an http.Agent emits a destroy for the old asyncId before calling | ||
// asyncReset()s when reusing a socket handle. The setup is nearly identical to | ||
// parallel/test-async-hooks-http-agent (which focuses on the assertion that | ||
// a fresh asyncId is assigned to the net.Socket instance). | ||
|
||
const destroyedIds = new Set(); | ||
async_hooks.createHook({ | ||
destroy: common.mustCallAtLeast((asyncId) => { | ||
destroyedIds.add(asyncId); | ||
}, 1) | ||
}).enable(); | ||
|
||
// Make sure a single socket is transparently reused for 2 requests. | ||
const agent = new http.Agent({ | ||
keepAlive: true, | ||
keepAliveMsecs: Infinity, | ||
maxSockets: 1 | ||
}); | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
req.once('data', common.mustCallAtLeast(() => { | ||
res.writeHead(200, { 'Content-Type': 'text/plain' }); | ||
res.write('foo'); | ||
})); | ||
req.on('end', common.mustCall(() => { | ||
res.end('bar'); | ||
})); | ||
}, 2)).listen(0, common.mustCall(() => { | ||
const port = server.address().port; | ||
const payload = 'hello world'; | ||
|
||
// First request. This is useless except for adding a socket to the | ||
// agent’s pool for reuse. | ||
const r1 = http.request({ | ||
agent, port, method: 'POST' | ||
}, common.mustCall((res) => { | ||
// Remember which socket we used. | ||
const socket = res.socket; | ||
const asyncIdAtFirstRequest = socket[async_id_symbol]; | ||
assert.ok(asyncIdAtFirstRequest > 0, `${asyncIdAtFirstRequest} > 0`); | ||
// Check that request and response share their socket. | ||
assert.strictEqual(r1.socket, socket); | ||
|
||
res.on('data', common.mustCallAtLeast(() => {})); | ||
res.on('end', common.mustCall(() => { | ||
// setImmediate() to give the agent time to register the freed socket. | ||
setImmediate(common.mustCall(() => { | ||
// The socket is free for reuse now. | ||
assert.strictEqual(socket[async_id_symbol], -1); | ||
|
||
// second request: | ||
const r2 = http.request({ | ||
agent, port, method: 'POST' | ||
}, common.mustCall((res) => { | ||
assert.ok(destroyedIds.has(asyncIdAtFirstRequest)); | ||
|
||
// Empty payload, to hit the “right” code path. | ||
r2.end(''); | ||
|
||
res.on('data', common.mustCallAtLeast(() => {})); | ||
res.on('end', common.mustCall(() => { | ||
// Clean up to let the event loop stop. | ||
server.close(); | ||
agent.destroy(); | ||
})); | ||
})); | ||
|
||
// Schedule a payload to be written immediately, but do not end the | ||
// request just yet. | ||
r2.write(payload); | ||
})); | ||
})); | ||
})); | ||
r1.end(payload); | ||
})); |
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,61 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const Countdown = require('../common/countdown'); | ||
const assert = require('assert'); | ||
const async_hooks = require('async_hooks'); | ||
const http = require('http'); | ||
|
||
// Regression test for https://github.com/nodejs/node/issues/19859. | ||
// Checks that matching destroys are emitted when creating new/reusing old http | ||
// parser instances. | ||
|
||
const N = 50; | ||
const KEEP_ALIVE = 100; | ||
|
||
const createdIds = []; | ||
const destroyedIds = []; | ||
async_hooks.createHook({ | ||
init: common.mustCallAtLeast((asyncId, type) => { | ||
if (type === 'HTTPPARSER') { | ||
createdIds.push(asyncId); | ||
} | ||
}, N), | ||
destroy: (asyncId) => { | ||
destroyedIds.push(asyncId); | ||
} | ||
}).enable(); | ||
|
||
const server = http.createServer(function(req, res) { | ||
res.end('Hello'); | ||
}); | ||
|
||
const keepAliveAgent = new http.Agent({ | ||
keepAlive: true, | ||
keepAliveMsecs: KEEP_ALIVE, | ||
}); | ||
|
||
const countdown = new Countdown(N, () => { | ||
server.close(() => { | ||
// give the server sockets time to close (which will also free their | ||
// associated parser objects) after the server has been closed. | ||
setTimeout(() => { | ||
createdIds.forEach((createdAsyncId) => { | ||
assert.ok(destroyedIds.indexOf(createdAsyncId) >= 0); | ||
}); | ||
}, KEEP_ALIVE * 2); | ||
}); | ||
}); | ||
|
||
server.listen(0, function() { | ||
for (let i = 0; i < N; ++i) { | ||
(function makeRequest() { | ||
http.get({ | ||
port: server.address().port, | ||
agent: keepAliveAgent | ||
}, function(res) { | ||
countdown.dec(); | ||
res.resume(); | ||
}); | ||
})(); | ||
} | ||
}); |
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
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
Oops, something went wrong.
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.
This check is actually new for the v8.x branch but since I was adding a new param here anyway (
args[1]/isReused
) I figured it wouldn't hurt to copy the existing check frommaster
here.