Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

Writing to ended write stream continues without error #5603

Closed
alexwhitman opened this Issue May 30, 2013 · 2 comments

Comments

Projects
None yet
3 participants

Not sure if this is a bug but it seems odd.

Running the following testcase:

var fs = require('fs');

var files = [
    __dirname + '/1.txt',
    __dirname + '/2.txt',
    __dirname + '/3.txt'
];

var outFile = __dirname + '/out.txt';

concat(files, outFile, function() {
    console.log(fs.readFileSync(outFile).toString());
});

function concat(files, outFile, cb) {

    var writer = fs.createWriteStream(outFile);
    writer.on('error', function(err) {
        console.log(err);
    });

    function iterate() {
        console.log('iterating');
        if (files.length === 0) {
            return cb();
        }

        var path = files.shift();
        var reader = fs.createReadStream(path);
        reader.on('error', function(err) {
            console.log(err);
        });
        reader.on('end', function() {
            console.log('end');
            iterate();
        });
        reader.pipe(writer);
    }

    iterate();

}

produced an output of:

iterating
end
iterating

I eventually figured out why things weren't working as I expected (pipe() ends by default) and fixed it but I thought it was odd that the script continued while trying to pipe() for the second time with no error.

Should the WriteStream have emitted error in this case?

Node.js version 0.10.8

isaacs commented Jun 7, 2013

Hm, yeah, I agree, piping to a Writable that's been ended should certainly raise an error. Thanks for the example.

@isaacs isaacs was assigned Jun 7, 2013

@joybro joybro added a commit to joybro/node that referenced this issue Oct 27, 2013

@joybro joybro stream: End writables before emitting 'end' event
There is a timing issue b/w the internal callback for ending writable
and the user callback of 'end' event because both of them are using
the same 'end' event.

So this fails:

    reader.pipe(writer);
    reader.on('end', function() {
      writer.end('Goodbye\n');  // should be failed
    });

But this succeeds:

    reader.on('end', function() {
      writer.end('Goodbye\n');  // should be failed
    });
    reader.pipe(writer);

This commit ends writables not using 'end' event.

Fixes #5603
9d23e7d
Owner

jasnell commented May 26, 2015

No longer happening in v0.12. Closing, can reopen if necessary

@jasnell jasnell closed this May 26, 2015

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment