From 90e48d6031de3188a71157fe22d80b138a8d7605 Mon Sep 17 00:00:00 2001 From: Samuel Newman Date: Mon, 5 Jan 2026 11:46:16 -0800 Subject: [PATCH] Capture idle callback before running it (#54984) Summary: Fixes https://github.com/facebook/react-native/issues/54983 https://github.com/facebook/react-native/issues/54983 has more details, but in short, this code here: ```kotlin currentIdleCallbackRunnable?.cancel() currentIdleCallbackRunnable = IdleCallbackRunnable(frameTimeNanos) reactApplicationContext.runOnJSQueueThread(currentIdleCallbackRunnable) ``` If `currentIdleCallbackRunnable` change between line 2 and line 3 (which is known to happen), the wrong runnable would be sent to be run. The fix is to simply capture it in a local variable before passing it to be run ```kotlin currentIdleCallbackRunnable?.cancel() val idleCallbackRunnable = IdleCallbackRunnable(frameTimeNanos) currentIdleCallbackRunnable = idleCallbackRunnable reactApplicationContext.runOnJSQueueThread(idleCallbackRunnable) ``` ## Changelog: [ANDROID] [FIXED] - Capture idle callback before running it Pull Request resolved: https://github.com/facebook/react-native/pull/54984 Test Plan: Unfortunately this is a pretty speculative change. That said, since the change is so simple, the worst thing that can happen is nothing. I have also run it in production here with no known issues https://github.com/bluesky-social/social-app/pull/9436 Reviewed By: cortinico Differential Revision: D90120471 Pulled By: lunaleaps fbshipit-source-id: c853a976be999fa883a5835fd5e8ac75a8699681 --- .../com/facebook/react/modules/core/JavaTimerManager.kt | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.kt b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.kt index 36116d46dda..9c3df748c47 100644 --- a/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.kt +++ b/packages/react-native/ReactAndroid/src/main/java/com/facebook/react/modules/core/JavaTimerManager.kt @@ -327,9 +327,11 @@ public open class JavaTimerManager( // If the JS thread is busy for multiple frames we cancel any other pending runnable. // We also capture the idleCallbackRunnable to tentatively fix: // https://github.com/facebook/react-native/issues/44842 + // https://github.com/facebook/react-native/issues/54983 currentIdleCallbackRunnable?.cancel() - currentIdleCallbackRunnable = IdleCallbackRunnable(frameTimeNanos) - reactApplicationContext.runOnJSQueueThread(currentIdleCallbackRunnable) + val idleCallbackRunnable = IdleCallbackRunnable(frameTimeNanos) + currentIdleCallbackRunnable = idleCallbackRunnable + reactApplicationContext.runOnJSQueueThread(idleCallbackRunnable) reactChoreographer.postFrameCallback(ReactChoreographer.CallbackType.IDLE_EVENT, this) } }