Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

242
Visualizações
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.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda