From a65fc06c50dc9b6f6bab4d8068ac6d2ecaa78cdf Mon Sep 17 00:00:00 2001 From: Eduardo Speroni Date: Mon, 7 Nov 2022 13:18:26 -0300 Subject: [PATCH] fix(android): WeakRef race condition on timers (#10066) --- packages/core/timer/index.android.ts | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/packages/core/timer/index.android.ts b/packages/core/timer/index.android.ts index 8bf793113..8ac0ac492 100644 --- a/packages/core/timer/index.android.ts +++ b/packages/core/timer/index.android.ts @@ -3,6 +3,10 @@ */ let timeoutHandler; const timeoutCallbacks = {}; +// this is needed to keep a strong reference to the callback +// currently fixes a race condition in V8 with the android runtime implementation of WeakRef +// there are fixes in the android runtime that will remove the need for this +const timeoutCallbacksCb = {}; let timerId = 0; function createHandlerAndGetId(): number { @@ -29,12 +33,14 @@ export function setTimeout(callback: Function, milliseconds = 0, ...args): numbe if (timeoutCallbacks[id]) { delete timeoutCallbacks[id]; + delete timeoutCallbacksCb[id]; } }, }); if (!timeoutCallbacks[id]) { timeoutCallbacks[id] = runnable; + timeoutCallbacksCb[id] = callback; } timeoutHandler.postDelayed(runnable, long(milliseconds)); @@ -47,6 +53,7 @@ export function clearTimeout(id: number): void { if (timeoutCallbacks[index]) { timeoutHandler.removeCallbacks(timeoutCallbacks[index]); delete timeoutCallbacks[index]; + delete timeoutCallbacksCb[index]; } } @@ -74,6 +81,7 @@ export function setInterval(callback: Function, milliseconds = 0, ...args): numb if (!timeoutCallbacks[id]) { timeoutCallbacks[id] = runnable; + timeoutCallbacksCb[id] = callback; } timeoutHandler.postDelayed(runnable, long(nextCallMs()));