You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Readable.unshift() can silently drop data during synchronous flow #67598
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');constchunks=[Buffer.from('AB'),Buffer.from('CD')];conststream=newReadable({read(){this.push(chunks.shift()??null);},});constreceived=[];letfirstChunk=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.
Activity
stream: keep data buffered during synchronous flow
stream: keep data buffered during synchronous flow
stream: keep data buffered during synchronous flow