Skip to content

Commit 18c057a

Browse files
Brett Kiefertargos
Brett Kiefer
authored andcommitted
net: emit 'close' when socket ends before connect
Don't set `writable` to true when a socket connects if the socket is already in an ending state. In the existing code, afterConnect always set `writable` to true. This has been the case for a long time, but previous to commit 9b7a691, the socket would still be destroyed by `destroySoon` and emit a `'close'` event. Since that commit removed this masking behavior, we have relied on maybeDestroy to destroy the socket when the readble state is ended, and that won't happen if `writable` is set to true. If the socket has `allowHalfOpen` set to true, then `destroy` will still not be called and `'close'` will not be emitted. PR-URL: #21290 Fixes: #21268 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent 6285fe9 commit 18c057a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

lib/net.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -1138,7 +1138,8 @@ function afterConnect(status, handle, req, readable, writable) {
11381138

11391139
if (status === 0) {
11401140
self.readable = readable;
1141-
self.writable = writable;
1141+
if (!self._writableState.ended)
1142+
self.writable = writable;
11421143
self._unrefTimer();
11431144

11441145
self.emit('connect');
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'use strict';
2+
3+
const common = require('../common');
4+
5+
const net = require('net');
6+
7+
const server = net.createServer();
8+
9+
server.listen(common.mustCall(() => {
10+
const socket = net.createConnection(server.address().port);
11+
socket.on('close', common.mustCall(() => server.close()));
12+
socket.end();
13+
}));

0 commit comments

Comments
 (0)