Skip to content

Fix JS timers stalling after app lifecycle transitions on iOS - #58976

Open
almeidagabriel01 wants to merge 1 commit into
react:mainfrom
almeidagabriel01:fix/rcttiming-lifecycle-on-timing-thread
Open

almeidagabriel01 wants to merge 1 commit into
react:mainfrom
almeidagabriel01:fix/rcttiming-lifecycle-on-timing-thread

Conversation

@almeidagabriel01

@almeidagabriel01 almeidagabriel01 commented Oct 9, 2026 •

Copy link
Copy Markdown

Summary

RCTTiming handles app lifecycle notifications (UIApplicationWillResignActive, DidEnterBackground, WillEnterForeground, DidBecomeActive) and proximity changes on the main thread, while every other method of the class runs on the thread that creates timers (the JS thread, through ObjCTimerRegistry).

Two consequences:

  1. appDidMoveToBackground → didUpdateFrame:nil → scheduleSleepTimer: installs the background NSTimer on the main run loop, in NSDefaultRunLoopMode only. While the main thread is busy or running another mode — for example during a Face ID prompt on launch — that timer does not fire. The JS thread cannot schedule its own: scheduleSleepTimer: sees a valid _sleepTimer and only moves its fire date. All JS timers (setTimeout, setInterval, requestAnimationFrame) stall.
  2. _paused and _inBackground are written from two threads without synchronization (stopTimers/startTimers on main vs. didUpdateFrame: on JS).

Measured on an iPhone 16 Pro Max (iOS 26, RN 0.86.3, bridgeless, release build), instrumenting RCTTiming during cold launches behind Face ID:

3.167 [main] WillResignActive → appDidMoveToBackground → scheduleSleepTimer: (fires in +0.022s)
3.440 [js]   scheduleSleepTimer: → only moves the fire date of the main-thread timer
4.199 [main] timerDidFire                      ← 1.03 s late; JS timers were frozen meanwhile

Fix

Forward lifecycle and proximity handling to the timers thread (-performOnTimingThread:), capturing its run loop when the first timer is created. Before that there is no timer to race with, so the handler runs in place (under @synchronized(self)). All timer state — and the sleep timer — now live on one thread.

Changelog:

[IOS] [FIXED] - JS timers stall after app lifecycle transitions (e.g. Face ID prompt) because RCTTiming handled them on the main thread

Test Plan

Measured and validated on RN 0.86.3 (the same diff, applied with patch-package in a production app); this PR ports it to main, where RCTTiming.mm lost the bridge paths but keeps the lifecycle handling unchanged.

  • Release build on device, 10 cold launches behind Face ID with RCTTiming instrumented: every appDidMoveTo*/startTimers/stopTimers/scheduleSleepTimer:/timerDidFire runs on the JS thread; no sleep timer overdue beyond a frame during the inactive phase.
  • Background/foreground and proximity transitions keep timers firing.

@meta-cla

meta-cla Bot commented Oct 9, 2026

Copy link
Copy Markdown

Hi @almeidagabriel01!

Thank you for your pull request and welcome to our community.

Action Required

In order to merge any pull request (code, docs, etc.), we require contributors to sign our Contributor License Agreement, and we don't seem to have one on file for you.

Process

In order for us to review and merge your suggested changes, please sign at https://code.facebook.com/cla. If you are contributing on behalf of someone else (eg your employer), the individual CLA may not be sufficient and your employer may need to sign the corporate CLA.

Once the CLA is signed, our tooling will perform checks and validations. Afterwards, the pull request will be tagged with CLA signed. The tagging process may take up to 1 hour after signing. Please give it that time before contacting us about it.

If you have received this in error or have any questions, please contact us at cla@meta.com. Thanks!

@meta-cla

meta-cla Bot commented Oct 9, 2026

Copy link
Copy Markdown

Thank you for signing our Contributor License Agreement. We can now accept your code for this (and any) Meta Open Source project. Thanks!

@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 Oct 9, 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 Oct 9, 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. 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.

1 participant