-
-
Notifications
You must be signed in to change notification settings - Fork 31.7k
async_hooks: don't reuse resource in HttpAgent when queued #34439
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
Closed
puzpuzpuz
wants to merge
1
commit into
nodejs:master
from
puzpuzpuz:defect/async-resource-reused-in-http-agent
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 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
'use strict'; | ||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const { AsyncLocalStorage } = require('async_hooks'); | ||
const http = require('http'); | ||
|
||
const asyncLocalStorage = new AsyncLocalStorage(); | ||
|
||
const agent = new http.Agent({ | ||
maxSockets: 1 | ||
}); | ||
|
||
const N = 3; | ||
let responses = 0; | ||
|
||
const server = http.createServer(common.mustCall((req, res) => { | ||
res.end('ok'); | ||
}, N)); | ||
|
||
server.listen(0, common.mustCall(() => { | ||
const port = server.address().port; | ||
|
||
for (let i = 0; i < N; i++) { | ||
asyncLocalStorage.run(i, () => { | ||
http.get({ agent, port }, common.mustCall((res) => { | ||
assert.strictEqual(asyncLocalStorage.getStore(), i); | ||
if (++responses === N) { | ||
server.close(); | ||
agent.destroy(); | ||
} | ||
res.resume(); | ||
})); | ||
}); | ||
} | ||
})); |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
'use strict'; | ||
// Flags: --expose-internals | ||
const common = require('../common'); | ||
const initHooks = require('./init-hooks'); | ||
const { checkInvocations } = require('./hook-checks'); | ||
const assert = require('assert'); | ||
const { async_id_symbol } = require('internal/async_hooks').symbols; | ||
const http = require('http'); | ||
|
||
// Checks that the async resource used in init in case of a reused handle | ||
// is not reused. Test is based on parallel\test-async-hooks-http-agent.js. | ||
|
||
const hooks = initHooks(); | ||
hooks.enable(); | ||
|
||
const reqAsyncIds = []; | ||
let socket; | ||
let responses = 0; | ||
|
||
// Make sure a single socket is transparently reused for 2 requests. | ||
const agent = new http.Agent({ | ||
keepAlive: true, | ||
keepAliveMsecs: Infinity, | ||
maxSockets: 1 | ||
}); | ||
|
||
const verifyRequest = (idx) => (res) => { | ||
reqAsyncIds[idx] = res.socket[async_id_symbol]; | ||
assert.ok(reqAsyncIds[idx] > 0, `${reqAsyncIds[idx]} > 0`); | ||
if (socket) { | ||
// Check that both requests share their socket. | ||
assert.strictEqual(res.socket, socket); | ||
} else { | ||
socket = res.socket; | ||
} | ||
|
||
res.on('data', common.mustCallAtLeast(() => {})); | ||
res.on('end', common.mustCall(() => { | ||
if (++responses === 2) { | ||
// Clean up to let the event loop stop. | ||
server.close(); | ||
agent.destroy(); | ||
} | ||
})); | ||
}; | ||
|
||
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. | ||
const r1 = http.request({ | ||
agent, port, method: 'POST' | ||
}, common.mustCall(verifyRequest(0))); | ||
r1.end(payload); | ||
|
||
// Second request. Sent in parallel with the first one. | ||
const r2 = http.request({ | ||
agent, port, method: 'POST' | ||
}, common.mustCall(verifyRequest(1))); | ||
r2.end(payload); | ||
})); | ||
|
||
|
||
process.on('exit', onExit); | ||
|
||
function onExit() { | ||
hooks.disable(); | ||
hooks.sanityCheck(); | ||
const activities = hooks.activities; | ||
|
||
// Verify both invocations | ||
const first = activities.filter((x) => x.uid === reqAsyncIds[0])[0]; | ||
checkInvocations(first, { init: 1, destroy: 1 }, 'when process exits'); | ||
|
||
const second = activities.filter((x) => x.uid === reqAsyncIds[1])[0]; | ||
checkInvocations(second, { init: 1, destroy: 1 }, 'when process exits'); | ||
|
||
// Verify reuse handle has been wrapped | ||
assert.strictEqual(first.type, second.type); | ||
assert.ok(first.handle !== second.handle, 'Resource reused'); | ||
assert.ok(first.handle === second.handle.handle, | ||
'Resource not wrapped correctly'); | ||
} |
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
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.
Uh oh!
There was an error while loading. Please reload this page.