Repository navigation
Conversation
|
@javache has exported this pull request. If you are a Meta employee, you can view the originating Diff in D117322680. |
Summary: The experiment for clearing the `RuntimeScheduler` queues before handling a task error has run its course, so make the behaviour the default for everyone. When a task throws, `RuntimeScheduler_Modern` now drops the pending task queue and any pending rendering updates before invoking the host error handler. That stops queued work from being executed against state the error handler is about to tear down. Turning this on for everyone exposed a bug in one of the call sites. A throwing resize-observer callback was routed through `handleTaskError`, which clears the queues - wrong inside the "update the rendering" step, because it drops the pending rendering updates that same step is about to drain, so a throwing observer callback aborts the remaining steps. That is exactly what the call site's own comment says must not happen. That path now reports the error without clearing. Queue clearing stays on the task-execution and microtask-checkpoint paths, which are the ones that unwind before the rest of the tick runs. The flag default flips to `true`, and the per-app runtime gates are removed so every consumer picks up the default rather than an override. Changelog: [General][Changed] - `RuntimeScheduler` now clears pending tasks and rendering updates when a task throws Differential Revision: D117322680
javache
force-pushed
the
export-D117322680
branch
from
August 25, 2026 11:23
dc7d3cd to
98ac763
Compare
|
This pull request has been merged in 14184ec. |
1 of 3 tasks
tvinhas
pushed a commit
to tvinhas/react-native-macos
that referenced
this pull request
Oct 21, 2026
Summary: Pull Request resolved: react#58129 The experiment for clearing the `RuntimeScheduler` queues before handling a task error has run its course, so make the behaviour the default for everyone. When a task throws, `RuntimeScheduler_Modern` now drops the pending task queue and any pending rendering updates before invoking the host error handler. That stops queued work from being executed against state the error handler is about to tear down. Turning this on for everyone exposed a bug in one of the call sites. A throwing resize-observer callback was routed through `handleTaskError`, which clears the queues - wrong inside the "update the rendering" step, because it drops the pending rendering updates that same step is about to drain, so a throwing observer callback aborts the remaining steps. That is exactly what the call site's own comment says must not happen. That path now reports the error without clearing. Queue clearing stays on the task-execution and microtask-checkpoint paths, which are the ones that unwind before the rest of the tick runs. The flag default flips to `true`, and the per-app runtime gates are removed so every consumer picks up the default rather than an override. Changelog: [General][Changed] - `RuntimeScheduler` now clears pending tasks and rendering updates when a task throws Reviewed By: christophpurrer Differential Revision: D117322680 fbshipit-source-id: e47de49b0fbb240023f8ee25bdb111a19b50a5b1
douglowder
added a commit
to expo/expo
that referenced
this pull request
Oct 26, 2026
…#51880) # Why On iOS, a fatal JS error in a React Native scheduler task can freeze the app instead of crashing it. The error never reaches the JS error handlers, so expo-app-metrics does not record it, and expo-updates error recovery does not run. The root cause is in React Native. After a JS error in a task, `RuntimeScheduler_Modern::clearQueues()` destroys the queued tasks while it holds `schedulingMutex_`. A task destructor that schedules work then calls `RuntimeScheduler_Modern::scheduleTask`, which waits for the same non-recursive mutex, and the JS thread deadlocks. The main thread then also waits for that mutex. This is reported upstream in react/react-native#59204. - react/react-native#56892 added the queue clearing behind a flag. - react/react-native#58129 and #58132 made it the default in React Native 0.88. `0.87-stable` does not have it. In Expo, the destructors that schedule work are the `deinit`s of `JavaScriptPromise` and `JavaScriptCallback`, which schedule the release of their state (#47521). # How This PR is part of a stack: 1. #51880 [jsi] Fix an iOS deadlock when a JS error clears the scheduler queues 2. #51881 [updates] Use millisecond log timestamps and correct error recovery codes 3. #51882 [observe] Add an expo-updates integration 4. #51883 [updates] Add a log entry listener registry 5. #51884 [observe] Report expo-updates log entries as they are logged This is PR 1. It is independent of the other PRs in the stack. The stack depends on it, because the end-to-end test of expo-updates error recovery on iOS freezes without it. This PR is a workaround in expo-modules-jsi until a React Native release fixes `clearQueues()`. When a promise or callback `deinit` runs on the JS thread, `Scheduler.scheduleRelease` no longer schedules from that thread: - It adds the release to a batch that a lock protects. - When the batch was empty, a private serial queue takes the batch and schedules one task that runs all its releases. That thread waits for the scheduler mutex until `clearQueues()` releases it, so no deadlock happens. Because the queue is serial, at most one thread waits for the mutex, and a batch needs one `scheduleTask` call instead of one for each value. - The batch is taken before its task is scheduled. So each release runs after a `resolve` or `reject` that was queued before its value was dropped, and the promise still finds its resolve and reject functions. Releasing the state at once on the JS thread would break this order. On other threads, and when the runtime has no host scheduler, the behavior does not change. Performance: a drop on the JS thread now adds the release to the batch, and the release thread wakes to schedule it. In a benchmark that drops about a million values per second on a runtime with a host scheduler, a drop costs about 0.5–0.8 µs more than on `main` (see the Test Plan). `DispatchQueue.global` was cheaper per drop, but it can start one thread for each blocked block during `clearQueues()`. The diff changes: - `JavaScriptRuntimeScheduler.swift`: `scheduleRelease`, the batch, and the release queue. - `JavaScriptPromise.swift` and `JavaScriptCallback.swift`: `deinit` uses `scheduleRelease`. - `TestRuntimeScheduler.swift`: test dispatch functions. One acts like React Native's mutex and counts a dispatch from the thread that holds the lock, which would deadlock. One counts the dispatches. - `SchedulerReentrancyTests.swift`: the new tests. - `Benchmarks/ReleaseBenchmarks.swift`: benchmarks with a host scheduler. The existing benchmarks use a standalone runtime, which never takes the new path. - A changeset for `expo-modules-jsi`. # Test Plan - `pnpm test:integration` in `packages/expo-modules-jsi`: 834 tests pass. The new tests: - A promise and a callback that are dropped on the JS thread while the emulated scheduler lock is held do not dispatch from that thread, and their state is released later. Both tests fail with the `deinit` code from `main`. - A promise that is resolved and then dropped on the JS thread still settles, and 100 such promises all settle. - 100 promises dropped together share release tasks. This test fails when each release has its own task. - `pnpm swift:lint` passes. - Benchmarks, Release, Mac Catalyst, median ns per operation: | Case | `main` | this PR (two runs) | |---|---|---| | `JavaScriptPromise(runtime)`: create deferred (standalone runtime) | 351.8 | 349.6, 359.8 | | `JavaScriptCallback.decode()`: create and drop (standalone runtime) | 149.3 | 153.5, 155.1 | | host scheduler: promise create and drop | 716.3 | 1554.0, 1438.9 | | host scheduler: promise create, resolve, and drop | 847.0 | 1494.2, 1353.3 | | host scheduler: callback decode and drop | 224.3 | 1220.4, 738.8 | The other promise and callback benchmarks match `main` within noise. The host scheduler cases drop their tasks, so they measure only the work on the JS thread. They differ by up to 40% between runs. - Release build of `apps/observe-tester` on the iPhone 17 simulator (iOS 26.5), with the integration from #51882. A sample throws a JS error in a `setTimeout` callback soon after launch. - Without a fix, the app froze. A `sample` of the process showed the JS thread in `RuntimeScheduler_Modern::clearQueues()`, then `JavaScriptPromise.deinit`, then `RuntimeScheduler_Modern::scheduleTask`, waiting for the mutex. - With an earlier version of this fix (one background dispatch per release, before batching), the app crashed. On the next launch, expo-app-metrics recorded the fatal `js.exception`, and the expo-updates log had the error recovery entry. I did not run this end-to-end test again with the batched version. The unit tests use an emulated lock, so this manual run is the only check against the real scheduler. - Android is not affected, because the change is Swift only. # Screenshots No UI change. # Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
tsapeta
pushed a commit
to expo/expo
that referenced
this pull request
Oct 28, 2026
…#51880) # Why On iOS, a fatal JS error in a React Native scheduler task can freeze the app instead of crashing it. The error never reaches the JS error handlers, so expo-app-metrics does not record it, and expo-updates error recovery does not run. The root cause is in React Native. After a JS error in a task, `RuntimeScheduler_Modern::clearQueues()` destroys the queued tasks while it holds `schedulingMutex_`. A task destructor that schedules work then calls `RuntimeScheduler_Modern::scheduleTask`, which waits for the same non-recursive mutex, and the JS thread deadlocks. The main thread then also waits for that mutex. This is reported upstream in react/react-native#59204. - react/react-native#56892 added the queue clearing behind a flag. - react/react-native#58129 and #58132 made it the default in React Native 0.88. `0.87-stable` does not have it. In Expo, the destructors that schedule work are the `deinit`s of `JavaScriptPromise` and `JavaScriptCallback`, which schedule the release of their state (#47521). # How This PR is part of a stack: 1. #51880 [jsi] Fix an iOS deadlock when a JS error clears the scheduler queues 2. #51881 [updates] Use millisecond log timestamps and correct error recovery codes 3. #51882 [observe] Add an expo-updates integration 4. #51883 [updates] Add a log entry listener registry 5. #51884 [observe] Report expo-updates log entries as they are logged This is PR 1. It is independent of the other PRs in the stack. The stack depends on it, because the end-to-end test of expo-updates error recovery on iOS freezes without it. This PR is a workaround in expo-modules-jsi until a React Native release fixes `clearQueues()`. When a promise or callback `deinit` runs on the JS thread, `Scheduler.scheduleRelease` no longer schedules from that thread: - It adds the release to a batch that a lock protects. - When the batch was empty, a private serial queue takes the batch and schedules one task that runs all its releases. That thread waits for the scheduler mutex until `clearQueues()` releases it, so no deadlock happens. Because the queue is serial, at most one thread waits for the mutex, and a batch needs one `scheduleTask` call instead of one for each value. - The batch is taken before its task is scheduled. So each release runs after a `resolve` or `reject` that was queued before its value was dropped, and the promise still finds its resolve and reject functions. Releasing the state at once on the JS thread would break this order. On other threads, and when the runtime has no host scheduler, the behavior does not change. Performance: a drop on the JS thread now adds the release to the batch, and the release thread wakes to schedule it. In a benchmark that drops about a million values per second on a runtime with a host scheduler, a drop costs about 0.5–0.8 µs more than on `main` (see the Test Plan). `DispatchQueue.global` was cheaper per drop, but it can start one thread for each blocked block during `clearQueues()`. The diff changes: - `JavaScriptRuntimeScheduler.swift`: `scheduleRelease`, the batch, and the release queue. - `JavaScriptPromise.swift` and `JavaScriptCallback.swift`: `deinit` uses `scheduleRelease`. - `TestRuntimeScheduler.swift`: test dispatch functions. One acts like React Native's mutex and counts a dispatch from the thread that holds the lock, which would deadlock. One counts the dispatches. - `SchedulerReentrancyTests.swift`: the new tests. - `Benchmarks/ReleaseBenchmarks.swift`: benchmarks with a host scheduler. The existing benchmarks use a standalone runtime, which never takes the new path. - A changeset for `expo-modules-jsi`. # Test Plan - `pnpm test:integration` in `packages/expo-modules-jsi`: 834 tests pass. The new tests: - A promise and a callback that are dropped on the JS thread while the emulated scheduler lock is held do not dispatch from that thread, and their state is released later. Both tests fail with the `deinit` code from `main`. - A promise that is resolved and then dropped on the JS thread still settles, and 100 such promises all settle. - 100 promises dropped together share release tasks. This test fails when each release has its own task. - `pnpm swift:lint` passes. - Benchmarks, Release, Mac Catalyst, median ns per operation: | Case | `main` | this PR (two runs) | |---|---|---| | `JavaScriptPromise(runtime)`: create deferred (standalone runtime) | 351.8 | 349.6, 359.8 | | `JavaScriptCallback.decode()`: create and drop (standalone runtime) | 149.3 | 153.5, 155.1 | | host scheduler: promise create and drop | 716.3 | 1554.0, 1438.9 | | host scheduler: promise create, resolve, and drop | 847.0 | 1494.2, 1353.3 | | host scheduler: callback decode and drop | 224.3 | 1220.4, 738.8 | The other promise and callback benchmarks match `main` within noise. The host scheduler cases drop their tasks, so they measure only the work on the JS thread. They differ by up to 40% between runs. - Release build of `apps/observe-tester` on the iPhone 17 simulator (iOS 26.5), with the integration from #51882. A sample throws a JS error in a `setTimeout` callback soon after launch. - Without a fix, the app froze. A `sample` of the process showed the JS thread in `RuntimeScheduler_Modern::clearQueues()`, then `JavaScriptPromise.deinit`, then `RuntimeScheduler_Modern::scheduleTask`, waiting for the mutex. - With an earlier version of this fix (one background dispatch per release, before batching), the app crashed. On the next launch, expo-app-metrics recorded the fatal `js.exception`, and the expo-updates log had the error recovery entry. I did not run this end-to-end test again with the batched version. The unit tests use an emulated lock, so this manual run is the only check against the real scheduler. - Android is not affected, because the change is Swift only. # Screenshots No UI change. # Checklist - [x] I added a changeset and followed [this short guide](https://github.com/expo/expo/blob/main/CONTRIBUTING.md#-before-submitting) - [ ] This diff will work correctly for `npx expo prebuild` & EAS Build (eg: updated a module plugin). - [ ] Conforms with the [Documentation Writing Style Guide](https://github.com/expo/expo/blob/main/guides/Expo%20Documentation%20Writing%20Style%20Guide.md) 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com> (cherry picked from commit 5599fba)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary:
The experiment for clearing the
RuntimeSchedulerqueues before handling a taskerror has run its course, so make the behaviour the default for everyone.
When a task throws,
RuntimeScheduler_Modernnow drops the pending task queueand any pending rendering updates before invoking the host error handler. That
stops queued work from being executed against state the error handler is about
to tear down.
Turning this on for everyone exposed a bug in one of the call sites. A throwing
resize-observer callback was routed through
handleTaskError, which clears thequeues - wrong inside the "update the rendering" step, because it drops the
pending rendering updates that same step is about to drain, so a throwing
observer callback aborts the remaining steps. That is exactly what the call
site's own comment says must not happen. That path now reports the error without
clearing. Queue clearing stays on the task-execution and microtask-checkpoint
paths, which are the ones that unwind before the rest of the tick runs.
The flag default flips to
true, and the per-app runtime gates are removed soevery consumer picks up the default rather than an override.
Changelog:
[General][Changed] -
RuntimeSchedulernow clears pending tasks and rendering updates when a task throwsDifferential Revision: D117322680