Skip to content

Commit ce47a8d

Browse files
committed
lib: run microtasks before ticks
This resolve multiple timing issues related to promises and nextTick. As well as resolving zaldo in promise only code, i.e. our current best practice of using process.nextTick will always apply and work. Refs: nodejs#51156 Refs: nodejs#51156 (comment) Refs: nodejs#51114 Refs: nodejs#51070 Refs: nodejs#51156 PR-URL: nodejs#51267
1 parent ee61c2c commit ce47a8d

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

doc/api/cli.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -863,6 +863,20 @@ Use this flag to generate a blob that can be injected into the Node.js
863863
binary to produce a [single executable application][]. See the documentation
864864
about [this configuration][`--experimental-sea-config`] for details.
865865

866+
867+
### `--experimental-task-ordering`
868+
869+
<!-- YAML
870+
added: REPLACEME
871+
-->
872+
873+
> Stability: 1 - Experimental
874+
875+
Enable experimental task ordering. Always drain micro task queue
876+
before running `process.nextTick` to avoid unintuitive behavior
877+
and unexpected logical deadlocks when mixing async callback and
878+
event API's with `Promise`, `async`/`await`` and `queueMicroTask`.
879+
866880
### `--experimental-shadow-realm`
867881

868882
<!-- YAML

lib/internal/process/task_queues.js

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ const {
4141

4242
const { AsyncResource } = require('async_hooks');
4343

44+
let experimentalTaskOrdering;
4445
// *Must* match Environment::TickInfo::Fields in src/env.h.
4546
const kHasTickScheduled = 0;
4647

@@ -55,16 +56,25 @@ function setHasTickScheduled(value) {
5556
const queue = new FixedQueue();
5657

5758
// Should be in sync with RunNextTicksNative in node_task_queue.cc
58-
function runNextTicks() {
59+
function runNextTicksNew() {
5960
if (!hasTickScheduled() && !hasRejectionToWarn())
6061
runMicrotasks();
6162
if (!hasTickScheduled() && !hasRejectionToWarn())
6263
return;
6364

64-
processTicksAndRejections();
65+
processTicksAndRejectionsNew();
6566
}
6667

67-
function processTicksAndRejections() {
68+
function runNextTicksOld() {
69+
if (!hasTickScheduled() && !hasRejectionToWarn())
70+
runMicrotasks();
71+
if (!hasTickScheduled() && !hasRejectionToWarn())
72+
return;
73+
74+
processTicksAndRejectionsOld();
75+
}
76+
77+
function processTicksAndRejectionsOld() {
6878
let tock;
6979
do {
7080
while ((tock = queue.shift()) !== null) {
@@ -98,6 +108,40 @@ function processTicksAndRejections() {
98108
setHasRejectionToWarn(false);
99109
}
100110

111+
function processTicksAndRejectionsNew() {
112+
let tock;
113+
do {
114+
runMicrotasks();
115+
while ((tock = queue.shift()) !== null) {
116+
const asyncId = tock[async_id_symbol];
117+
emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
118+
119+
try {
120+
const callback = tock.callback;
121+
if (tock.args === undefined) {
122+
callback();
123+
} else {
124+
const args = tock.args;
125+
switch (args.length) {
126+
case 1: callback(args[0]); break;
127+
case 2: callback(args[0], args[1]); break;
128+
case 3: callback(args[0], args[1], args[2]); break;
129+
case 4: callback(args[0], args[1], args[2], args[3]); break;
130+
default: callback(...args);
131+
}
132+
}
133+
} finally {
134+
if (destroyHooksExist())
135+
emitDestroy(asyncId);
136+
}
137+
138+
emitAfter(asyncId);
139+
}
140+
} while (!queue.isEmpty() || processPromiseRejections());
141+
setHasTickScheduled(false);
142+
setHasRejectionToWarn(false);
143+
}
144+
101145
// `nextTick()` will not enqueue any callback when the process is about to
102146
// exit since the callback would not have a chance to be executed.
103147
function nextTick(callback) {
@@ -160,13 +204,23 @@ function queueMicrotask(callback) {
160204

161205
module.exports = {
162206
setupTaskQueue() {
207+
if (experimentalTaskOrdering === undefined) {
208+
const { getOptionValue } = require('internal/options');
209+
experimentalTaskOrdering = getOptionValue('--experimental-task-ordering');
210+
}
211+
163212
// Sets the per-isolate promise rejection callback
164213
listenForRejections();
165214
// Sets the callback to be run in every tick.
166-
setTickCallback(processTicksAndRejections);
215+
setTickCallback(experimentalTaskOrdering
216+
? processTicksAndRejectionsNew
217+
: processTicksAndRejectionsOld
218+
);
167219
return {
168220
nextTick,
169-
runNextTicks,
221+
runNextTicks: experimentalTaskOrdering
222+
? runNextTicksNew
223+
: runNextTicksOld,
170224
};
171225
},
172226
queueMicrotask,

0 commit comments

Comments
 (0)