Skip to content

Commit 22b4ab7

Browse files
committed
socket: don't emit premature 'close'
1 parent 810af50 commit 22b4ab7

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

doc/api/net.md

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -407,10 +407,7 @@ endpoint, depending on what it [`connect()`][`socket.connect()`] to.
407407
added: v0.1.90
408408
-->
409409

410-
* `hadError` {boolean} `true` if the socket had a transmission error.
411-
412-
Emitted once the socket is fully closed. The argument `hadError` is a boolean
413-
which says if the socket was closed due to a transmission error.
410+
Emitted once the socket is fully closed.
414411

415412
### Event: 'connect'
416413
<!-- YAML

lib/net.js

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,6 @@ function Socket(options) {
249249

250250
// Prevent the "no-half-open enforcer" from being inherited from `Duplex`.
251251
options.allowHalfOpen = true;
252-
// For backwards compat do not emit close on destroy.
253-
options.emitClose = false;
254252
stream.Duplex.call(this, options);
255253

256254
// Default to *not* allowing half open sockets.
@@ -583,33 +581,33 @@ Socket.prototype._destroy = function(exception, cb) {
583581
clearTimeout(s[kTimeout]);
584582
}
585583

586-
debug('close');
584+
const onClosed = () => {
585+
cb(exception);
586+
587+
if (this._server) {
588+
COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
589+
debug('has server');
590+
this._server._connections--;
591+
if (this._server._emitCloseIfDrained) {
592+
this._server._emitCloseIfDrained();
593+
}
594+
}
595+
};
596+
587597
if (this._handle) {
598+
debug('close');
588599
if (this !== process.stderr)
589600
debug('close handle');
590-
var isException = exception ? true : false;
591601
// `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
592602
this[kBytesRead] = this._handle.bytesRead;
593603
this[kBytesWritten] = this._handle.bytesWritten;
594604

595-
this._handle.close(() => {
596-
debug('emit close');
597-
this.emit('close', isException);
598-
});
605+
this._handle.close(onClosed);
599606
this._handle.onread = noop;
600607
this._handle = null;
601608
this._sockname = null;
602-
}
603-
604-
cb(exception);
605-
606-
if (this._server) {
607-
COUNTER_NET_SERVER_CONNECTION_CLOSE(this);
608-
debug('has server');
609-
this._server._connections--;
610-
if (this._server._emitCloseIfDrained) {
611-
this._server._emitCloseIfDrained();
612-
}
609+
} else {
610+
onClosed();
613611
}
614612
};
615613

0 commit comments

Comments
 (0)