Skip to content
Permalink
Browse files
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: #27996
Reviewed-By: Anna Henningsen <anna@addaleax.net>
Reviewed-By: Rich Trott <rtrott@gmail.com>
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
  • Loading branch information
lpinca authored and BridgeAR committed Jun 17, 2019
1 parent e5c2675 commit dfbbfbb765e46899b67ce5c7cab6c2bb618c8f68
Showing with 10 additions and 2 deletions.
  1. +2 −0 lib/net.js
  2. +8 −2 test/parallel/test-net-write-after-end-nt.js
@@ -410,6 +410,8 @@ function writeAfterFIN(chunk, encoding, cb) {
if (typeof cb === 'function') {
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
}

return false;
}

Socket.prototype.setTimeout = setStreamTimeout;
@@ -4,7 +4,7 @@ const common = require('../common');
const assert = require('assert');
const net = require('net');

const { mustCall } = common;
const { expectsError, mustCall } = common;

// This test ensures those errors caused by calling `net.Socket.write()`
// after sockets ending will be emitted in the next tick.
@@ -18,7 +18,13 @@ const server = net.createServer(mustCall((socket) => {
server.close();
}));
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.');
}));
client.end();

0 comments on commit dfbbfbb

Please sign in to comment.