Skip to content

Commit 627f44a

Browse files
committed
fix(ownts): don't credit cleanups from async effect callbacks (Codex)
An `async` effect callback returns a Promise, so React never runs its returned function as cleanup. With `function`-callback support now accepting `useEffect(async () => …)` / `useEffect(async function () …)`, a `return () => …` inside an async effect was wrongly treated as a real cleanup, masking the leak in JS/transpiled inputs that aren't type-checked. extract() now detects an async outer callback and credits no cleanup for it (an inner sync IIFE keeps its own cleanup — only the OUTER callback's async-ness matters). EffectLeakControl.tsx gains a fourth control (async effect whose dead cleanup must still leak); test + CI updated to 4. Full suite green; ruff + mypy --strict clean. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01PVeuchch67CtjSbizYm8gi
1 parent 70cbb5f commit 627f44a

4 files changed

Lines changed: 19 additions & 7 deletions

File tree

‎.github/workflows/ci.yml‎

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,15 +1132,16 @@ jobs:
11321132
# cleanup, observer.subscribe/unsubscribe — all released, zero findings.
11331133
[ "$(echo "$rw" | grep -cE '\[(OWN|EFF|DI)[0-9]{3}\]')" -eq 0 ] \
11341134
|| { echo "FAIL: real-world cleanup patterns must not be reported"; exit 1; }
1135-
- name: False-negative controls (wrong controller / args / conditional cleanup) still leak
1135+
- name: False-negative controls (wrong controller / args / conditional / async) still leak
11361136
run: |
11371137
python frontend/ownts/ownts.py frontend/ownts/examples/EffectLeakControl.tsx \
11381138
-o "$RUNNER_TEMP/leak.facts.json"
11391139
leak=$(python -m ownlang ownir "$RUNNER_TEMP/leak.facts.json" || true)
11401140
echo "$leak"
1141-
# a release-shaped cleanup that does not release THIS resource must not be
1142-
# over-suppressed — all three controls stay OWN001
1143-
[ "$(echo "$leak" | grep -c 'OWN001')" -eq 3 ] \
1141+
# a release-shaped cleanup that does not release THIS resource (wrong
1142+
# controller / mismatched args / conditional return / async effect) must
1143+
# not be over-suppressed — all four controls stay OWN001
1144+
[ "$(echo "$leak" | grep -c 'OWN001')" -eq 4 ] \
11441145
|| { echo "FAIL: broadened matchers must not over-suppress real leaks"; exit 1; }
11451146
- name: ES5 `function` callbacks parse; capture-mismatch leak caught (real bug shape)
11461147
run: |

‎frontend/ownts/examples/EffectLeakControl.tsx‎

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
// each uses a release-shaped cleanup that does NOT actually release THIS resource,
33
// so it must STILL report a leak. Run:
44
// python frontend/ownts/ownts.py frontend/ownts/examples/EffectLeakControl.tsx --check
5-
// Expect exactly three OWN001 findings.
5+
// Expect exactly four OWN001 findings.
66
import { useEffect } from "react";
77

88
export function LeakControl({ enabled }: { enabled: boolean }) {
@@ -29,5 +29,12 @@ export function LeakControl({ enabled }: { enabled: boolean }) {
2929
if (enabled) return () => clearInterval(id);
3030
}, [enabled]);
3131

32+
// (4) async effect: React receives a Promise and never runs the returned
33+
// function as cleanup, so the listener leaks despite the release-shaped return.
34+
useEffect(async () => {
35+
window.addEventListener("message", onMessage);
36+
return () => window.removeEventListener("message", onMessage);
37+
}, []);
38+
3239
return <div>leak control</div>;
3340
}

‎frontend/ownts/ownts.py‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,11 @@ def extract(path: str) -> list[Component]:
536536
for eff in _USE_EFFECT.finditer(masked):
537537
body_m, body_start, _deps, end = _effect_callback(masked, eff.end())
538538
body_o = text[body_start:end] # original (unmasked) body — same positions
539-
span = _cleanup_span(body_m)
539+
# An ASYNC effect callback returns a Promise, so React never runs its
540+
# returned function as cleanup — credit no cleanup for it (a `return () => …`
541+
# inside `useEffect(async () => …)` is dead, so the resource still leaks).
542+
is_async = bool(re.match(r"\s*async\b", masked[eff.end():]))
543+
span = None if is_async else _cleanup_span(body_m)
540544
if span:
541545
cs, ce, cov_start = span
542546
setup_m = body_m[:cs] + body_m[ce:]

‎frontend/ownts/test_ownts.py‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ def main() -> int:
6868
# returned cleanup over an unconditional acquire) must STILL report the leak —
6969
# the broadened matchers must not over-suppress.
7070
leaks = codes("EffectLeakControl.tsx")
71-
assert leaks == ["OWN001", "OWN001", "OWN001"], f"EffectLeakControl -> {leaks}"
71+
assert leaks == ["OWN001"] * 4, f"EffectLeakControl -> {leaks}"
7272

7373
# Transpiled-ES5 shape: `function () { … return function () { … } }`. The parser
7474
# handles `function` callbacks + `return function` cleanups — a matched cleanup is

0 commit comments

Comments
 (0)