diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index b6598f0c17036f..31973fceadb6b8 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -633,7 +633,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) { function write_(msg, chunk, encoding, callback, fromEnd) { if (msg.finished) { var err = new Error('write after end'); - nextTick(msg.socket[async_id_symbol], + nextTick(msg.socket && msg.socket[async_id_symbol], writeAfterEndNT.bind(msg), err, callback); diff --git a/test/parallel/test-http-server-write-after-end.js b/test/parallel/test-http-server-write-after-end.js new file mode 100644 index 00000000000000..045cdcf4b75afb --- /dev/null +++ b/test/parallel/test-http-server-write-after-end.js @@ -0,0 +1,27 @@ +'use strict'; + +const common = require('../common'); +const http = require('http'); +const assert = require('assert'); + +// Fix for https://github.com/nodejs/node/issues/14368 + +const server = http.createServer(handle); + +function handle(req, res) { + res.on('error', common.mustCall((err) => { + assert.strictEqual(err.message, 'write after end'); + server.close(); + })); + + res.write('hello'); + res.end(); + + setImmediate(common.mustCall(() => { + res.write('world'); + })); +} + +server.listen(0, common.mustCall(() => { + http.get(`http://localhost:${server.address().port}`); +}));