Skip to content

Commit

Permalink
net: make writeAfterFIN() return false
Browse files Browse the repository at this point in the history
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 <[email protected]>
Reviewed-By: Rich Trott <[email protected]>
Reviewed-By: Colin Ihrig <[email protected]>
  • Loading branch information
lpinca authored and BridgeAR committed Jun 17, 2019
1 parent e5c2675 commit dfbbfbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
10 changes: 8 additions & 2 deletions test/parallel/test-net-write-after-end-nt.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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();
Expand Down

0 comments on commit dfbbfbb

Please sign in to comment.