Skip to content

Fix iOS crash when a queued view command runs after the renderer scheduler is released - #60517

Open
Pajn wants to merge 1 commit into
react:mainfrom
Pajn:rctscheduler-keep-delegate-proxy-alive
Open

Pajn wants to merge 1 commit into
react:mainfrom
Pajn:rctscheduler-keep-delegate-proxy-alive

Conversation

@Pajn

@Pajn Pajn commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Summary:

iOS apps on the New Architecture crash with EXC_BAD_ACCESS on the JS thread when a view command or transaction is still queued as the renderer's scheduler is released. Production reports in #58601 come mostly from swipe-to-kill, where RCTSurfacePresenter suspends from its UIApplicationWillTerminateNotification observer. #59754 closed the reload path by keeping the scheduler alive through instance shutdown, but termination releases it directly and is still open on main.

The cause is ownership. Rendering updates queued by Scheduler capture the iOS SchedulerDelegateProxy by raw pointer, and RCTScheduler freed that proxy in dealloc on the main thread. This change, confined to RCTScheduler.mm, makes the proxy outlive every update that captured it:

  1. The proxy holds its RCTSchedulerDelegate weakly, and dealloc clears it, so a late call is a no-op.
  2. dealloc gives the last reference to the proxy to a task on the runtime scheduler, which schedules a second task that holds it. A rendering update runs at the end of the tick that queued it, or of the next tick when it was queued outside one. A task error discards pending updates together with queued tasks. So by the time the second task runs, every update that captured the proxy has run or been dropped.

The release now happens on the JS thread, after the queued updates:

main thread                          JS thread
RCTScheduler dealloc
  proxy.delegate = nil
  schedule task A  ────────────────▶ task A: schedule task B
                                     end of tick: queued updates call the proxy, which forwards to nil
                                     task B: releases the proxy

Nothing is locked per update, no flag is checked, and the main thread never waits on the JS thread. Because the fix is in dealloc, it covers every way the scheduler is released, not only termination. Android is unaffected: FabricUIManagerBinding is the delegate there and outlives the Scheduler.

One narrow window stays out of scope. UIManager calls into Scheduler through a pointer that ~Scheduler clears on the main thread, so a view command already past that null check on the JS thread could still reach a Scheduler being destroyed. It is a few instructions wide and has not shown up in any reproduction.

Fixes #58601

Changelog:

[IOS] [FIXED] - Fix a crash when a queued view command or transaction runs after the renderer scheduler is released, such as when the app is terminated

Test Plan:

RCTSchedulerTests builds a real RCTScheduler on a Hermes runtime with a test-controlled JS queue. It dispatches a view command, so a rendering update captures the delegate proxy, releases the scheduler, then runs the next event loop tick.

  • Before: the queued command reads the freed proxy. Under Address Sanitizer, testQueuedCommandIsDroppedAfterSchedulerIsReleased fails:
    ERROR: AddressSanitizer: heap-use-after-free
      #0 Scheduler::uiManagerDidDispatchCommand(...)::$_0::operator()()
    freed by thread T0 here:
      #1 SchedulerDelegateProxy::~SchedulerDelegateProxy()
    
    After: the command is dropped without reaching the delegate. Both RCTSchedulerTests cases and the existing AppleEventBeatTests pass under Address Sanitizer and UBSan.

The React-RCTFabric test spec isn't part of RNTester's pods, so these ran with :testspecs => ['Tests'] added to the React-RCTFabric pod and the spec's sources limited to Tests/Fabric. Outside that folder, RCTSelectableParagraphComponentViewTests.mm does not compile in the CocoaPods build:

xcodebuild test -workspace RNTesterPods.xcworkspace -scheme React-RCTFabric-Unit-Tests \
  -destination 'platform=iOS Simulator,name=iPhone 17' \
  -enableAddressSanitizer YES -enableUndefinedBehaviorSanitizer YES \
  -only-testing:React-RCTFabric-Unit-Tests/RCTSchedulerTests \
  -only-testing:React-RCTFabric-Unit-Tests/AppleEventBeatTests

The same change, applied to earlier releases, was measured in the issue:

Merge Danger:

Door: two-way. The change is one file plus a test and reverts cleanly.

Blast Radius: iOS renderer. A mistake here would show up as missed or late view commands and transactions on iOS, or a leaked proxy per released scheduler if the runtime scheduler never runs again, for example when the process exits.

@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 Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Visual regression

No visual differences at fb7d3636b01a.

android: no capture

…duler is released

Rendering updates that Scheduler queues on the JS thread, such as a view
command or a finished transaction, call the iOS SchedulerDelegateProxy
through a raw pointer. RCTScheduler owned that proxy and freed it in
dealloc, which runs on the main thread when RCTSurfacePresenter suspends
on UIApplicationWillTerminateNotification. A JS tick that ran between
the release and process exit drained a stale update into freed memory.

The proxy now holds its RCTSchedulerDelegate weakly, so a late call is a
no-op once dealloc clears it, and the proxy itself outlives the updates
that captured it: dealloc hands the last reference to a task that
schedules a second task on the RuntimeScheduler. A rendering update runs
at the end of the tick that queued it, or of the next tick when queued
outside one, and a task error discards pending updates together with
queued tasks, so none can run after the proxy is released.

Fixes react#58601

Signed-off-by: Emma <817422+Pajn@users.noreply.github.com>
@Pajn
Pajn force-pushed the rctscheduler-keep-delegate-proxy-alive branch from 9705dc8 to fb7d363 Compare December 3, 2026 20:48

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

Meta Review Required This PR is considered risky and requires Meta to import it to do more testing before merging.

Projects

None yet

1 participant