Skip to content

Commit 4d07434

Browse files
committed
async_hooks,process: remove internalNextTick
Instead of having mostly duplicate code in form of internalNextTick, instead use the existing defaultAsyncTriggerIdScope with a slight modification which allows undefined triggerAsyncId to be passed in, which then just triggers the callback with the provided arguments. PR-URL: #19147 Refs: #19104 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Andreas Madsen <amwebdk@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
1 parent 64d5234 commit 4d07434

File tree

10 files changed

+79
-119
lines changed

10 files changed

+79
-119
lines changed

lib/_http_agent.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ const util = require('util');
2626
const EventEmitter = require('events');
2727
const debug = util.debuglog('http');
2828
const { async_id_symbol } = require('internal/async_hooks').symbols;
29-
const { nextTick } = require('internal/process/next_tick');
3029

3130
// New Agent code.
3231

@@ -342,10 +341,7 @@ Agent.prototype.destroy = function destroy() {
342341
function handleSocketCreation(request, informRequest) {
343342
return function handleSocketCreation_Inner(err, socket) {
344343
if (err) {
345-
const asyncId = (socket && socket._handle && socket._handle.getAsyncId) ?
346-
socket._handle.getAsyncId() :
347-
null;
348-
nextTick(asyncId, () => request.emit('error', err));
344+
process.nextTick(emitErrorNT, request, err);
349345
return;
350346
}
351347
if (informRequest)
@@ -355,6 +351,10 @@ function handleSocketCreation(request, informRequest) {
355351
};
356352
}
357353

354+
function emitErrorNT(emitter, err) {
355+
emitter.emit('error', err);
356+
}
357+
358358
module.exports = {
359359
Agent,
360360
globalAgent: new Agent()

lib/_http_client.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ const {
3636
const { OutgoingMessage } = require('_http_outgoing');
3737
const Agent = require('_http_agent');
3838
const { Buffer } = require('buffer');
39+
const { defaultTriggerAsyncIdScope } = require('internal/async_hooks');
3940
const { urlToOptions, searchParamsSymbol } = require('internal/url');
4041
const { outHeadersKey, ondrain } = require('internal/http');
41-
const { nextTick } = require('internal/process/next_tick');
4242
const {
4343
ERR_HTTP_HEADERS_SENT,
4444
ERR_INVALID_ARG_TYPE,
@@ -563,10 +563,10 @@ function responseKeepAlive(res, req) {
563563
socket.once('error', freeSocketErrorListener);
564564
// There are cases where _handle === null. Avoid those. Passing null to
565565
// nextTick() will call getDefaultTriggerAsyncId() to retrieve the id.
566-
const asyncId = socket._handle ? socket._handle.getAsyncId() : null;
566+
const asyncId = socket._handle ? socket._handle.getAsyncId() : undefined;
567567
// Mark this socket as available, AFTER user-added end
568568
// handlers have a chance to run.
569-
nextTick(asyncId, emitFreeNT, socket);
569+
defaultTriggerAsyncIdScope(asyncId, process.nextTick, emitFreeNT, socket);
570570
}
571571
}
572572

lib/_http_outgoing.js

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@ const common = require('_http_common');
3131
const checkIsHttpToken = common._checkIsHttpToken;
3232
const checkInvalidHeaderChar = common._checkInvalidHeaderChar;
3333
const { outHeadersKey } = require('internal/http');
34-
const { async_id_symbol } = require('internal/async_hooks').symbols;
35-
const { nextTick } = require('internal/process/next_tick');
34+
const {
35+
defaultTriggerAsyncIdScope,
36+
symbols: { async_id_symbol }
37+
} = require('internal/async_hooks');
3638
const {
3739
ERR_HTTP_HEADERS_SENT,
3840
ERR_HTTP_INVALID_HEADER_VALUE,
@@ -272,15 +274,14 @@ function _writeRaw(data, encoding, callback) {
272274
this._flushOutput(conn);
273275
} else if (!data.length) {
274276
if (typeof callback === 'function') {
275-
let socketAsyncId = this.socket[async_id_symbol];
276277
// If the socket was set directly it won't be correctly initialized
277278
// with an async_id_symbol.
278279
// TODO(AndreasMadsen): @trevnorris suggested some more correct
279280
// solutions in:
280281
// https://github.com/nodejs/node/pull/14389/files#r128522202
281-
if (socketAsyncId === undefined) socketAsyncId = null;
282-
283-
nextTick(socketAsyncId, callback);
282+
defaultTriggerAsyncIdScope(conn[async_id_symbol],
283+
process.nextTick,
284+
callback);
284285
}
285286
return true;
286287
}
@@ -633,10 +634,13 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
633634
function write_(msg, chunk, encoding, callback, fromEnd) {
634635
if (msg.finished) {
635636
const err = new ERR_STREAM_WRITE_AFTER_END();
636-
nextTick(msg.socket && msg.socket[async_id_symbol],
637-
writeAfterEndNT.bind(msg),
638-
err,
639-
callback);
637+
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
638+
defaultTriggerAsyncIdScope(triggerAsyncId,
639+
process.nextTick,
640+
writeAfterEndNT,
641+
msg,
642+
err,
643+
callback);
640644

641645
return true;
642646
}
@@ -686,8 +690,8 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
686690
}
687691

688692

689-
function writeAfterEndNT(err, callback) {
690-
this.emit('error', err);
693+
function writeAfterEndNT(msg, err, callback) {
694+
msg.emit('error', err);
691695
if (callback) callback(err);
692696
}
693697

lib/dgram.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ const {
4444
symbols: { async_id_symbol }
4545
} = require('internal/async_hooks');
4646
const { UV_UDP_REUSEADDR } = process.binding('constants').os;
47-
const { nextTick } = require('internal/process/next_tick');
4847

4948
const { UDP, SendWrap } = process.binding('udp_wrap');
5049

@@ -526,7 +525,10 @@ Socket.prototype.close = function(callback) {
526525
this._stopReceiving();
527526
this._handle.close();
528527
this._handle = null;
529-
nextTick(this[async_id_symbol], socketCloseNT, this);
528+
defaultTriggerAsyncIdScope(this[async_id_symbol],
529+
process.nextTick,
530+
socketCloseNT,
531+
this);
530532

531533
return this;
532534
};

lib/internal/async_hooks.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,8 @@ function clearDefaultTriggerAsyncId() {
281281

282282

283283
function defaultTriggerAsyncIdScope(triggerAsyncId, block, ...args) {
284+
if (triggerAsyncId === undefined)
285+
return Reflect.apply(block, null, args);
284286
// CHECK(Number.isSafeInteger(triggerAsyncId))
285287
// CHECK(triggerAsyncId > 0)
286288
const oldDefaultTriggerAsyncId = async_id_fields[kDefaultTriggerAsyncId];

lib/internal/process/next_tick.js

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
'use strict';
22

33
exports.setup = setupNextTick;
4-
// Will be overwritten when setupNextTick() is called.
5-
exports.nextTick = null;
64

75
function setupNextTick() {
86
const {
@@ -87,9 +85,6 @@ function setupNextTick() {
8785
// Needs to be accessible from beyond this scope.
8886
process._tickCallback = _tickCallback;
8987

90-
// Set the nextTick() function for internal usage.
91-
exports.nextTick = internalNextTick;
92-
9388
function _tickCallback() {
9489
let tock;
9590
do {
@@ -165,32 +160,4 @@ function setupNextTick() {
165160

166161
push(new TickObject(callback, args, getDefaultTriggerAsyncId()));
167162
}
168-
169-
// `internalNextTick()` will not enqueue any callback when the process is
170-
// about to exit since the callback would not have a chance to be executed.
171-
function internalNextTick(triggerAsyncId, callback) {
172-
if (typeof callback !== 'function')
173-
throw new ERR_INVALID_CALLBACK();
174-
// CHECK(Number.isSafeInteger(triggerAsyncId) || triggerAsyncId === null)
175-
// CHECK(triggerAsyncId > 0 || triggerAsyncId === null)
176-
177-
if (process._exiting)
178-
return;
179-
180-
var args;
181-
switch (arguments.length) {
182-
case 2: break;
183-
case 3: args = [arguments[2]]; break;
184-
case 4: args = [arguments[2], arguments[3]]; break;
185-
case 5: args = [arguments[2], arguments[3], arguments[4]]; break;
186-
default:
187-
args = new Array(arguments.length - 2);
188-
for (var i = 2; i < arguments.length; i++)
189-
args[i - 2] = arguments[i];
190-
}
191-
192-
if (triggerAsyncId === null)
193-
triggerAsyncId = getDefaultTriggerAsyncId();
194-
push(new TickObject(callback, args, triggerAsyncId));
195-
}
196163
}

lib/net.js

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ const {
5252
defaultTriggerAsyncIdScope,
5353
symbols: { async_id_symbol }
5454
} = require('internal/async_hooks');
55-
const { nextTick } = require('internal/process/next_tick');
5655
const errors = require('internal/errors');
5756
const {
5857
ERR_INVALID_ARG_TYPE,
@@ -392,7 +391,7 @@ function writeAfterFIN(chunk, encoding, cb) {
392391
// TODO: defer error events consistently everywhere, not just the cb
393392
this.emit('error', er);
394393
if (typeof cb === 'function') {
395-
nextTick(this[async_id_symbol], cb, er);
394+
defaultTriggerAsyncIdScope(this[async_id_symbol], process.nextTick, cb, er);
396395
}
397396
}
398397

@@ -1069,7 +1068,7 @@ function lookupAndConnect(self, options) {
10691068
// If host is an IP, skip performing a lookup
10701069
var addressType = isIP(host);
10711070
if (addressType) {
1072-
nextTick(self[async_id_symbol], function() {
1071+
defaultTriggerAsyncIdScope(self[async_id_symbol], process.nextTick, () => {
10731072
if (self.connecting)
10741073
defaultTriggerAsyncIdScope(
10751074
self[async_id_symbol],
@@ -1372,7 +1371,11 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
13721371
var ex = exceptionWithHostPort(err, 'listen', address, port);
13731372
this._handle.close();
13741373
this._handle = null;
1375-
nextTick(this[async_id_symbol], emitErrorNT, this, ex);
1374+
defaultTriggerAsyncIdScope(this[async_id_symbol],
1375+
process.nextTick,
1376+
emitErrorNT,
1377+
this,
1378+
ex);
13761379
return;
13771380
}
13781381

@@ -1383,7 +1386,10 @@ function setupListenHandle(address, port, addressType, backlog, fd) {
13831386
if (this._unref)
13841387
this.unref();
13851388

1386-
nextTick(this[async_id_symbol], emitListeningNT, this);
1389+
defaultTriggerAsyncIdScope(this[async_id_symbol],
1390+
process.nextTick,
1391+
emitListeningNT,
1392+
this);
13871393
}
13881394

13891395
Server.prototype._listen2 = setupListenHandle; // legacy alias
@@ -1590,8 +1596,11 @@ Server.prototype.getConnections = function(cb) {
15901596
const self = this;
15911597

15921598
function end(err, connections) {
1593-
const asyncId = self._handle ? self[async_id_symbol] : null;
1594-
nextTick(asyncId, cb, err, connections);
1599+
defaultTriggerAsyncIdScope(self[async_id_symbol],
1600+
process.nextTick,
1601+
cb,
1602+
err,
1603+
connections);
15951604
}
15961605

15971606
if (!this._usingWorkers) {
@@ -1669,8 +1678,10 @@ Server.prototype._emitCloseIfDrained = function() {
16691678
return;
16701679
}
16711680

1672-
const asyncId = this._handle ? this[async_id_symbol] : null;
1673-
nextTick(asyncId, emitCloseNT, this);
1681+
defaultTriggerAsyncIdScope(this[async_id_symbol],
1682+
process.nextTick,
1683+
emitCloseNT,
1684+
this);
16741685
};
16751686

16761687

test/async-hooks/test-internal-nexttick-default-trigger.js

Lines changed: 0 additions & 47 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
// This tests ensures that the triggerId of the nextTick function sets the
5+
// triggerAsyncId correctly.
6+
7+
const assert = require('assert');
8+
const async_hooks = require('async_hooks');
9+
const initHooks = require('./init-hooks');
10+
const { checkInvocations } = require('./hook-checks');
11+
12+
const hooks = initHooks();
13+
hooks.enable();
14+
15+
const rootAsyncId = async_hooks.executionAsyncId();
16+
17+
process.nextTick(common.mustCall(function() {
18+
assert.strictEqual(async_hooks.triggerAsyncId(), rootAsyncId);
19+
}));
20+
21+
process.on('exit', function() {
22+
hooks.sanityCheck();
23+
24+
const as = hooks.activitiesOfTypes('TickObject');
25+
checkInvocations(as[0], {
26+
init: 1, before: 1, after: 1, destroy: 1
27+
}, 'when process exits');
28+
});

test/async-hooks/test-no-assert-when-disabled.js

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,8 @@
11
'use strict';
22
// Flags: --no-force-async-hooks-checks --expose-internals
3-
const common = require('../common');
3+
require('../common');
44

55
const async_hooks = require('internal/async_hooks');
6-
const internal = require('internal/process/next_tick');
7-
8-
// Using undefined as the triggerAsyncId.
9-
// Ref: https://github.com/nodejs/node/issues/14386
10-
// Ref: https://github.com/nodejs/node/issues/14381
11-
// Ref: https://github.com/nodejs/node/issues/14368
12-
internal.nextTick(undefined, common.mustCall());
136

147
// Negative asyncIds and invalid type name
158
async_hooks.emitInit(-1, null, -1, {});

0 commit comments

Comments
 (0)