Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib: use deferred queue for all internal ticks calls #51279

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/_http_client.js
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,7 @@ function ClientRequest(input, options, cb) {
if (typeof optsWithoutSignal.createConnection === 'function') {
const oncreate = once((err, socket) => {
if (err) {
process.nextTick(() => this.emit('error', err));
process._deferTick(() => this.emit('error', err));
} else {
this.onSocket(socket);
}
Expand Down Expand Up @@ -402,7 +402,7 @@ ClientRequest.prototype.abort = function abort() {
return;
}
this.aborted = true;
process.nextTick(emitAbortNT, this);
process._deferTick(emitAbortNT, this);
this.destroy();
};

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

req.destroyed = true;
if (req.res) {
Expand Down Expand Up @@ -857,7 +857,7 @@ function listenSocketTimeout(req) {
ClientRequest.prototype.onSocket = function onSocket(socket, err) {
// TODO(ronag): Between here and onSocketNT the socket
// has no 'error' handler.
process.nextTick(onSocketNT, this, socket, err);
process._deferTick(onSocketNT, this, socket, err);
};

function onSocketNT(req, socket, err) {
Expand Down
4 changes: 2 additions & 2 deletions lib/_http_incoming.js
Original file line number Diff line number Diff line change
Expand Up @@ -236,10 +236,10 @@ IncomingMessage.prototype._destroy = function _destroy(err, cb) {
e = null;
}
cleanup();
process.nextTick(onError, this, e || err, cb);
process._deferTick(onError, this, e || err, cb);
});
} else {
process.nextTick(onError, this, err, cb);
process._deferTick(onError, this, err, cb);
}
};

Expand Down
10 changes: 5 additions & 5 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
function onError(msg, err, callback) {
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
defaultTriggerAsyncIdScope(triggerAsyncId,
process.nextTick,
process._deferTick,
emitErrorNt,
msg,
err,
Expand Down Expand Up @@ -942,7 +942,7 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
if (!msg.destroyed) {
onError(msg, err, callback);
} else {
process.nextTick(callback, err);
process._deferTick(callback, err);
}
return false;
}
Expand Down Expand Up @@ -976,14 +976,14 @@ function write_(msg, chunk, encoding, callback, fromEnd) {
} else {
debug('This type of response MUST NOT have a body. ' +
'Ignoring write() calls.');
process.nextTick(callback);
process._deferTick(callback);
return true;
}
}

if (!fromEnd && msg.socket && !msg.socket.writableCorked) {
msg.socket.cork();
process.nextTick(connectionCorkNT, msg.socket);
process._deferTick(connectionCorkNT, msg.socket);
}

let ret;
Expand Down Expand Up @@ -1117,7 +1117,7 @@ OutgoingMessage.prototype.end = function end(chunk, encoding, callback) {
} else if (!this._headerSent || this.writableLength || chunk) {
this._send('', 'latin1', finish);
} else {
process.nextTick(finish);
process._deferTick(finish);
}

if (this[kSocket]) {
Expand Down
2 changes: 1 addition & 1 deletion lib/_http_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -988,7 +988,7 @@ function resOnFinish(req, res, socket, state, server) {

res.detachSocket(socket);
clearIncoming(req);
process.nextTick(emitCloseNT, res);
process._deferTick(emitCloseNT, res);

if (res._last) {
if (typeof socket.destroySoon === 'function') {
Expand Down
4 changes: 2 additions & 2 deletions lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,7 @@ function TLSSocket(socket, opts) {
}

// Read on next tick so the caller has a chance to setup listeners
process.nextTick(initRead, this, socket);
process._deferTick(initRead, this, socket);
}
ObjectSetPrototypeOf(TLSSocket.prototype, net.Socket.prototype);
ObjectSetPrototypeOf(TLSSocket, net.Socket);
Expand Down Expand Up @@ -999,7 +999,7 @@ TLSSocket.prototype.renegotiate = function(options, callback) {
this._handle.renegotiate();
} catch (err) {
if (callback) {
process.nextTick(callback, err);
process._deferTick(callback, err);
}
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion lib/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -783,7 +783,7 @@ function spawn(file, args, options) {
if (options.signal) {
const signal = options.signal;
if (signal.aborted) {
process.nextTick(onAbortListener);
process._deferTick(onAbortListener);
} else {
addAbortListener ??= require('events').addAbortListener;
const disposable = addAbortListener(signal, onAbortListener);
Expand Down
14 changes: 7 additions & 7 deletions lib/dgram.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ function doConnect(ex, self, ip, address, port, callback) {

if (ex) {
state.connectState = CONNECT_STATE_DISCONNECTED;
return process.nextTick(() => {
return process._deferTick(() => {
if (callback) {
self.removeListener('connect', callback);
callback(ex);
Expand All @@ -447,7 +447,7 @@ function doConnect(ex, self, ip, address, port, callback) {
}

state.connectState = CONNECT_STATE_CONNECTED;
process.nextTick(() => self.emit('connect'));
process._deferTick(() => self.emit('connect'));
}


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

if (ex) {
if (typeof callback === 'function') {
process.nextTick(callback, ex);
process._deferTick(callback, ex);
return;
}

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

if (err && callback) {
// Don't emit as error, dgram_legacy.js compatibility
const ex = new ExceptionWithHostPort(err, 'send', address, port);
process.nextTick(callback, ex);
process._deferTick(callback, ex);
}
}

Expand Down Expand Up @@ -747,7 +747,7 @@ Socket.prototype.close = function(callback) {
state.handle.close();
state.handle = null;
defaultTriggerAsyncIdScope(this[async_id_symbol],
process.nextTick,
process._deferTick,
socketCloseNT,
this);

Expand Down
4 changes: 2 additions & 2 deletions lib/diagnostics_channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ function wrapStoreRun(store, data, next, transform = defaultTransform) {
try {
context = transform(data);
} catch (err) {
process.nextTick(() => {
process._deferTick(() => {
triggerUncaughtException(err, false);
});
return next();
Expand Down Expand Up @@ -141,7 +141,7 @@ class ActiveChannel {
const onMessage = this._subscribers[i];
onMessage(data, this.name);
} catch (err) {
process.nextTick(() => {
process._deferTick(() => {
triggerUncaughtException(err, false);
});
}
Expand Down
10 changes: 5 additions & 5 deletions lib/dns.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,20 +194,20 @@ function lookup(hostname, options, callback) {
if (!hostname) {
emitInvalidHostnameWarning(hostname);
if (all) {
process.nextTick(callback, null, []);
process._deferTick(callback, null, []);
} else {
process.nextTick(callback, null, null, family === 6 ? 6 : 4);
process._deferTick(callback, null, null, family === 6 ? 6 : 4);
}
return {};
}

const matchedFamily = isIP(hostname);
if (matchedFamily) {
if (all) {
process.nextTick(
process._deferTick(
callback, null, [{ address: hostname, family: matchedFamily }]);
} else {
process.nextTick(callback, null, hostname, matchedFamily);
process._deferTick(callback, null, hostname, matchedFamily);
}
return {};
}
Expand All @@ -222,7 +222,7 @@ function lookup(hostname, options, callback) {
req, hostname, family, hints, verbatim,
);
if (err) {
process.nextTick(callback, new DNSException(err, 'getaddrinfo', hostname));
process._deferTick(callback, new DNSException(err, 'getaddrinfo', hostname));
return {};
}
if (hasObserver('dns')) {
Expand Down
2 changes: 1 addition & 1 deletion lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,7 @@ function addCatch(that, promise, type, args) {
then.call(promise, undefined, function(err) {
// The callback is called with nextTick to avoid a follow-up
// rejection from this promise.
process.nextTick(emitUnhandledRejectionOrErr, that, err, type, args);
process._deferTick(emitUnhandledRejectionOrErr, that, err, type, args);
});
}
} catch (err) {
Expand Down
16 changes: 8 additions & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,7 @@ function readFile(path, options, callback) {
context.signal = options.signal;
}
if (context.isUserFd) {
process.nextTick(function tick(context) {
process._deferTick(function tick(context) {
ReflectApply(readFileAfterOpen, { context }, [null, path]);
}, context);
return;
Expand Down Expand Up @@ -664,7 +664,7 @@ function read(fd, buffer, offsetOrOptions, length, position, callback) {
length |= 0;

if (length === 0) {
return process.nextTick(function tick() {
return process._deferTick(function tick() {
callback(null, 0, buffer);
});
}
Expand Down Expand Up @@ -958,7 +958,7 @@ function writev(fd, buffers, position, callback) {
callback = maybeCallback(callback || position);

if (buffers.length === 0) {
process.nextTick(callback, null, 0, buffers);
process._deferTick(callback, null, 0, buffers);
return;
}

Expand Down Expand Up @@ -2472,7 +2472,7 @@ function watch(filename, options, listener) {
}
if (options.signal) {
if (options.signal.aborted) {
process.nextTick(() => watcher.close());
process._deferTick(() => watcher.close());
} else {
const listener = () => watcher.close();
kResistStopPropagation ??= require('internal/event_target').kResistStopPropagation;
Expand Down Expand Up @@ -2813,7 +2813,7 @@ function realpath(p, options, callback) {
LOOP();
});
} else {
process.nextTick(LOOP);
process._deferTick(LOOP);
}

// Walk down the path, swapping out linked path parts for their real
Expand Down Expand Up @@ -2844,7 +2844,7 @@ function realpath(p, options, callback) {
isFileType(statValues, S_IFSOCK)) {
return callback(null, encodeRealpathResult(p, options));
}
return process.nextTick(LOOP);
return process._deferTick(LOOP);
}

return fs.lstat(base, { bigint: true }, gotStat);
Expand All @@ -2856,7 +2856,7 @@ function realpath(p, options, callback) {
// If not a symlink, skip to the next path part
if (!stats.isSymbolicLink()) {
knownHard.add(base);
return process.nextTick(LOOP);
return process._deferTick(LOOP);
}

// Stat & read the link if not read before.
Expand Down Expand Up @@ -2901,7 +2901,7 @@ function realpath(p, options, callback) {
LOOP();
});
} else {
process.nextTick(LOOP);
process._deferTick(LOOP);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion lib/inspector.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ class Session extends EventEmitter {
this.#connection = null;
const remainingCallbacks = this.#messageCallbacks.values();
for (const callback of remainingCallbacks) {
process.nextTick(callback, new ERR_INSPECTOR_CLOSED());
process._deferTick(callback, new ERR_INSPECTOR_CLOSED());
}
this.#messageCallbacks.clear();
this.#nextId = 1;
Expand Down
3 changes: 2 additions & 1 deletion lib/internal/bootstrap/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -303,13 +303,14 @@ process.emitWarning = emitWarning;
// bootstrap to make sure that any operation done before this are synchronous.
// If any ticks or timers are scheduled before this they are unlikely to work.
{
const { nextTick, runNextTicks } = setupTaskQueue();
const { nextTick, deferTick, runNextTicks } = setupTaskQueue();
process.nextTick = nextTick;
// Used to emulate a tick manually in the JS land.
// A better name for this function would be `runNextTicks` but
// it has been exposed to the process object so we keep this legacy name
// TODO(joyeecheung): either remove it or make it public
process._tickCallback = runNextTicks;
process._deferTick = deferTick;

const { setupTimers } = internalBinding('timers');
const {
Expand Down
4 changes: 2 additions & 2 deletions lib/internal/bootstrap/switches/is_main_thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ function dummyDestroy(err, cb) {
// The 'close' event is needed so that finished and
// pipeline work correctly.
if (!this._writableState.emitClose) {
process.nextTick(() => {
process._deferTick(() => {
this.emit('close');
});
}
Expand Down Expand Up @@ -258,7 +258,7 @@ function getStdin() {
// once the stream implementation does so (one nextTick later),
// so that the process can close down.
stdin.on('pause', () => {
process.nextTick(onpause);
process._deferTick(onpause);
});

function onpause() {
Expand Down
Loading
Loading