Skip to content

Commit d32a6ea

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: #51156 Refs: #51156 (comment) Refs: #51114 Refs: #51070 Refs: #51156 PR-URL: #51267
1 parent ee61c2c commit d32a6ea

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
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: 48 additions & 0 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

@@ -65,6 +66,19 @@ function runNextTicks() {
6566
}
6667

6768
function processTicksAndRejections() {
69+
if (experimentalTaskOrdering === undefined) {
70+
const { getOptionValue } = require('internal/options');
71+
experimentalTaskOrdering = getOptionValue('--experimental-task-ordering');
72+
}
73+
74+
if (experimentalTaskOrdering) {
75+
processTicksAndRejectionsNew();
76+
} else {
77+
processTicksAndRejectionsOld();
78+
}
79+
}
80+
81+
function processTicksAndRejectionsOld() {
6882
let tock;
6983
do {
7084
while ((tock = queue.shift()) !== null) {
@@ -98,6 +112,40 @@ function processTicksAndRejections() {
98112
setHasRejectionToWarn(false);
99113
}
100114

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

0 commit comments

Comments
 (0)