Skip to content

Commit 0dcb465

Browse files
committed
lib: use deferred queue for all internal ticks calls
Refs: nodejs#51267
1 parent ee61c2c commit 0dcb465

50 files changed

Lines changed: 248 additions & 184 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

lib/_http_client.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ function ClientRequest(input, options, cb) {
339339
if (typeof optsWithoutSignal.createConnection === 'function') {
340340
const oncreate = once((err, socket) => {
341341
if (err) {
342-
process.nextTick(() => this.emit('error', err));
342+
process._deferTick(() => this.emit('error', err));
343343
} else {
344344
this.onSocket(socket);
345345
}
@@ -402,7 +402,7 @@ ClientRequest.prototype.abort = function abort() {
402402
return;
403403
}
404404
this.aborted = true;
405-
process.nextTick(emitAbortNT, this);
405+
process._deferTick(emitAbortNT, this);
406406
this.destroy();
407407
};
408408

@@ -723,7 +723,7 @@ function responseKeepAlive(req) {
723723
const asyncId = socket._handle ? socket._handle.getAsyncId() : undefined;
724724
// Mark this socket as available, AFTER user-added end
725725
// handlers have a chance to run.
726-
defaultTriggerAsyncIdScope(asyncId, process.nextTick, emitFreeNT, req);
726+
defaultTriggerAsyncIdScope(asyncId, process._deferTick, emitFreeNT, req);
727727

728728
req.destroyed = true;
729729
if (req.res) {
@@ -857,7 +857,7 @@ function listenSocketTimeout(req) {
857857
ClientRequest.prototype.onSocket = function onSocket(socket, err) {
858858
// TODO(ronag): Between here and onSocketNT the socket
859859
// has no 'error' handler.
860-
process.nextTick(onSocketNT, this, socket, err);
860+
process._deferTick(onSocketNT, this, socket, err);
861861
};
862862

863863
function onSocketNT(req, socket, err) {

lib/_http_incoming.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,10 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
236236
e = null;
237237
}
238238
cleanup();
239-
process.nextTick(onError, this, e || err, cb);
239+
process._deferTick(onError, this, e || err, cb);
240240
});
241241
} else {
242-
process.nextTick(onError, this, err, cb);
242+
process._deferTick(onError, this, err, cb);
243243
}
244244
};
245245

lib/_http_outgoing.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
895895
function onError(msg, err, callback) {
896896
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
897897
defaultTriggerAsyncIdScope(triggerAsyncId,
898-
process.nextTick,
898+
process._deferTick,
899899
emitErrorNt,
900900
msg,
901901
err,
@@ -942,7 +942,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
942942
if (!msg.destroyed) {
943943
onError(msg, err, callback);
944944
} else {
945-
process.nextTick(callback, err);
945+
process._deferTick(callback, err);
946946
}
947947
return false;
948948
}
@@ -976,14 +976,14 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
976976
} else {
977977
debug('This type of response MUST NOT have a body. ' +
978978
'Ignoring write() calls.');
979-
process.nextTick(callback);
979+
process._deferTick(callback);
980980
return true;
981981
}
982982
}
983983

984984
if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
985985
msg.socket.cork();
986-
process.nextTick(connectionCorkNT, msg.socket);
986+
process._deferTick(connectionCorkNT, msg.socket);
987987
}
988988

989989
let ret;
@@ -1117,7 +1117,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
11171117
} else if (!this._headerSent || this.writableLength || chunk) {
11181118
this._send('', 'latin1', finish);
11191119
} else {
1120-
process.nextTick(finish);
1120+
process._deferTick(finish);
11211121
}
11221122

11231123
if (this[kSocket]) {

lib/_http_server.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -988,7 +988,7 @@ function resOnFinish(req, res, socket, state, server) {
988988

989989
res.detachSocket(socket);
990990
clearIncoming(req);
991-
process.nextTick(emitCloseNT, res);
991+
process._deferTick(emitCloseNT, res);
992992

993993
if (res._last) {
994994
if (typeof socket.destroySoon === 'function') {

lib/_tls_wrap.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,7 @@ function TLSSocket(socket, opts) {
609609
}
610610

611611
// Read on next tick so the caller has a chance to setup listeners
612-
process.nextTick(initRead, this, socket);
612+
process._deferTick(initRead, this, socket);
613613
}
614614
ObjectSetPrototypeOf(TLSSocket.prototype, net.Socket.prototype);
615615
ObjectSetPrototypeOf(TLSSocket, net.Socket);
@@ -999,7 +999,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
999999
this._handle.renegotiate();
10001000
} catch (err) {
10011001
if (callback) {
1002-
process.nextTick(callback, err);
1002+
process._deferTick(callback, err);
10031003
}
10041004
return false;
10051005
}

lib/child_process.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -783,7 +783,7 @@ function spawn(file, args, options) {
783783
if (options.signal) {
784784
const signal = options.signal;
785785
if (signal.aborted) {
786-
process.nextTick(onAbortListener);
786+
process._deferTick(onAbortListener);
787787
} else {
788788
addAbortListener ??= require('events').addAbortListener;
789789
const disposable = addAbortListener(signal, onAbortListener);

lib/dgram.js

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ function doConnect(ex, self, ip, address, port, callback) {
436436

437437
if (ex) {
438438
state.connectState = CONNECT_STATE_DISCONNECTED;
439-
return process.nextTick(() => {
439+
return process._deferTick(() => {
440440
if (callback) {
441441
self.removeListener('connect', callback);
442442
callback(ex);
@@ -447,7 +447,7 @@ function doConnect(ex, self, ip, address, port, callback) {
447447
}
448448

449449
state.connectState = CONNECT_STATE_CONNECTED;
450-
process.nextTick(() => self.emit('connect'));
450+
process._deferTick(() => self.emit('connect'));
451451
}
452452

453453

@@ -680,11 +680,11 @@ function doSend(ex, self, ip, list, address, port, callback) {
680680

681681
if (ex) {
682682
if (typeof callback === 'function') {
683-
process.nextTick(callback, ex);
683+
process._deferTick(callback, ex);
684684
return;
685685
}
686686

687-
process.nextTick(() => self.emit('error', ex));
687+
process._deferTick(() => self.emit('error', ex));
688688
return;
689689
} else if (!state.handle) {
690690
return;
@@ -709,14 +709,14 @@ function doSend(ex, self, ip, list, address, port, callback) {
709709
// Synchronous finish. The return code is msg_length + 1 so that we can
710710
// distinguish between synchronous success and asynchronous success.
711711
if (callback)
712-
process.nextTick(callback, null, err - 1);
712+
process._deferTick(callback, null, err - 1);
713713
return;
714714
}
715715

716716
if (err && callback) {
717717
// Don't emit as error, dgram_legacy.js compatibility
718718
const ex = new ExceptionWithHostPort(err, 'send', address, port);
719-
process.nextTick(callback, ex);
719+
process._deferTick(callback, ex);
720720
}
721721
}
722722

@@ -747,7 +747,7 @@ Socket.prototype.close = function(callback) {
747747
state.handle.close();
748748
state.handle = null;
749749
defaultTriggerAsyncIdScope(this[async_id_symbol],
750-
process.nextTick,
750+
process._deferTick,
751751
socketCloseNT,
752752
this);
753753

lib/diagnostics_channel.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ function wrapStoreRun(store, data, next, transform = defaultTransform) {
8282
try {
8383
context = transform(data);
8484
} catch (err) {
85-
process.nextTick(() => {
85+
process._deferTick(() => {
8686
triggerUncaughtException(err, false);
8787
});
8888
return next();
@@ -141,7 +141,7 @@ class ActiveChannel {
141141
const onMessage = this._subscribers[i];
142142
onMessage(data, this.name);
143143
} catch (err) {
144-
process.nextTick(() => {
144+
process._deferTick(() => {
145145
triggerUncaughtException(err, false);
146146
});
147147
}

lib/dns.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -194,20 +194,20 @@ function lookup(hostname, options, callback) {
194194
if (!hostname) {
195195
emitInvalidHostnameWarning(hostname);
196196
if (all) {
197-
process.nextTick(callback, null, []);
197+
process._deferTick(callback, null, []);
198198
} else {
199-
process.nextTick(callback, null, null, family === 6 ? 6 : 4);
199+
process._deferTick(callback, null, null, family === 6 ? 6 : 4);
200200
}
201201
return {};
202202
}
203203

204204
const matchedFamily = isIP(hostname);
205205
if (matchedFamily) {
206206
if (all) {
207-
process.nextTick(
207+
process._deferTick(
208208
callback, null, [{ address: hostname, family: matchedFamily }]);
209209
} else {
210-
process.nextTick(callback, null, hostname, matchedFamily);
210+
process._deferTick(callback, null, hostname, matchedFamily);
211211
}
212212
return {};
213213
}
@@ -222,7 +222,7 @@ function lookup(hostname, options, callback) {
222222
req, hostname, family, hints, verbatim,
223223
);
224224
if (err) {
225-
process.nextTick(callback, new DNSException(err, 'getaddrinfo', hostname));
225+
process._deferTick(callback, new DNSException(err, 'getaddrinfo', hostname));
226226
return {};
227227
}
228228
if (hasObserver('dns')) {

lib/events.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -377,7 +377,7 @@ function addCatch(that, promise, type, args) {
377377
then.call(promise, undefined, function(err) {
378378
// The callback is called with nextTick to avoid a follow-up
379379
// rejection from this promise.
380-
process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
380+
process._deferTick(emitUnhandledRejectionOrErr, that, err, type, args);
381381
});
382382
}
383383
} catch (err) {

0 commit comments

Comments
 (0)