-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
http: reduce likelihood of race conditions on keep-alive timeout
Fixes: #52649 Refs: #54293 Co-authored-by: Arrigo Zanette <[email protected]>
- Loading branch information
Showing
3 changed files
with
57 additions
and
5 deletions.
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
38 changes: 38 additions & 0 deletions
38
test/parallel/test-http-keep-alive-timeout-race-condition.js
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,38 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const http = require('http'); | ||
|
||
const makeRequest = (port, agent) => | ||
new Promise((resolve, reject) => { | ||
const req = http.get( | ||
{ path: '/', port, agent }, | ||
(res) => { | ||
res.resume(); | ||
res.on('end', () => resolve()); | ||
}, | ||
); | ||
req.on('error', (e) => reject(e)); | ||
req.end(); | ||
}); | ||
|
||
const server = http.createServer( | ||
{ keepAliveTimeout: common.platformTimeout(2000), keepAlive: true }, | ||
common.mustCall((req, res) => { | ||
const body = 'hello world\n'; | ||
res.writeHead(200, { 'Content-Length': body.length }); | ||
res.write(body); | ||
res.end(); | ||
}, 2) | ||
); | ||
|
||
const agent = new http.Agent({ maxSockets: 5, keepAlive: true }); | ||
|
||
server.listen(0, common.mustCall(async function() { | ||
await makeRequest(this.address().port, agent); | ||
// Block the event loop for 2 seconds | ||
Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, 2000); | ||
await makeRequest(this.address().port, agent); | ||
server.close(); | ||
agent.destroy(); | ||
})); |