Skip to content

Commit 1a5faec

Browse files
authored
fix(android): WeakRef race condition on timers (#10066)
1 parent 497a9db commit 1a5faec

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

packages/core/timer/index.android.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
*/
44
let timeoutHandler;
55
const timeoutCallbacks = {};
6+
// this is needed to keep a strong reference to the callback
7+
// currently fixes a race condition in V8 with the android runtime implementation of WeakRef
8+
// there are fixes in the android runtime that will remove the need for this
9+
const timeoutCallbacksCb = {};
610
let timerId = 0;
711

812
function createHandlerAndGetId(): number {
@@ -29,12 +33,14 @@ export function setTimeout(callback: Function, milliseconds = 0, ...args): numbe
2933

3034
if (timeoutCallbacks[id]) {
3135
delete timeoutCallbacks[id];
36+
delete timeoutCallbacksCb[id];
3237
}
3338
},
3439
});
3540

3641
if (!timeoutCallbacks[id]) {
3742
timeoutCallbacks[id] = runnable;
43+
timeoutCallbacksCb[id] = callback;
3844
}
3945

4046
timeoutHandler.postDelayed(runnable, long(milliseconds));
@@ -47,6 +53,7 @@ export function clearTimeout(id: number): void {
4753
if (timeoutCallbacks[index]) {
4854
timeoutHandler.removeCallbacks(timeoutCallbacks[index]);
4955
delete timeoutCallbacks[index];
56+
delete timeoutCallbacksCb[index];
5057
}
5158
}
5259

@@ -74,6 +81,7 @@ export function setInterval(callback: Function, milliseconds = 0, ...args): numb
7481

7582
if (!timeoutCallbacks[id]) {
7683
timeoutCallbacks[id] = runnable;
84+
timeoutCallbacksCb[id] = callback;
7785
}
7886

7987
timeoutHandler.postDelayed(runnable, long(nextCallMs()));

0 commit comments

Comments
 (0)