Skip to content

[WIP] src: only block on user blocking worker tasks #58047

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

joyeecheung
Copy link
Member

src: only block on user blocking worker tasks

Previously, we block on all worker tasks in the main thread,
which can cause a deadlock if the worker task post another
foreground task and wait for it to complete. This patch changes
it to only block on worker tasks that have kUserBlocking priority
to avoid the deadlock.

In addition this updates the task queue to be a priority queue
and order the task based on priority, so that higher priority
tasks get executed first, as the documentation suggests.

Refs: #47452
Fixes: #54918

Previously, we block on all worker tasks in the main thread,
which can cause a deadlock if the worker task post another
foreground task and wait for it to complete. This patch changes
it to only block on worker tasks that have kUserBlocking priority
to avoid the deadlock.

In addition this updates the task queue to be a priority queue
and order the task based on priority, so that higher priority
tasks get executed first, as the documentation suggests.
@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run. labels Apr 27, 2025
@joyeecheung
Copy link
Member Author

joyeecheung commented Apr 27, 2025

This seems to fix #54918 - I started two stress test jobs before opening this PR:

And the CI seems to be happy about it: https://ci.nodejs.org/job/node-test-commit/79439/

I am being somewhat lazy here to achieve the "only block on user blocking worker tasks" but only counting them as outstanding tasks. Need another look to make sure I am not throwing other tasks into the queue in the delayed scheduler. Also I am just speculating that worker tasks that would post another foreground task shouldn't be kUserBlocking, or at least I haven't found any so far (the ones that are causing the deadlocks that I can reproduce are just kUserVisible GC tasks). May need to confirm this.

@lpinca
Copy link
Member

lpinca commented Apr 27, 2025

I confirm that this patch fixes the issue on my machine.

} else {
fprintf(stderr, " <no location>\n");
}
// DumpNativeBacktrace(stderr);
Copy link
Contributor

Choose a reason for hiding this comment

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

We probably don't want to keep those

Suggested change
// DumpNativeBacktrace(stderr);
Copy link
Member

@Qard Qard left a comment

Choose a reason for hiding this comment

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

Amazing work, Joyee! So glad to see this one finally resolved. 👏🏻

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
c++ Issues and PRs that require attention from people who are familiar with C++. lib / src Issues and PRs related to general changes in the lib or src directory. needs-ci PRs that need a full CI run.
5 participants