Skip to content

memory leak:ctx.res.write triggering clientError will cause ServerResponse not to be released #44463

@hdepro

Description

@hdepro

Version

14.16.1

Platform

darwin

What steps will reproduce the bug?

Set the client's timeout to a relatively short time, then perform two write operations on the server, and at the same time allow a period of time between the two writes to ensure that the error occurs between the two writes, the problem can be reproduced. Moreover, if client.js is executed again, the ServerResponse that is not released in memory will be updated to a new one; if client.js is executed again, and the timeout time of client is set longer (20ms->2000ms), write will not be triggered When the error occurs, the old ServerResponse will also be cleaned up.

//client.js
const fetch = require('node-fetch');
fetch('http://localhost:5000/fetch', {
    timeout: 20
}).then(res => {
    console.log('client receive response time:', new Date().toLocaleTimeString());
    return res.text();
});
console.log('client send request time:', new Date().toLocaleTimeString());
//server.js
const Koa = require('koa');
const app = new Koa();
app.listen(5000);
app.use(async (ctx, next) => {
    ctx.status = 200;
    ctx.res.write(new Array(Math.pow(2, 22)).join('a'));
    console.log('write1');
    await new Promise(res => setTimeout(res, 100));
    ctx.res.write(new Array(Math.pow(2, 22)).join('b'));
    console.log('write2');
    ctx.res.end();
})

error

error may also be read ECONNRESET
image

heap snapshots

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions