Company logo
  • Empleos
  • Bootcamp
  • Acerca de nosotros
  • Para profesionales
    • Inicio
    • Empleos
    • Cursos y retos
    • Preguntas
    • Profesores
    • Bootcamp
  • Para empresas
    • Inicio
    • Nuestro proceso
    • Planes
    • Pruebas
    • Nómina
    • Blog
    • Comercial
    • Calculadora

0

75
Vistas
IncomingMessage,ServerResponse not garbage collected in keep-alive connections

In the last few days, I've investigated a memory leak issue regarding keep-alive HTTP/1.1 connections in NodeJS 14.17.3.

I have simplified the implementation of the API as shown below.

async function index(req, res) {
  res.writeHead(200, {
    'Content-Type': 'text/event-stream',
    'Cache-Control': 'no-cache',
    Connection: 'keep-alive',
    'Access-Control-Allow-Credentials': 'true',
    'Access-Control-Expose-Headers':
      'Accept,Accept-Encoding,Accept-Language,Cache-Control,Connection,Host,Origin,Pragma,Referer,Sec-Fetch-Dest,Sec-Fetch-Mode,Sec-Fetch-Site,Sec-GPC,User-Agent',
  });

  res.write('test');

  res.on('close', () => {
    res.end();
  });
}

The issue appears when the client interrupts the connection from his side. Although the 'close' event is fired and the callback is executed successfully, the objects remain in memory. Using the inspect tool of chrome, I took a picture and attached it below.

Memory Snapshot

What I expect is the highlighted ServerResponse object to be garbage collected after the response ends. Also, the 'finish' event is not fired in this case. I do not know if it is relevant or if it is a standard behavior when the request is interrupted by the client.

I use expressjs as the web framework and the reason the connections are 1.1 is that the API is for server-sent events.

7 months ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos