Skip to content

Commit 9389b46

Browse files
oyydTrott
authored andcommitted
net: emit "write after end" errors in the next tick
This commit makes those errors caused by calling `net.Socket.write()` after sockets ending be emitted in the next tick. PR-URL: #24457 Fixes: #24111 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 8c0aa84 commit 9389b46

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

lib/net.js

+1-2
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,7 @@ function writeAfterFIN(chunk, encoding, cb) {
401401
// eslint-disable-next-line no-restricted-syntax
402402
var er = new Error('This socket has been ended by the other party');
403403
er.code = 'EPIPE';
404-
// TODO: defer error events consistently everywhere, not just the cb
405-
this.emit('error', er);
404+
process.nextTick(emitErrorNT, this, er);
406405
if (typeof cb === 'function') {
407406
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
408407
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
const assert = require('assert');
5+
const net = require('net');
6+
7+
const { mustCall } = common;
8+
9+
// This test ensures those errors caused by calling `net.Socket.write()`
10+
// after sockets ending will be emitted in the next tick.
11+
const server = net.createServer(mustCall((socket) => {
12+
socket.end();
13+
})).listen(() => {
14+
const client = net.connect(server.address().port, () => {
15+
let hasError = false;
16+
client.on('error', mustCall((err) => {
17+
hasError = true;
18+
server.close();
19+
}));
20+
client.on('end', mustCall(() => {
21+
client.write('hello', mustCall());
22+
assert(!hasError, 'The error should be emitted in the next tick.');
23+
}));
24+
client.end();
25+
});
26+
});

0 commit comments

Comments
 (0)