• Empleos
  • Sobre nosotros
  • Empleos
    • Inicio
    • Empleos
    • Cursos y retos
  • Empresas
    • Inicio
    • Publicar vacante
    • Nuestro proceso
    • Precios
    • Evaluaciones
    • Nómina
    • Blog
    • Comercial
    • Calculadora de salario

0

180
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.

over 3 years ago · Juan Pablo Isaza
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Nuestro proceso Comercial
Legal
Términos y condiciones Política de privacidad
© 2025 PeakU Inc. All Rights Reserved.

Andres GPT

Recomiéndame algunas ofertas
Necesito ayuda