src: fix task queue deadlock when built as C++23 - #66066
Open
codebytere wants to merge 1 commit into
Open
codebytere wants to merge 1 commit into
codebytere wants to merge 1 commit into
Conversation
Since nodejs#65353, FlushForegroundTasksInternal() and the DelayedTaskScheduler drain their queues with `for (auto& task : queue.Lock().PopAll())`. In C++20 the Locked temporary is destroyed at the end of the range initializer, before the loop body runs. C++23 (P2718R0) extends the lifetime of every temporary in a range-for initializer to the end of the loop, so the queue's mutex stays held while the tasks run, and the first task that posts to the same queue - any V8 foreground task that schedules another one - deadlocks on the non-recursive mutex. Node.js itself builds with -std=gnu++20, but an embedder that compiles it as C++23 hangs in the first foreground task flush. Store the drained tasks in a local before iterating so the lock is released independent of the language version. Refs: nodejs#65353 Signed-off-by: Shelley Vohr <shelley.vohr@gmail.com>
codebytere
force-pushed
the
platform-popall-lock-scope
branch
from
September 16, 2026 13:22
bb29cbb to
8e9431e
Compare
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #66066 +/- ##
==========================================
- Coverage 90.27% 90.26% -0.01%
==========================================
Files 789 789
Lines 271473 271475 +2
Branches 51808 51806 -2
==========================================
- Hits 245066 245045 -21
- Misses 16880 16897 +17
- Partials 9527 9533 +6
🚀 New features to boost your workflow:
|
gurgunday
approved these changes
Sep 16, 2026
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.
Refs: #65353
Since #65353,
FlushForegroundTasksInternal()and the workerDelayedTaskSchedulerdrain their queues withfor (auto& task : queue.Lock().PopAll()). In C++20 theLockedtemporary is destroyed at the end of the range initializer, before the loop body runs. C++23 (P2718R0) extends the lifetime of every temporary in a range-for initializer to the end of the loop, so the queue's mutex stays held while the tasks run, and the first task that posts to the same queue - any V8 foreground task that schedules another one - deadlocks on the non-recursive mutex. Node.js itself builds with-std=gnu++20and is unaffected today, but an embedder that compiles it as C++23 hangs in the first foreground task flush, and so will Node.js oncecommon.gypimoves on.This stores the drained tasks in a local before iterating at the three sites, so the lock is released independent of the language version, and notes the constraint on
PopAll(). No new test:PlatformTest.SkipNewTasksInFlushForegroundTasksalready posts a foreground task from inside a flushed one and deadlocks under a C++23 build without this change; verified with a clang 20-std=gnu++23build ofmainbefore (hangs intest-worker-arraybuffer-zerofilland that cctest) and after (passes), plus the regular gcc C++20 build.Disclosure: the code and this description were written by Claude Code, directed and reviewed by @codebytere.