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

run microtasks before ticks #51267

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
14 changes: 14 additions & 0 deletions doc/api/cli.md
Original file line number Diff line number Diff line change
Expand Up @@ -863,6 +863,20 @@ Use this flag to generate a blob that can be injected into the Node.js
binary to produce a [single executable application][]. See the documentation
about [this configuration][`--experimental-sea-config`] for details.


### `--experimental-task-ordering`

<!-- YAML
added: REPLACEME
-->

> Stability: 1 - Experimental

Enable experimental task ordering. Always drain micro task queue
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Enable experimental task ordering. Always drain micro task queue
Enable experimental task ordering. Always drain Microtasks queue

It would also be useful to link to a doc like https://developer.mozilla.org/en-US/docs/Web/API/HTML_DOM_API/Microtask_guide#microtasks here.

before running `process.nextTick` to avoid unintuitive behavior
and unexpected logical deadlocks when mixing async callback and
event API's with `Promise`, `async`/`await`` and `queueMicroTask`.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you provide some examples? Without examples I doubt how many users would be able to recognize whether this can be useful for them and pick up this option.

Also note that we have a section in process.md saying..

every time the "next tick queue" is drained, the microtask queue
is drained immediately after.

I think that should be updated to mention this option.


### `--experimental-shadow-realm`

<!-- YAML
Expand Down
13 changes: 12 additions & 1 deletion lib/internal/process/task_queues.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ const {

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

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

Expand All @@ -65,8 +66,16 @@ function runNextTicks() {
}

function processTicksAndRejections() {
if (experimentalTaskOrdering === undefined) {
const { getOptionValue } = require('internal/options');
experimentalTaskOrdering = getOptionValue('--experimental-task-ordering');
}

let tock;
do {
if (experimentalTaskOrdering) {
runMicrotasks();
}
while ((tock = queue.shift()) !== null) {
const asyncId = tock[async_id_symbol];
emitBefore(asyncId, tock[trigger_async_id_symbol], tock);
Expand All @@ -92,7 +101,9 @@ function processTicksAndRejections() {

emitAfter(asyncId);
}
runMicrotasks();
if (!experimentalTaskOrdering) {
runMicrotasks();
}
} while (!queue.isEmpty() || processPromiseRejections());
setHasTickScheduled(false);
setHasRejectionToWarn(false);
Expand Down
Loading