Skip to content

Commit a4d4248

Browse files
addaleaxBridgeAR
authored andcommitted
net: use _final instead of on('finish')
Shutting down the connection is what `_final` is there for. PR-URL: nodejs#18608 Reviewed-By: Luigi Pinca <[email protected]> Reviewed-By: James M Snell <[email protected]> Reviewed-By: Matteo Collina <[email protected]>
1 parent d19f29f commit a4d4248

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

lib/internal/streams/destroy.js

Lines changed: 2 additions & 0 deletions
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

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -251,7 +251,6 @@ function Socket(options) {
251251
}
252252

253253
// shut down the socket when we're finished with it.
254-
this.on('finish', onSocketFinish);
255254
this.on('end', onReadableStreamEnd);
256255

257256
initSocketHandle(this);
@@ -292,39 +291,42 @@ Socket.prototype._unrefTimer = function _unrefTimer() {
292291

293292
function shutdownSocket(self, callback) {
294293
var req = new ShutdownWrap();
295-
req.oncomplete = callback;
294+
req.oncomplete = afterShutdown;
296295
req.handle = self._handle;
296+
req.callback = callback;
297297
return self._handle.shutdown(req);
298298
}
299299

300300
// the user has called .end(), and all the bytes have been
301301
// sent out to the other side.
302-
function onSocketFinish() {
303-
// If still connecting - defer handling 'finish' until 'connect' will happen
302+
Socket.prototype._final = function(cb) {
303+
// If still connecting - defer handling `_final` until 'connect' will happen
304304
if (this.connecting) {
305-
debug('osF: not yet connected');
306-
return this.once('connect', onSocketFinish);
305+
debug('_final: not yet connected');
306+
return this.once('connect', () => this._final(cb));
307307
}
308308

309-
debug('onSocketFinish');
310309
if (!this.readable || this._readableState.ended) {
311-
debug('oSF: ended, destroy', this._readableState);
310+
debug('_final: ended, destroy', this._readableState);
311+
cb();
312312
return this.destroy();
313313
}
314314

315-
debug('oSF: not ended, call shutdown()');
315+
debug('_final: not ended, call shutdown()');
316316

317317
// otherwise, just shutdown, or destroy() if not possible
318-
if (!this._handle || !this._handle.shutdown)
318+
if (!this._handle || !this._handle.shutdown) {
319+
cb();
319320
return this.destroy();
321+
}
320322

321323
var err = defaultTriggerAsyncIdScope(
322-
this[async_id_symbol], shutdownSocket, this, afterShutdown
324+
this[async_id_symbol], shutdownSocket, this, cb
323325
);
324326

325327
if (err)
326328
return this.destroy(errnoException(err, 'shutdown'));
327-
}
329+
};
328330

329331

330332
function afterShutdown(status, handle) {
@@ -333,6 +335,8 @@ function afterShutdown(status, handle) {
333335
debug('afterShutdown destroyed=%j', self.destroyed,
334336
self._readableState);
335337

338+
this.callback();
339+
336340
// callback may come after call to destroy.
337341
if (self.destroyed)
338342
return;

test/async-hooks/test-shutdownwrap.js

Lines changed: 7 additions & 5 deletions
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

Lines changed: 14 additions & 0 deletions
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)