Skip to content
Closed
Show file tree
Hide file tree
Changes from 4 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
8 changes: 7 additions & 1 deletion lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -685,10 +685,10 @@ function onError(msg, err, callback) {
}

function emitErrorNt(msg, err, callback) {
callback(err);
if (typeof msg.emit === 'function' && !msg._closed) {
msg.emit('error', err);
}
callback(err);
Comment thread
rickyes marked this conversation as resolved.
Outdated
}

function write_(msg, chunk, encoding, callback, fromEnd) {
Expand Down Expand Up @@ -809,6 +809,12 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
}

if (chunk) {
if (this.finished) {
onError(this,
new ERR_STREAM_WRITE_AFTER_END(),
typeof callback !== 'function' ? nop : callback);
return this;
}
write_(this, chunk, encoding, null, true);
} else if (this.finished) {
if (typeof callback === 'function') {
Expand Down
10 changes: 9 additions & 1 deletion test/parallel/test-http-outgoing-end-multiple.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,17 @@ const common = require('../common');
const assert = require('assert');
const http = require('http');

const onWriteAfterEndError = common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_WRITE_AFTER_END');
}, 2);

const server = http.createServer(common.mustCall(function(req, res) {
res.end('testing ended state', common.mustCall());
res.end(common.mustCall());
res.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
}));
res.end('end', onWriteAfterEndError);
res.on('error', onWriteAfterEndError);
res.on('finish', common.mustCall(() => {
res.end(common.mustCall((err) => {
assert.strictEqual(err.code, 'ERR_STREAM_ALREADY_FINISHED');
Expand Down