Skip to content

Commit 3181abc

Browse files
hcbylmzmeta-codesync[bot]
authored andcommitted
Fix deadlock in RuntimeScheduler clearQueues when a task destructor schedules work (#59298)
Summary: Fixes #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: #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
1 parent 2b52147 commit 3181abc

2 files changed

Lines changed: 45 additions & 1 deletion

File tree

‎packages/react-native/ReactCommon/react/renderer/runtimescheduler/RuntimeScheduler_Modern.cpp‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -504,9 +504,11 @@ void RuntimeScheduler_Modern::handleTaskError(
504504
}
505505

506506
void RuntimeScheduler_Modern::clearQueues() {
507+
// Destroyed after the lock is released: task destructors may schedule work.
508+
decltype(taskQueue_) drainedTasks;
507509
{
508510
std::unique_lock lock(schedulingMutex_);
509-
taskQueue_ = {};
511+
drainedTasks.swap(taskQueue_);
510512
isEventLoopScheduled_ = false;
511513
}
512514

‎packages/react-native/ReactCommon/react/renderer/runtimescheduler/tests/RuntimeSchedulerTest.cpp‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,48 @@ TEST_P(RuntimeSchedulerTest, clearsQueuesOnError) {
946946
EXPECT_EQ(stubErrorUtils_->getReportFatalCallCount(), 1);
947947
}
948948

949+
TEST_P(RuntimeSchedulerTest, clearQueuesAllowsTaskDestructorsToScheduleTasks) {
950+
// Only for event loop
951+
if (!GetParam()) {
952+
return;
953+
}
954+
955+
bool didRunScheduledFromDestructor = false;
956+
957+
auto scheduleTask = [&]() {
958+
runtimeScheduler_->scheduleTask(
959+
SchedulerPriority::NormalPriority, [&](jsi::Runtime& /*runtime*/) {
960+
didRunScheduledFromDestructor = true;
961+
});
962+
};
963+
// Schedules a task when it is destroyed.
964+
auto scheduleOnDestroy =
965+
std::make_shared<OnScopeExit<decltype(scheduleTask)>>(
966+
std::move(scheduleTask));
967+
968+
auto throwingCallback =
969+
createHostFunctionFromLambda([&](bool /*unused*/) -> jsi::Value {
970+
throw jsi::JSError(*runtime_, "Test error");
971+
});
972+
973+
runtimeScheduler_->scheduleTask(
974+
SchedulerPriority::ImmediatePriority, std::move(throwingCallback));
975+
runtimeScheduler_->scheduleTask(
976+
SchedulerPriority::LowPriority,
977+
[scheduleOnDestroy =
978+
std::move(scheduleOnDestroy)](jsi::Runtime& /*runtime*/) {});
979+
980+
EXPECT_EQ(stubQueue_->size(), 1);
981+
982+
// The throwing task clears the queues, which destroys the low priority task
983+
// and runs its capture's destructor. That must not deadlock, and the task it
984+
// schedules must still run.
985+
stubQueue_->tick();
986+
987+
EXPECT_TRUE(didRunScheduledFromDestructor);
988+
EXPECT_EQ(stubErrorUtils_->getReportFatalCallCount(), 1);
989+
}
990+
949991
TEST_P(RuntimeSchedulerTest, basicSameThreadExecution) {
950992
bool didRunSynchronousTask = false;
951993
std::thread t1([this, &didRunSynchronousTask]() {

0 commit comments

Comments
 (0)