Skip to content

Commit bd23218

Browse files
authored
node: fix test-http-server-keepalive-req-gc.js (#21333)
1 parent 87df752 commit bd23218

File tree

1 file changed

+41
-0
lines changed

1 file changed

+41
-0
lines changed
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
// Flags: --expose-gc
2+
'use strict';
3+
const common = require('../common');
4+
const { onGC } = require('../common/gc');
5+
const { createServer } = require('http');
6+
const { connect } = require('net');
7+
8+
// Make sure that for HTTP keepalive requests, the req object can be
9+
// garbage collected once the request is finished.
10+
// Refs: https://github.com/nodejs/node/issues/9668
11+
12+
let client;
13+
const server = createServer(common.mustCall((req, res) => {
14+
onGC(req, { ongc: common.mustCall(() => { server.close(); }) });
15+
req.resume();
16+
req.on('end', common.mustCall(() => {
17+
setImmediate(async () => {
18+
client.end();
19+
await globalThis.gc({ type: 'major', execution: 'async' });
20+
await globalThis.gc({ type: 'major', execution: 'async' });
21+
});
22+
}));
23+
res.end('hello world');
24+
}));
25+
26+
server.listen(0, common.mustCall(() => {
27+
client = connect(server.address().port);
28+
29+
const req = [
30+
'POST / HTTP/1.1',
31+
`Host: localhost:${server.address().port}`,
32+
'Connection: keep-alive',
33+
'Content-Length: 11',
34+
'',
35+
'hello world',
36+
'',
37+
].join('\r\n');
38+
39+
client.write(req);
40+
client.unref();
41+
}));

0 commit comments

Comments
 (0)