src: fix deadlock in NodePlatform::DrainTasks on process shutdown#61963
Open
RajeshKumar11 wants to merge 2 commits intonodejs:mainfrom
Open
src: fix deadlock in NodePlatform::DrainTasks on process shutdown#61963RajeshKumar11 wants to merge 2 commits intonodejs:mainfrom
RajeshKumar11 wants to merge 2 commits intonodejs:mainfrom
Conversation
Replace the unconditional BlockingDrain() in DrainTasks() with a 1ms timed drain loop that periodically flushes foreground tasks while waiting for outstanding kUserBlocking worker tasks to complete. This fixes a deadlock where a kUserBlocking worker task (e.g. Maglev JIT compilation) posts a foreground task and waits for it. If the foreground task is posted after the main thread flushes the foreground queue but before BlockingDrain() enters its indefinite sleep, both threads wait on each other — a mutual deadlock. With the timed drain, the main thread wakes every 1ms and flushes any foreground tasks that were posted in that window. In the common case, tasks complete before the timeout and the wait is woken immediately by NotifyOfOutstandingCompletion(), so there is no performance regression. Adds ConditionVariableBase::TimedWait() backed by uv_cond_timedwait() and TaskQueue::Locked::TimedBlockingDrain() to support this. Fixes: nodejs#54918
Fix clang-format alignment issues in node_mutex.h: - Split cond_timedwait parameters onto separate lines - Correct continuation alignment in TimedWait implementation
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #61963 +/- ##
==========================================
+ Coverage 89.75% 89.77% +0.02%
==========================================
Files 675 674 -1
Lines 204721 205619 +898
Branches 39344 39422 +78
==========================================
+ Hits 183751 184600 +849
- Misses 13230 13275 +45
- Partials 7740 7744 +4
🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #54918.
NodePlatform::DrainTasks()was callingBlockingDrain()which waitsindefinitely for outstanding
kUserBlockingworker tasks to complete.This creates a deadlock when:
kUserBlockingworker task (e.g. Maglev JIT compilation) completesits work and posts a foreground task, then waits for that foreground task
to run (e.g. via a
std::promise<>resolve).DrainTasks) flushes the foreground queue — missingthe newly-posted task because it was posted just after the flush.
BlockingDrain()'s indefinite sleep.Fix
Replace
BlockingDrain()withTimedBlockingDrain(1ms). On timeout, flushforeground tasks before retrying. This breaks the deadlock because the worker
task's foreground task will be flushed in the next 1ms window.
In the common (non-deadlock) case, tasks complete before the timeout and
NotifyOfOutstandingCompletion()wakes the wait immediately — noperformance regression.
New APIs added:
ConditionVariableBase::TimedWait()— wrapsuv_cond_timedwait()TaskQueue::Locked::TimedBlockingDrain()— timed version ofBlockingDrain()WorkerThreadsTaskRunner::TimedBlockingDrain()— delegates toTaskQueueTest
Stress-tested locally with 50 (and 1000) iterations of
test/parallel/test-http2-large-write-multiple-requests.js— all pass:Previously this test was a known reproducer for the deadlock on some machines.
Related
FIXME(54918)comment added in src: only block on user blocking worker tasks #58047@ronag in the issue — move the blocking wait off the hot path.