Skip to content

stream: emit .write() end error in next tick #4749

New issue

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

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 5 additions & 6 deletions lib/_stream_writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -155,11 +155,10 @@ Writable.prototype.pipe = function() {
};


function writeAfterEnd(stream, cb) {
var er = new Error('write after end');
// TODO: defer error events consistently everywhere, not just the cb
stream.emit('error', er);
process.nextTick(cb, er);
function writeAfterEndErr(stream, cb) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just curious, why change the function name?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm probably too conscious about what function names says at first glance :) writeAfterEnd() gave me the impression it should perform a write operation after it has ended. Appending err to the name makes it explicit there's an error involved in what's going to happen.

If you see it as superfluous, I'll revert the name change - just say the word :)

const err = new Error('write after end');
stream.emit('error', err);
cb(err);
}

// If we get something that is not a buffer, string, null, or undefined,
Expand Down Expand Up @@ -201,7 +200,7 @@ Writable.prototype.write = function(chunk, encoding, cb) {
cb = nop;

if (state.ended)
writeAfterEnd(this, cb);
process.nextTick(writeAfterEndErr, this, cb);
else if (validChunk(this, state, chunk, cb)) {
state.pendingcb++;
ret = writeOrBuffer(this, state, chunk, encoding, cb);
Expand Down
14 changes: 9 additions & 5 deletions test/parallel/test-file-write-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ var EXPECTED = '012345678910';
var callbacks = {
open: -1,
drain: -2,
close: -1
close: -1,
error: -1
};

file
Expand All @@ -25,6 +26,10 @@ file
assert.equal('number', typeof fd);
})
.on('error', function(err) {
// we're expecting write after end error
if (err.message === 'write after end') {
return callbacks.error++;
}
throw err;
})
.on('drain', function() {
Expand All @@ -43,10 +48,9 @@ file
assert.strictEqual(file.bytesWritten, EXPECTED.length * 2);

callbacks.close++;
assert.throws(function() {
console.error('write after end should not be allowed');
file.write('should not work anymore');
});

console.error('write after end should not be allowed');
file.write('should not work anymore');

fs.unlinkSync(fn);
});
Expand Down
31 changes: 31 additions & 0 deletions test/parallel/test-stream-writeable-write-ended-err.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
'use strict';

require('../common');

const assert = require('assert');
const Writable = require('stream').Writable;

const stream = new Writable({ write });

let errorsRecorded = 0;

function write() {
throw new Error('write() should not have been called!');
}

function errorRecorder(err) {
if (err.message === 'write after end') {
errorsRecorded++;
}
}

// should trigger ended errors when writing later
stream.end();

stream.on('error', errorRecorder);
stream.write('this should explode', errorRecorder);

assert.equal(errorsRecorded, 0,
'Waits until next tick before emitting error');

process.nextTick(() => assert.equal(errorsRecorded, 2));