• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1// Flags: --expose-gc
2'use strict';
3const common = require('../common');
4const onGC = require('../common/ongc');
5const { createServer } = require('http');
6const { 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
12let client;
13const 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(() => {
18      client.end();
19      global.gc();
20    });
21  }));
22  res.end('hello world');
23}));
24
25server.listen(0, common.mustCall(() => {
26  client = connect(server.address().port);
27
28  const req = [
29    'POST / HTTP/1.1',
30    `Host: localhost:${server.address().port}`,
31    'Connection: keep-alive',
32    'Content-Length: 11',
33    '',
34    'hello world',
35    '',
36  ].join('\r\n');
37
38  client.write(req);
39  client.unref();
40}));
41