Skip to content

Commit d31f707

Browse files
committed
fix(compiler): detect named function expression timer loops and conditional timer ownership
Fix two bugs in the strong-effects resource leak checker: 1. rescheduleKeys now handles named function expressions passed directly as timer callbacks (e.g. setTimeout(function tick() { setTimeout(tick, 1000); }, 1000)). Previously it required the callback argument to be an Identifier, so inline named function expressions that reschedule themselves were classified as one-shot timers and escaped the leak diagnostic. 2. Timer ID ownership now propagates through ConditionalExpression and LogicalExpression branches. Previously, const id = cond ? setInterval(fn, ms) : null set ownership only on the conditional wrapper, so the nested setInterval call got a null handle and clearInterval(id) in cleanup was not recognized as a release.
1 parent 1d7bb34 commit d31f707

2 files changed

Lines changed: 48 additions & 4 deletions

File tree

‎packages/octane/src/compiler/strong-effects.js‎

Lines changed: 24 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -962,9 +962,17 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
962962
// A timer callback that schedules itself again is an interval.
963963
function rescheduleKeys(node, name) {
964964
const callback = functionOf(node.arguments?.[0]);
965+
if (callback === null) return null;
965966
const self = unwrap(node.arguments?.[0]);
966-
if (callback === null || self?.type !== 'Identifier') return null;
967-
const binding = bindingOf(self);
967+
let binding;
968+
if (self?.type === 'Identifier') {
969+
binding = bindingOf(self);
970+
} else if (FUNCTIONS.has(self?.type) && self.id?.type === 'Identifier') {
971+
const fnScope = analysis.functionScopes.get(callback);
972+
binding = fnScope ? lookup(fnScope, self.id.name) : null;
973+
} else {
974+
return null;
975+
}
968976
const keys = [];
969977
const visit = (value) => {
970978
if (value == null || typeof value !== 'object') return;
@@ -1059,6 +1067,18 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
10591067
owners: new WeakMap(),
10601068
};
10611069
const { roots, aliases, declarations, owners } = references;
1070+
const setOwner = (expression, key) => {
1071+
const node = unwrap(expression);
1072+
if (node == null) return;
1073+
owners.set(node, key);
1074+
if (node.type === 'ConditionalExpression') {
1075+
setOwner(node.consequent, key);
1076+
setOwner(node.alternate, key);
1077+
} else if (node.type === 'LogicalExpression') {
1078+
setOwner(node.left, key);
1079+
setOwner(node.right, key);
1080+
}
1081+
};
10621082
const names = new Set();
10631083
for (const { decl, bindings } of declarators) {
10641084
const init = unwrap(decl.init);
@@ -1071,7 +1091,7 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
10711091
}
10721092
}
10731093
if (decl.id?.type === 'Identifier' && binding && init != null) {
1074-
owners.set(init, `b${binding.id}`);
1094+
setOwner(init, `b${binding.id}`);
10751095
}
10761096
}
10771097
// Stable aliases share their root's classification.
@@ -1109,7 +1129,7 @@ export function createStrongEffectPolicy({ ast, analysis, callNames, report }) {
11091129
}
11101130
if (node.type === 'AssignmentExpression' && node.operator === '=') {
11111131
const key = keyOf(node.left);
1112-
if (key !== null) owners.set(unwrap(node.right), key);
1132+
if (key !== null) setOwner(node.right, key);
11131133
}
11141134
if (node.type === 'Identifier' && names.has(node.name)) classify(node, parents);
11151135
parents.push(node);

‎packages/octane/tests/compiler/strong-effects.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,6 +1575,14 @@ export function App(props) @{
15751575
'a self-rescheduling timeout',
15761576
`useEffect(() => { const tick = () => { setWidth((value) => value + 1); setTimeout(tick, 1000); }; setTimeout(tick, 1000); });`,
15771577
],
1578+
[
1579+
'a named function expression timeout loop',
1580+
`useEffect(() => { setTimeout(function tick() { setWidth((value) => value + 1); setTimeout(tick, 1000); }, 1000); });`,
1581+
],
1582+
[
1583+
'a named function expression animation frame loop',
1584+
`useEffect(() => { requestAnimationFrame(function loop() { setWidth((value) => value + 1); requestAnimationFrame(loop); }); });`,
1585+
],
15781586
[
15791587
'a timeout loop that loses its ID',
15801588
`useEffect(() => { let id = 0; const tick = () => { setWidth((value) => value + 1); setTimeout(tick, 1000); }; id = setTimeout(tick, 1000); return () => clearTimeout(id); });`,
@@ -1583,6 +1591,10 @@ export function App(props) @{
15831591
'an animation frame loop',
15841592
`useEffect(() => { function loop() { setWidth((value) => value + 1); requestAnimationFrame(loop); } requestAnimationFrame(loop); });`,
15851593
],
1594+
[
1595+
'a conditional interval without cleanup',
1596+
`useEffect(() => { const id = props.enabled ? setInterval(() => setWidth(1), 1000) : null; });`,
1597+
],
15861598
[
15871599
'a chained ResizeObserver',
15881600
`useEffect(() => { new ResizeObserver(([entry]) => setWidth(entry.contentRect.height)).observe(element.current); });`,
@@ -1789,10 +1801,22 @@ export function App(props) @{
17891801
'a cancelled timeout loop',
17901802
`useEffect(() => { let id = 0; const tick = () => { setWidth((value) => value + 1); id = setTimeout(tick, 1000); }; id = setTimeout(tick, 1000); return () => clearTimeout(id); });`,
17911803
],
1804+
[
1805+
'a cancelled named function expression timeout loop',
1806+
`useEffect(() => { let id = 0; id = setTimeout(function tick() { setWidth((value) => value + 1); id = setTimeout(tick, 1000); }, 1000); return () => clearTimeout(id); });`,
1807+
],
17921808
[
17931809
'a cancelled animation loop',
17941810
`useEffect(() => { let frame = 0; function loop() { setWidth((value) => value + 1); frame = requestAnimationFrame(loop); } frame = requestAnimationFrame(loop); return () => cancelAnimationFrame(frame); });`,
17951811
],
1812+
[
1813+
'a conditional interval with cleanup',
1814+
`useEffect(() => { const id = props.enabled ? setInterval(() => setWidth(1), 1000) : null; return () => clearInterval(id); });`,
1815+
],
1816+
[
1817+
'a logical interval with cleanup',
1818+
`useEffect(() => { const id = props.enabled && setInterval(() => setWidth(1), 1000); return () => clearInterval(id); });`,
1819+
],
17961820
[
17971821
'a disconnected observer',
17981822
`useEffect(() => { const observer = new ResizeObserver(([entry]) => setWidth(entry.contentRect.height)); observer.observe(element.current); return () => observer.disconnect(); });`,

0 commit comments

Comments
 (0)