node/test/parallel/test-http-agent-timeout-option.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

24 lines
633 B
JavaScript

'use strict';
const { mustCall } = require('../common');
const { strictEqual } = require('assert');
const { Agent, get } = require('http');
// Test that the listener that forwards the `'timeout'` event from the socket to
// the `ClientRequest` instance is added to the socket when the `timeout` option
// of the `Agent` is set.
const request = get({
agent: new Agent({ timeout: 50 }),
lookup: () => {}
});
request.on('socket', mustCall((socket) => {
strictEqual(socket.timeout, 50);
const listeners = socket.listeners('timeout');
strictEqual(listeners.length, 2);
strictEqual(listeners[1], request.timeoutCb);
}));