Skip to content

Piping twice to same stream then unpiping loses data #12718

Closed
@ovikholt

Description

@ovikholt
  • Version: v7.7.2
  • Platform: Darwin ter.local 15.6.0 Darwin Kernel Version 15.6.0: Thu Jun 23 18:25:34 PDT 2016; root:xnu-3248.60.10~1/RELEASE_X86_64 x86_64
  • Subsystem: stream

Piping twice to one single destination, and then unpiping once, has unexpected consequences. See the below code example.

var stream = require('stream');

var passThrough = new stream.PassThrough();
var writable = process.stdout;  // same results with fs.createWriteStream('somefile.txt')

passThrough.pipe(writable);
passThrough.pipe(writable);  // remove this line to see output
// At this point, as we expect, two writables are listening.
// (passThrough._events.data.length == 2)

passThrough.unpipe(writable);
// At this point, no writable is listening to the data event any more!
// (passThrough._events.data is undefined)

// But, passThrough does have pipeCount=1, and that pipe is our writable.
// The passThrough stream is flowing aka resumed...

passThrough.write('this does not get buffered, and thus gets lost\n');
passThrough.pipe(writable);
// Prints nothing

If the intended behavior of unpiping once is to leave one destination in place, the data listener for that destination must be retained, so that our writable will keep receiving data.

Otherwise, I assume the intended behavior of unpiping once is to remove all pipes to the
same destination, (and pausing the stream when there are no remaining pipes).

If it's invalid to pipe twice to one destination, the documentation should say so and/or the program should crash.

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