Skip to content

Readable stream internal buffer grows unbounded under backpressure (regression vs Node 10) #62558

@kevgeoleo

Description

@kevgeoleo

Version

v24.13.1, v25.8.1

Platform

Linux KContainer 5.10.0-12-amd64 #1 SMP Debian 5.10.103-1 (2022-03-07) x86_64 x86_64 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Hi,
The bug can be reproduced using the below PoC:

const stream = require('stream');

const rs = stream.Readable({
  read: function () {
    // Fast readable: push a large chunk and then many smaller ones
    this.push(Buffer.alloc(65500));
    for (let i = 0; i < 40; i++) {
      this.push(Buffer.alloc(1024));
    }
  }
});

const ws = stream.Writable({
  write: function (data, enc, cb) {
    // Slow writable: simulate slow consumer
    setTimeout(cb, 10);
  }
});

setInterval(function () {
  const state = rs._readableState;
  console.log('state.length %d, state.buffer.length %d, state.highWaterMark %d',
    state.length,
    state.buffer.length,
    state.highWaterMark
  );
}, 1000);

rs.pipe(ws);

How often does it reproduce? Is there a required condition?

No specific condition required

What is the expected behavior? Why is that the expected behavior?

This was previously reported as an issue (#19601) and later fixed in Node v10.0.0

root@KContainer:~/19601# nvm use 10.0.0
Now using node v10.0.0 (npm v5.6.0)
root@KContainer:~/19601# node repro.cjs
state.length 24576, state.buffer.length 24, state.highWaterMark 16384
state.length 40960, state.buffer.length 40, state.highWaterMark 16384
state.length 24576, state.buffer.length 24, state.highWaterMark 16384
state.length 114652, state.buffer.length 49, state.highWaterMark 16384
state.length 24576, state.buffer.length 24, state.highWaterMark 16384
^C

Notice that buffer length does not increase in an unbounded manner

What do you see instead?

Buffer length keeps increasing in Node v24.13.1 and 25.8.1

root@KContainer:~/19601# node repro.cjs
state.length 147420, state.buffer.length 205, state.highWaterMark 65536
state.length 147420, state.buffer.length 287, state.highWaterMark 65536
state.length 147420, state.buffer.length 410, state.highWaterMark 65536
state.length 147420, state.buffer.length 492, state.highWaterMark 65536
state.length 147420, state.buffer.length 574, state.highWaterMark 65536
state.length 147420, state.buffer.length 697, state.highWaterMark 65536
state.length 147420, state.buffer.length 779, state.highWaterMark 65536
^C

Additional information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    streamIssues and PRs related to the stream subsystem.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions