|
| 1 | +// Flags: --expose-gc |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +const common = require('../common'); |
| 5 | +if (!common.hasCrypto) |
| 6 | + common.skip('missing crypto'); |
| 7 | + |
| 8 | +// A stream that is reset because of an oversized header block keeps the |
| 9 | +// headers it had already accepted. If the stream is destroyed while one of |
| 10 | +// its writes is still on the socket, it outlives the session, and the session |
| 11 | +// destructor used to abort because that memory was still allocated. |
| 12 | + |
| 13 | +const http2 = require('http2'); |
| 14 | +const net = require('net'); |
| 15 | +const { setTimeout: sleep } = require('timers/promises'); |
| 16 | + |
| 17 | +// Enough response data to fill the socket buffers on loopback. |
| 18 | +const kStreams = 80; |
| 19 | + |
| 20 | +function frame(type, flags, id, payload = Buffer.alloc(0)) { |
| 21 | + const header = Buffer.alloc(9); |
| 22 | + header.writeUIntBE(payload.length, 0, 3); |
| 23 | + header[3] = type; |
| 24 | + header[4] = flags; |
| 25 | + header.writeUInt32BE(id, 5); |
| 26 | + return Buffer.concat([header, payload]); |
| 27 | +} |
| 28 | + |
| 29 | +// :method POST, :scheme http, :path /, :authority localhost |
| 30 | +const requestHeaders = Buffer.from([ |
| 31 | + 0x83, 0x86, 0x84, 0x01, 0x09, ...Buffer.from('localhost'), |
| 32 | +]); |
| 33 | + |
| 34 | +// More trailer fields than the default maxHeaderListPairs (128). |
| 35 | +const trailers = Buffer.concat(Array.from({ length: 200 }, (_, i) => { |
| 36 | + const name = `x-${i}`; |
| 37 | + return Buffer.from([0x00, name.length, ...Buffer.from(name), 0x01, 0x76]); |
| 38 | +})); |
| 39 | + |
| 40 | +const server = http2.createServer(common.mustCall((req, res) => { |
| 41 | + req.on('error', () => {}); |
| 42 | + res.on('error', () => {}); |
| 43 | + // Respond before the request has ended, with more data than the client |
| 44 | + // reads, so that the response stays in a pending socket write. |
| 45 | + res.end(Buffer.alloc(100 * 1024)); |
| 46 | +}, kStreams)); |
| 47 | + |
| 48 | +server.on('session', common.mustCall((session) => { |
| 49 | + session.on('error', () => {}); |
| 50 | + session.on('close', common.mustCall(async () => { |
| 51 | + server.close(); |
| 52 | + // Collect the session before the streams that are still referenced by |
| 53 | + // their pending writes. |
| 54 | + for (let i = 0; i < 10; i++) { |
| 55 | + globalThis.gc(); |
| 56 | + await sleep(10); |
| 57 | + } |
| 58 | + })); |
| 59 | +})); |
| 60 | + |
| 61 | +server.listen(0, common.mustCall(async () => { |
| 62 | + const client = net.connect(server.address().port); |
| 63 | + client.on('error', () => {}); |
| 64 | + client.pause(); |
| 65 | + |
| 66 | + const settings = Buffer.from([0x00, 0x04, 0x7f, 0xff, 0xff, 0xff]); |
| 67 | + const windowUpdate = Buffer.alloc(4); |
| 68 | + windowUpdate.writeUInt32BE(0x7fffffff - 65535); |
| 69 | + const requests = []; |
| 70 | + for (let i = 0; i < kStreams; i++) |
| 71 | + requests.push(frame(1, 0x4, 1 + 2 * i, requestHeaders)); |
| 72 | + client.write(Buffer.concat([ |
| 73 | + Buffer.from('PRI * HTTP/2.0\r\n\r\nSM\r\n\r\n'), |
| 74 | + frame(4, 0, 0, settings), |
| 75 | + frame(8, 0, 0, windowUpdate), |
| 76 | + ...requests, |
| 77 | + ])); |
| 78 | + await sleep(common.platformTimeout(500)); |
| 79 | + |
| 80 | + const blocks = []; |
| 81 | + for (let i = 0; i < kStreams; i++) |
| 82 | + blocks.push(frame(1, 0x5, 1 + 2 * i, trailers)); |
| 83 | + client.write(Buffer.concat(blocks)); |
| 84 | + await sleep(common.platformTimeout(200)); |
| 85 | + |
| 86 | + client.resetAndDestroy(); |
| 87 | +})); |