Skip to content

Fix use-after-free of SchedulerDelegate in queued rendering updates on reload - #60325

Open
yspreen wants to merge 1 commit into
react:mainfrom
yspreen:fix-scheduler-delegate-uaf-on-reload
Open

yspreen wants to merge 1 commit into
react:mainfrom
yspreen:fix-scheduler-delegate-uaf-on-reload

Conversation

@yspreen

@yspreen yspreen commented Nov 27, 2026

Copy link
Copy Markdown

Summary:

Fixes #60324.

Scheduler queues rendering updates on RuntimeScheduler_Modern (from uiManagerDidFinishTransaction, uiManagerDidDispatchCommand and uiManagerDidFinishReactCommit) that capture the SchedulerDelegate as a raw pointer. On iOS the delegate is SchedulerDelegateProxy, owned by RCTScheduler. When an instance is torn down for a reload, -[RCTScheduler dealloc] calls _scheduler->setDelegate(nullptr) and frees the proxy on the main thread, while the old JS thread can still be in runEventLoopTick. updateRendering() then drains the queued lambda into freed memory, and the app crashes with EXC_BAD_ACCESS in RuntimeScheduler_Modern::runEventLoopTick. We see this in production after expo-updates' reloadAsync(); the stack trace is in the issue.

This is the window that #58138 left open on purpose: a delegate detached with no JS error never reaches handleTaskError, so nothing clears the queue. That PR notes that closing it needs more than the removed per-Scheduler token. A token that is only flipped also leaves a check-then-call race, because the host can free the delegate while a lambda is between its check and its call.

This change routes the queued updates through a DelegateBox, shared by Scheduler and its lambdas and guarded by a mutex:

  • A lambda calls the delegate it was queued for only while box->delegate is still that delegate. A replaced or detached delegate is never called, which also keeps the old "updates go to the delegate they were queued for" behavior on a swap.
  • setDelegate updates the box under the same mutex, so it waits for a lambda that is currently calling the delegate. When setDelegate(nullptr) returns, the host can free the delegate (which is what RCTScheduler does).
  • The mutex is recursive because the delegate call can drop the last reference to the host on the JS thread (e.g. the strong RCTScheduler local in -[RCTSurfacePresenter _mergeReactRevisionForSurfaceId:]), which re-enters setDelegate on the same thread.
  • ~Scheduler deliberately does not take the mutex. Android destroys the Scheduler inside uninstallFabricUIManager while holding installMutex_ exclusively, and the delegate calls there take it shared (getMountingManager), so waiting there could deadlock. Only iOS calls setDelegate. On that path no lock the queued calls need is held: -[RCTSurfacePresenter suspend] holds _schedulerLifeCycleMutex, and scheduleTransaction/dispatchCommand dispatch to main asynchronously. Android's delegate is the binding itself and outlives the Scheduler.

The cost is one uncontended mutex lock per drained rendering update, on the JS thread.

Changelog:

[IOS] [FIXED] - Fix a crash (EXC_BAD_ACCESS in RuntimeScheduler_Modern::runEventLoopTick) when the React instance is reloaded while rendering updates are still queued

Test Plan:

SchedulerDelegateInvalidationTest, built and run on macOS with the OSS Fantom CMake build (./gradlew :private:react-native-fantom:configureFantomTester, then cmake --build ... --target react_renderer_scheduler_tests). It compiles with -Wall -Werror -Wpedantic.

  • Test 3 is now the regression test (DelegateDetachedWithoutError_PendingRenderingUpdatesAreDropped). It queues updates from both the transaction and dispatch-command sites, detaches the delegate, drains, and expects no call.
  • New DelegateDetach_WaitsForRunningRenderingUpdate: while a queued update is inside the delegate call on one thread, setDelegate(nullptr) on another thread does not return until the call finishes.
  • New DelegateDetach_FromInsideRenderingUpdate: detaching from inside the delegate call on the JS thread doesn't deadlock, and the next queued update is dropped.

Without the change (Scheduler.cpp/.h reverted, tests kept), all three fail: the stale delegate is called, setDelegate returns while the delegate runs (detached == true), and the second update calls the detached delegate (calls == 2). With the change, all 8 tests in the target pass, over three consecutive runs.

Rendering updates that Scheduler queues on RuntimeScheduler captured the
SchedulerDelegate as a raw pointer. When a host detaches and destroys the
delegate with no JS error involved (an iOS instance reload: RCTScheduler's
dealloc calls setDelegate(nullptr) and frees SchedulerDelegateProxy), the old
JS thread can still drain those updates in runEventLoopTick and call into the
freed proxy.

Queued updates now reach the delegate through a shared box guarded by a
recursive mutex: an update calls the delegate it was queued for only while it
is still the current one, and setDelegate waits for an update that is calling
it. Fixes react#60324.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Nov 27, 2026
@github-actions github-actions Bot added the Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. label Nov 27, 2026
@facebook-github-tools facebook-github-tools Bot added the Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team. label Nov 27, 2026

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging. Shared with Meta Applied via automation to indicate that an Issue or Pull Request has been shared with the team.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

iOS: EXC_BAD_ACCESS in RuntimeScheduler_Modern::runEventLoopTick on reload — queued rendering update calls the freed SchedulerDelegate

1 participant