Skip to content

Stream.Writable reports wrong number in _writableState.bufferedRequestCount #6758

Closed
@andrey-makhnutin

Description

@andrey-makhnutin
  • Version: v6.1.0
  • Platform: Windows 10 64-bit
  • Subsystem: Stream

Was casually reading _stream_writable.js and noticed that clearBuffer() mistakenly zeroes state.bufferedRequestCount at the end of the function in case when _writev is not implemented and _write is not synchronous. The while (entry) loop is breaken from, leaving data in the buffer, but the request counter is zeroed out anyway.

Here's the testing code

'use strict';

const Stream = require('stream');

class testWritable extends Stream.Writable {
  constructor() {
    super({objectMode: true});
  }

  _write(chunk, encoding, cb) {
    console.log(`_writing chunk ${chunk}`);
    setTimeout(cb, 1000);
  }
}

const testStream = new testWritable();

testStream.cork();
for (let i = 1; i <= 5; ++i) {
  testStream.write(i, () => { 
    console.log(`chunk ${i} cb called`);
    console.log(`_writableState.bufferedRequestCount = ${testStream._writableState.bufferedRequestCount}`);
    console.log(`real buffered request count = ${testStream._writableState.getBuffer().length}`);
  });
}
testStream.end();
console.log('main program ends here');

And the output:

_writing chunk 1
main program ends here
_writing chunk 2
chunk 1 cb called
_writableState.bufferedRequestCount = 0
real buffered request count = 3
_writing chunk 3
chunk 2 cb called
_writableState.bufferedRequestCount = 0
real buffered request count = 2
_writing chunk 4
chunk 3 cb called
_writableState.bufferedRequestCount = 0
real buffered request count = 1
_writing chunk 5
chunk 4 cb called
_writableState.bufferedRequestCount = 0
real buffered request count = 0
chunk 5 cb called
_writableState.bufferedRequestCount = 0
real buffered request count = 0

The implications of this are super low, I understand, but you know, just in case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.good first issueIssues that are suitable for first-time contributors.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