Skip to content

Readable.unshift() can silently drop data during synchronous flow #67598

Open

Description

@jakecastelli

Version

v28.0.0-pre (current main)

Platform

macOS

Subsystem

stream

What steps will reproduce the bug?

Calling unshift() from a 'data' listener can lose data when a byte-mode Readable produces chunks synchronously. The stream then emits 'end' without reporting an error.

The example below consumes A from the first chunk and puts B back so it can be read before CD:

const { Readable } = require('node:stream');

const chunks = [Buffer.from('AB'), Buffer.from('CD')];
const stream = new Readable({
  read() {
    this.push(chunks.shift() ?? null);
  },
});

const received = [];
let firstChunk = true;

stream.on('data', (chunk) => {
  if (firstChunk) {
    firstChunk = false;

    // Consume A and put B back for the next read.
    received.push(chunk.subarray(0, 1));
    stream.unshift(chunk.subarray(1));
  } else {
    received.push(chunk);
  }
});

stream.on('end', () => {
  console.log(Buffer.concat(received).toString());
});

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

every time, no condition required

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

Expected output:

ABCD

What do you see instead?

Actual output:

ACD

Additional information

B is never delivered, even though unshift() was called before the stream ended. When 'end' fires, B is still in state.buffer while state.length is 0.

Metadata

Metadata

Assignees

Labels

streamIssues and PRs related to Node.js streams.

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions