net: make writeAfterFIN() return false

If `false` is not returned a readable stream piped into the socket
might continue reading indefinitely until the process goes out of
memory.

PR-URL: https://github.com/nodejs/node/pull/27996
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
This commit is contained in:
Luigi Pinca 2019-05-31 20:36:23 +02:00 committed by Daniel Bevenius
parent d9117896a6
commit 714a32c41f
2 changed files with 10 additions and 2 deletions

View File

@ -410,6 +410,8 @@ function writeAfterFIN(chunk, encoding, cb) {
if (typeof cb === 'function') { if (typeof cb === 'function') {
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er); defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
} }
return false;
} }
Socket.prototype.setTimeout = setStreamTimeout; Socket.prototype.setTimeout = setStreamTimeout;

View File

@ -4,7 +4,7 @@ const common = require('../common');
const assert = require('assert'); const assert = require('assert');
const net = require('net'); const net = require('net');
const { mustCall } = common; const { expectsError, mustCall } = common;
// This test ensures those errors caused by calling `net.Socket.write()` // This test ensures those errors caused by calling `net.Socket.write()`
// after sockets ending will be emitted in the next tick. // after sockets ending will be emitted in the next tick.
@ -18,7 +18,13 @@ const server = net.createServer(mustCall((socket) => {
server.close(); server.close();
})); }));
client.on('end', mustCall(() => { client.on('end', mustCall(() => {
client.write('hello', mustCall()); const ret = client.write('hello', expectsError({
code: 'EPIPE',
message: 'This socket has been ended by the other party',
type: Error
}));
assert.strictEqual(ret, false);
assert(!hasError, 'The error should be emitted in the next tick.'); assert(!hasError, 'The error should be emitted in the next tick.');
})); }));
client.end(); client.end();