mirror of
https://github.com/facebook/react-native.git
synced 2026-11-18 20:19:07 +00:00
Fix deadlock in RuntimeScheduler clearQueues when a task destructor schedules work (#59298)
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
This commit is contained in:
1 parent
2b521471b4
commit
3181abc5c9
2 files changed
+45
-1
No files matched your search
+3
-1
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
+42
@@ -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<OnScopeExit<decltype(scheduleTask)>>(
|
||||
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]() {
|
||||
|
||||
Reference in new issue
Block a user