mirror of
https://github.com/nodejs/node.git
synced 2025-04-30 23:56:58 +00:00

Whether and when a socket is destroyed or not after a timeout is up to the user. This leaves an edge case where a socket that has emitted 'timeout' might be re-used from the free pool. Even if destroy is called on the socket, it won't be removed from the freelist until 'close' which can happen several ticks later. Sockets are removed from the free list on the 'close' event. However, there is a delay between calling destroy() and 'close' being emitted. This means that it possible for a socket that has been destroyed to be re-used from the free list, causing unexpected failures. PR-URL: https://github.com/nodejs/node/pull/32000 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
48 lines
1.2 KiB
JavaScript
48 lines
1.2 KiB
JavaScript
'use strict';
|
|
const common = require('../common');
|
|
const http = require('http');
|
|
const assert = require('assert');
|
|
|
|
const agent = new http.Agent({ keepAlive: true });
|
|
|
|
const server = http.createServer((req, res) => {
|
|
res.end('');
|
|
});
|
|
|
|
// Maximum allowed value for timeouts
|
|
const timeout = 2 ** 31 - 1;
|
|
|
|
const options = {
|
|
agent,
|
|
method: 'GET',
|
|
port: undefined,
|
|
host: common.localhostIPv4,
|
|
path: '/',
|
|
timeout: timeout
|
|
};
|
|
|
|
server.listen(0, options.host, common.mustCall(() => {
|
|
options.port = server.address().port;
|
|
doRequest(common.mustCall((numListeners) => {
|
|
assert.strictEqual(numListeners, 2);
|
|
doRequest(common.mustCall((numListeners) => {
|
|
assert.strictEqual(numListeners, 2);
|
|
server.close();
|
|
agent.destroy();
|
|
}));
|
|
}));
|
|
}));
|
|
|
|
function doRequest(cb) {
|
|
http.request(options, common.mustCall((response) => {
|
|
const sockets = agent.sockets[`${options.host}:${options.port}:`];
|
|
assert.strictEqual(sockets.length, 1);
|
|
const socket = sockets[0];
|
|
const numListeners = socket.listeners('timeout').length;
|
|
response.resume();
|
|
response.once('end', common.mustCall(() => {
|
|
process.nextTick(cb, numListeners);
|
|
}));
|
|
})).end();
|
|
}
|