Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 906bbef

Browse files
committedFeb 21, 2018
net: use _final instead of on('finish')
Shutting down the connection is what `_final` is there for.
1 parent 6bdc18c commit 906bbef

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed
 

‎lib/internal/streams/destroy.js

+2
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@ function undestroy() {
5555
this._writableState.destroyed = false;
5656
this._writableState.ended = false;
5757
this._writableState.ending = false;
58+
this._writableState.finalCalled = false;
59+
this._writableState.prefinished = false;
5860
this._writableState.finished = false;
5961
this._writableState.errorEmitted = false;
6062
}

‎lib/net.js

+16-12
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,6 @@ function Socket(options) {
256256
}
257257

258258
// shut down the socket when we're finished with it.
259-
this.on('finish', onSocketFinish);
260259
this.on('_socketEnd', onSocketEnd);
261260

262261
initSocketHandle(this);
@@ -303,39 +302,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
303302

304303
function shutdownSocket(self, callback) {
305304
var req = new ShutdownWrap();
306-
req.oncomplete = callback;
305+
req.oncomplete = afterShutdown;
307306
req.handle = self._handle;
307+
req.callback = callback;
308308
return self._handle.shutdown(req);
309309
}
310310

311311
// the user has called .end(), and all the bytes have been
312312
// sent out to the other side.
313-
function onSocketFinish() {
314-
// If still connecting - defer handling 'finish' until 'connect' will happen
313+
Socket.prototype._final = function(cb) {
314+
// If still connecting - defer handling `_final` until 'connect' will happen
315315
if (this.connecting) {
316-
debug('osF: not yet connected');
317-
return this.once('connect', onSocketFinish);
316+
debug('_final: not yet connected');
317+
return this.once('connect', () => this._final(cb));
318318
}
319319

320-
debug('onSocketFinish');
321320
if (!this.readable || this._readableState.ended) {
322-
debug('oSF: ended, destroy', this._readableState);
321+
debug('_final: ended, destroy', this._readableState);
322+
cb();
323323
return this.destroy();
324324
}
325325

326-
debug('oSF: not ended, call shutdown()');
326+
debug('_final: not ended, call shutdown()');
327327

328328
// otherwise, just shutdown, or destroy() if not possible
329-
if (!this._handle || !this._handle.shutdown)
329+
if (!this._handle || !this._handle.shutdown) {
330+
cb();
330331
return this.destroy();
332+
}
331333

332334
var err = defaultTriggerAsyncIdScope(
333-
this[async_id_symbol], shutdownSocket, this, afterShutdown
335+
this[async_id_symbol], shutdownSocket, this, cb
334336
);
335337

336338
if (err)
337339
return this.destroy(errnoException(err, 'shutdown'));
338-
}
340+
};
339341

340342

341343
function afterShutdown(status, handle) {
@@ -344,6 +346,8 @@ function afterShutdown(status, handle) {
344346
debug('afterShutdown destroyed=%j', self.destroyed,
345347
self._readableState);
346348

349+
this.callback();
350+
347351
// callback may come after call to destroy.
348352
if (self.destroyed)
349353
return;

‎test/async-hooks/test-shutdownwrap.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,13 @@ let endedConnection = false;
2424
function onconnection(c) {
2525
assert.strictEqual(hooks.activitiesOfTypes('SHUTDOWNWRAP').length, 0);
2626
c.end();
27-
endedConnection = true;
28-
const as = hooks.activitiesOfTypes('SHUTDOWNWRAP');
29-
assert.strictEqual(as.length, 1);
30-
checkInvocations(as[0], { init: 1 }, 'after ending client connection');
31-
this.close(onserverClosed);
27+
process.nextTick(() => {
28+
endedConnection = true;
29+
const as = hooks.activitiesOfTypes('SHUTDOWNWRAP');
30+
assert.strictEqual(as.length, 1);
31+
checkInvocations(as[0], { init: 1 }, 'after ending client connection');
32+
this.close(onserverClosed);
33+
});
3234
}
3335

3436
function onconnected() {

‎test/parallel/test-stream-writable-destroy.js

+14
Original file line numberDiff line numberDiff line change
@@ -185,3 +185,17 @@ const { inherits } = require('util');
185185
assert.strictEqual(expected, err);
186186
}));
187187
}
188+
189+
{
190+
// Checks that `._undestroy()` restores the state so that `final` will be
191+
// called again.
192+
const write = new Writable({
193+
write: common.mustNotCall(),
194+
final: common.mustCall((cb) => cb(), 2)
195+
});
196+
197+
write.end();
198+
write.destroy();
199+
write._undestroy();
200+
write.end();
201+
}

0 commit comments

Comments
 (0)
Please sign in to comment.