Skip to content

Commit dfbbfbb

Browse files
lpincaBridgeAR
authored andcommitted
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 <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: Colin Ihrig <[email protected]>
1 parent e5c2675 commit dfbbfbb

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

lib/net.js

+2
Original file line numberDiff line numberDiff line change
@@ -410,6 +410,8 @@ function writeAfterFIN(chunk, encoding, cb) {
410410
if (typeof cb === 'function') {
411411
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
412412
}
413+
414+
return false;
413415
}
414416

415417
Socket.prototype.setTimeout = setStreamTimeout;

test/parallel/test-net-write-after-end-nt.js

+8-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const common = require('../common');
44
const assert = require('assert');
55
const net = require('net');
66

7-
const { mustCall } = common;
7+
const { expectsError, mustCall } = common;
88

99
// This test ensures those errors caused by calling `net.Socket.write()`
1010
// after sockets ending will be emitted in the next tick.
@@ -18,7 +18,13 @@ const server = net.createServer(mustCall((socket) => {
1818
server.close();
1919
}));
2020
client.on('end', mustCall(() => {
21-
client.write('hello', mustCall());
21+
const ret = client.write('hello', expectsError({
22+
code: 'EPIPE',
23+
message: 'This socket has been ended by the other party',
24+
type: Error
25+
}));
26+
27+
assert.strictEqual(ret, false);
2228
assert(!hasError, 'The error should be emitted in the next tick.');
2329
}));
2430
client.end();

0 commit comments

Comments
 (0)