From 3181abc5c9b0143090125d726e42aee4269d10b4 Mon Sep 17 00:00:00 2001 From: Hicabi Date: Wed, 21 Oct 2026 09:55:59 -0700 Subject: [PATCH] Fix deadlock in RuntimeScheduler clearQueues when a task destructor schedules work (#59298) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Summary: Fixes https://github.com/react/react-native/issues/59204. `RuntimeScheduler_Modern::clearQueues()` destroys the queued tasks while it holds `schedulingMutex_`. A task owns its callback, so this also destroys everything the callback captured. If one of those captures schedules a task in its destructor, `scheduleTask()` tries to lock the same non-recursive mutex on the same thread, and the JS thread deadlocks. `clearQueues()` runs from `handleTaskError()`, so a single uncaught error in a task is enough to trigger it when another queued task has such a capture. The fix is the one fregster proposed in the issue: swap the queue out under the lock, and let the drained tasks be destroyed after the lock is released. A task scheduled from one of those destructors goes into the now-empty queue and still runs. ## Changelog: [GENERAL] [FIXED] - Fix deadlock in `RuntimeScheduler` when a task destructor schedules work while the queues are cleared after an error Pull Request resolved: https://github.com/react/react-native/pull/59298 Test Plan: Added `clearQueuesAllowsTaskDestructorsToScheduleTasks` to `RuntimeSchedulerTest`. It queues a task whose callback captures an object that schedules a task when destroyed, then runs a task that throws. - Without the fix: the test hangs. The stuck thread is in `std::shared_mutex::lock` called from `RuntimeScheduler_Modern::scheduleTask`, called from `Task::~Task`, called from `clearQueues` → `handleTaskError`. - With the fix: all 88 `RuntimeSchedulerTest` tests pass. I ran the tests on macOS (arm64) with a standalone CMake + googletest build of `RuntimeSchedulerTest.cpp` and its dependencies, linked against the Hermes macOS framework. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Reviewed By: rubennorte Differential Revision: D126662928 Pulled By: javache fbshipit-source-id: f66a3109a021c02c5ecf47f799a8605249afc7cb --- .../RuntimeScheduler_Modern.cpp | 4 +- .../tests/RuntimeSchedulerTest.cpp | 42 +++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp index 296ea85ca9d..791a4f3a8d0 100644 --- a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp +++ b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp @@ -504,9 +504,11 @@ void RuntimeScheduler_Modern::handleTaskError( } void RuntimeScheduler_Modern::clearQueues() { + // Destroyed after the lock is released: task destructors may schedule work. + decltype(taskQueue_) drainedTasks; { std::unique_lock lock(schedulingMutex_); - taskQueue_ = {}; + drainedTasks.swap(taskQueue_); isEventLoopScheduled_ = false; } diff --git a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp index c264972043c..5d1d3499804 100644 --- a/packages/react-native/ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp +++ b/packages/react-native/ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp @@ -946,6 +946,48 @@ TEST_P(RuntimeSchedulerTest, clearsQueuesOnError) { EXPECT_EQ(stubErrorUtils_->getReportFatalCallCount(), 1); } +TEST_P(RuntimeSchedulerTest, clearQueuesAllowsTaskDestructorsToScheduleTasks) { + // Only for event loop + if (!GetParam()) { + return; + } + + bool didRunScheduledFromDestructor = false; + + auto scheduleTask = [&]() { + runtimeScheduler_->scheduleTask( + SchedulerPriority::NormalPriority, [&](jsi::Runtime& /*runtime*/) { + didRunScheduledFromDestructor = true; + }); + }; + // Schedules a task when it is destroyed. + auto scheduleOnDestroy = + std::make_shared>( + std::move(scheduleTask)); + + auto throwingCallback = + createHostFunctionFromLambda([&](bool /*unused*/) -> jsi::Value { + throw jsi::JSError(*runtime_, "Test error"); + }); + + runtimeScheduler_->scheduleTask( + SchedulerPriority::ImmediatePriority, std::move(throwingCallback)); + runtimeScheduler_->scheduleTask( + SchedulerPriority::LowPriority, + [scheduleOnDestroy = + std::move(scheduleOnDestroy)](jsi::Runtime& /*runtime*/) {}); + + EXPECT_EQ(stubQueue_->size(), 1); + + // The throwing task clears the queues, which destroys the low priority task + // and runs its capture's destructor. That must not deadlock, and the task it + // schedules must still run. + stubQueue_->tick(); + + EXPECT_TRUE(didRunScheduledFromDestructor); + EXPECT_EQ(stubErrorUtils_->getReportFatalCallCount(), 1); +} + TEST_P(RuntimeSchedulerTest, basicSameThreadExecution) { bool didRunSynchronousTask = false; std::thread t1([this, &didRunSynchronousTask]() {