Repository navigation
Commit 3181abc
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: f66a3109a021c02c5ecf47f799a8605249afc7cb1 parent 2b52147 commit 3181abc
2 files changed
Lines changed: 45 additions & 1 deletion
File tree
- packages/react-native/ReactCommon/react/renderer/runtimescheduler
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
504 | 504 | | |
505 | 505 | | |
506 | 506 | | |
| 507 | + | |
| 508 | + | |
507 | 509 | | |
508 | 510 | | |
509 | | - | |
| 511 | + | |
510 | 512 | | |
511 | 513 | | |
512 | 514 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
946 | 946 | | |
947 | 947 | | |
948 | 948 | | |
| 949 | + | |
| 950 | + | |
| 951 | + | |
| 952 | + | |
| 953 | + | |
| 954 | + | |
| 955 | + | |
| 956 | + | |
| 957 | + | |
| 958 | + | |
| 959 | + | |
| 960 | + | |
| 961 | + | |
| 962 | + | |
| 963 | + | |
| 964 | + | |
| 965 | + | |
| 966 | + | |
| 967 | + | |
| 968 | + | |
| 969 | + | |
| 970 | + | |
| 971 | + | |
| 972 | + | |
| 973 | + | |
| 974 | + | |
| 975 | + | |
| 976 | + | |
| 977 | + | |
| 978 | + | |
| 979 | + | |
| 980 | + | |
| 981 | + | |
| 982 | + | |
| 983 | + | |
| 984 | + | |
| 985 | + | |
| 986 | + | |
| 987 | + | |
| 988 | + | |
| 989 | + | |
| 990 | + | |
949 | 991 | | |
950 | 992 | | |
951 | 993 | | |
| |||
0 commit comments