Skip to content

Commit 012e034

Browse files
committed
WIP net: enable emitclose in socket
1 parent fef2128 commit 012e034

File tree

6 files changed

+32
-16
lines changed

6 files changed

+32
-16
lines changed

lib/internal/streams/destroy.js

+12-4
Original file line numberDiff line numberDiff line change
@@ -151,12 +151,20 @@ function _destroy(self, err, cb) {
151151

152152
function emitErrorCloseNT(self, err) {
153153
emitErrorNT(self, err);
154-
emitCloseNT(self);
154+
emitCloseNT(self, err);
155155
}
156156

157-
function emitCloseNT(self) {
157+
function emitCloseNT(self, err) {
158158
const r = self._readableState;
159159
const w = self._writableState;
160+
const shouldEmitClose = (w && w.emitClose) || (r && r.emitClose);
161+
const shouldPassErrorFlag = (w && w.errorFlagOnClose) ||
162+
(r && r.errorFlagOnClose);
163+
164+
let flag;
165+
if (shouldPassErrorFlag) {
166+
flag = err ? true : false;
167+
}
160168

161169
if (w) {
162170
w.closeEmitted = true;
@@ -165,8 +173,8 @@ function emitCloseNT(self) {
165173
r.closeEmitted = true;
166174
}
167175

168-
if ((w && w.emitClose) || (r && r.emitClose)) {
169-
self.emit('close');
176+
if (shouldEmitClose) {
177+
self.emit('close', flag);
170178
}
171179
}
172180

lib/internal/streams/readable.js

+4
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,10 @@ function ReadableState(options, stream, isDuplex) {
134134
// Should close be emitted on destroy. Defaults to true.
135135
this.emitClose = !options || options.emitClose !== false;
136136

137+
// Should a boolean flag be passed as argument when emitting close if an error
138+
// occurred
139+
this.errorFlagOnClose = options && options.errorFlagOnClose === true;
140+
137141
// Should .destroy() be called after 'end' (and potentially 'finish').
138142
this.autoDestroy = !options || options.autoDestroy !== false;
139143

lib/internal/streams/writable.js

+4
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ function WritableState(options, stream, isDuplex) {
180180
// Should close be emitted on destroy. Defaults to true.
181181
this.emitClose = !options || options.emitClose !== false;
182182

183+
// Should a boolean flag be passed as argument when emitting close if an error
184+
// occurred
185+
this.errorFlagOnClose = options && options.errorFlagOnClose === true;
186+
183187
// Should .destroy() be called after 'finish' (and potentially 'end').
184188
this.autoDestroy = !options || options.autoDestroy !== false;
185189

lib/net.js

+4-12
Original file line numberDiff line numberDiff line change
@@ -301,8 +301,8 @@ function Socket(options) {
301301

302302
// Default to *not* allowing half open sockets.
303303
options.allowHalfOpen = Boolean(options.allowHalfOpen);
304-
// For backwards compat do not emit close on destroy.
305-
options.emitClose = false;
304+
options.emitClose = true;
305+
options.errorFlagOnClose = true;
306306
options.autoDestroy = true;
307307
// Handle strings directly.
308308
options.decodeStrings = false;
@@ -657,28 +657,20 @@ Socket.prototype._destroy = function(exception, cb) {
657657
if (this._handle) {
658658
if (this !== process.stderr)
659659
debug('close handle');
660-
const isException = exception ? true : false;
660+
// const isException = exception ? true : false;
661661
// `bytesRead` and `kBytesWritten` should be accessible after `.destroy()`
662662
this[kBytesRead] = this._handle.bytesRead;
663663
this[kBytesWritten] = this._handle.bytesWritten;
664664

665665
this._handle.close(() => {
666666
debug('emit close');
667-
if (this._writableState) {
668-
this._writableState.closed = true;
669-
}
670-
if (this._readableState) {
671-
this._readableState.closed = true;
672-
}
673-
this.emit('close', isException);
667+
cb(exception);
674668
});
675669
this._handle.onread = noop;
676670
this._handle = null;
677671
this._sockname = null;
678-
cb(exception);
679672
} else {
680673
cb(exception);
681-
process.nextTick(emitCloseNT, this);
682674
}
683675

684676
if (this._server) {

test/parallel/test-http2-respond-with-file-connection-abort.js

+4
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ server.listen(0, common.mustCall(() => {
2525

2626
req.on('response', common.mustCall(() => {}));
2727
req.once('data', common.mustCall(() => {
28+
// TODO: is this test needed?
29+
// It errors with ERR_HTTP2_NO_SOCKET_MANIPULATION because we are
30+
// calling destroy on the Proxy(ed) socket of the Http2ClientSession
31+
// which causes `emit` to becalled and the error to be thrown.
2832
net.Socket.prototype.destroy.call(client.socket);
2933
server.close();
3034
}));

test/parallel/test-tls-socket-close.js

+4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ const tls = require('tls');
88
const net = require('net');
99
const fixtures = require('../common/fixtures');
1010

11+
// TODO: is this test needed?
12+
// It fails with a timeout error because the `error` event is never emitted.
13+
// `write` doesn't error, is that a good thing?
14+
1115
// Regression test for https://github.com/nodejs/node/issues/8074
1216
//
1317
// This test has a dependency on the order in which the TCP connection is made,

0 commit comments

Comments
 (0)