From 69fdb47e207f9201f36593ba72440e122d193743 Mon Sep 17 00:00:00 2001 From: Matteo Collina Date: Thu, 20 Jul 2017 18:02:54 +0200 Subject: [PATCH] http: do not abort if socket is missing Fixes: https://github.com/nodejs/node/issues/14368 PR-URL: https://github.com/nodejs/node/pull/14387 Reviewed-By: Refael Ackermann Reviewed-By: Anna Henningsen Reviewed-By: James M Snell Reviewed-By: Trevor Norris --- lib/_http_outgoing.js | 2 +- .../test-http-server-write-after-end.js | 27 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/parallel/test-http-server-write-after-end.js diff --git a/lib/_http_outgoing.js b/lib/_http_outgoing.js index 8402bd2b61aaf4..1c570284ef5ccb 100644 --- a/lib/_http_outgoing.js +++ b/lib/_http_outgoing.js @@ -642,7 +642,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}`); +}));