|
| 1 | +'use strict'; |
| 2 | + |
| 3 | +// Regression test: when a pooled socket's writableHighWaterMark differs from |
| 4 | +// the new request's highWaterMark, the agent must not reuse that socket. |
| 5 | +// Reusing it causes the user's backpressure threshold to be silently ignored |
| 6 | +// (and can deadlock under TCP backpressure conditions). |
| 7 | +// |
| 8 | +// See: https://github.com/nodejs/node/pull/64653#issuecomment-5047543911 |
| 9 | + |
| 10 | +const common = require('../common'); |
| 11 | +const assert = require('assert'); |
| 12 | +const http = require('http'); |
| 13 | + |
| 14 | +const server = http.createServer(common.mustCall((req, res) => { |
| 15 | + req.resume(); |
| 16 | + req.on('end', () => res.end('ok')); |
| 17 | +}, 3)); |
| 18 | + |
| 19 | +server.listen(0, common.mustCall(() => { |
| 20 | + const port = server.address().port; |
| 21 | + const agent = new http.Agent({ keepAlive: true }); |
| 22 | + |
| 23 | + // Request A: creates socket with HWM=1MB. |
| 24 | + http.request({ |
| 25 | + host: 'localhost', port, method: 'POST', agent, |
| 26 | + highWaterMark: 1024 * 1024, |
| 27 | + }, common.mustCall((res) => { |
| 28 | + res.resume(); |
| 29 | + res.on('end', common.mustCall(() => { |
| 30 | + // Wait for socket to return to pool. |
| 31 | + setTimeout(requestB, 100); |
| 32 | + })); |
| 33 | + })).end('x'); |
| 34 | + |
| 35 | + function requestB() { |
| 36 | + const freeCount = Object.values(agent.freeSockets).flat().length; |
| 37 | + assert.strictEqual(freeCount, 1); |
| 38 | + |
| 39 | + // Request B: HWM=10KB — must get a fresh socket, not the 1MB one. |
| 40 | + const reqB = http.request({ |
| 41 | + host: 'localhost', port, method: 'POST', agent, |
| 42 | + highWaterMark: 10 * 1024, |
| 43 | + }, common.mustCall((res) => { |
| 44 | + res.resume(); |
| 45 | + res.on('end', common.mustCall(() => { |
| 46 | + setTimeout(requestC, 100); |
| 47 | + })); |
| 48 | + })); |
| 49 | + |
| 50 | + reqB.on('socket', common.mustCall((socket) => { |
| 51 | + assert.strictEqual(socket.writableHighWaterMark, 10 * 1024); |
| 52 | + })); |
| 53 | + |
| 54 | + reqB.end('y'); |
| 55 | + } |
| 56 | + |
| 57 | + function requestC() { |
| 58 | + // Request C: same HWM=10KB — should reuse the socket from Request B. |
| 59 | + const reqC = http.request({ |
| 60 | + host: 'localhost', port, method: 'POST', agent, |
| 61 | + highWaterMark: 10 * 1024, |
| 62 | + }, common.mustCall((res) => { |
| 63 | + res.resume(); |
| 64 | + res.on('end', common.mustCall(() => { |
| 65 | + server.close(); |
| 66 | + })); |
| 67 | + })); |
| 68 | + |
| 69 | + reqC.on('socket', common.mustCall((socket) => { |
| 70 | + assert.strictEqual(socket.writableHighWaterMark, 10 * 1024); |
| 71 | + })); |
| 72 | + |
| 73 | + reqC.end('z'); |
| 74 | + } |
| 75 | +})); |
0 commit comments