0
0
mirror of https://github.com/nodejs/node.git synced 2024-11-21 21:19:50 +01:00
nodejs/test/parallel/test-http-client-set-timeout-after-end.js
Robert Nagy 8700d89306 http: fix socket re-use races
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>
2020-03-07 20:51:12 +01:00

34 lines
737 B
JavaScript

'use strict';
// Test https://github.com/nodejs/node/issues/25499 fix.
const { mustCall } = require('../common');
const { Agent, createServer, get } = require('http');
const { strictEqual } = require('assert');
const server = createServer(mustCall((req, res) => {
res.end();
}));
server.listen(0, () => {
const agent = new Agent({ keepAlive: true, maxSockets: 1 });
const port = server.address().port;
let socket;
const req = get({ agent, port }, (res) => {
res.on('end', () => {
strictEqual(req.setTimeout(0), req);
strictEqual(socket.listenerCount('timeout'), 1);
agent.destroy();
server.close();
});
res.resume();
});
req.on('socket', (sock) => {
socket = sock;
});
});