Skip to content

Commit be5a6a7

Browse files
committed
fix(attribution): count GRAPH_GROWTH after a held navigation's landing commits
A navigation that settles in a transaction's landing was counted from the transitionSettled hook, before land() commits and disposes the frame parked for the page left. The walk still reached those computations through the sources both pages read, so arriving at a small route from ever bigger pages reported an ownerless-effect leak. Defer the graph walk for landed navigations to flushEnd; the navigation record still settles in the landing. Fixes #3989 Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]> Claude-Session: https://claude.ai/code/session_015Q7JD9PktsxtBJJbWmtuwr
1 parent 2322d64 commit be5a6a7

4 files changed

Lines changed: 99 additions & 6 deletions

File tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@solidjs/signals": patch
3+
---
4+
5+
Attribution: `GRAPH_GROWTH` no longer counts the previous route's computations when a navigation settles through a held transition (#3989). The navigation record still settles in the landing, but its graph walk waits for that drain's end, after the landing's commits dispose the page left — the same instant a one-drain navigation is counted. Observe tier only; no production bytes.

‎packages/signals/src/core/attribution.ts‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3480,6 +3480,14 @@ const openNavs = new Set<NavState>();
34803480
let navigationLog: NavigationEvent[] = [];
34813481
/** Drains completed since enable() — the clock `writeDrain` reads. */
34823482
let drainSeq = 0;
3483+
/**
3484+
* Navigations settled in a landing, waiting for their graph count: the hook
3485+
* fires before the landing's commits dispose the frame parked for the page
3486+
* left, which the walk would still reach through the sources both pages read
3487+
* (#3989). `land()` runs only inside `flush()`'s drain, so `flushEnd` counts
3488+
* them, after those commits — the instant a one-drain navigation is counted.
3489+
*/
3490+
const landedGraphs: NavigationEvent[] = [];
34833491

34843492
/**
34853493
* Copy what the router currently says onto the frame (what writes stamped —
@@ -3597,7 +3605,7 @@ function settleNavigations(t: Transition, hold: HoldEvent | undefined): void {
35973605
if (state === undefined || !openNavs.has(state)) continue;
35983606
// A navigation whose frame is still open when its transition commits
35993607
// (`until()` inside the frame) settles here too: its writes are through.
3600-
settleNavigation(state, state.held ? "held" : "committed", hold);
3608+
settleNavigation(state, state.held ? "held" : "committed", hold, true);
36013609
}
36023610
}
36033611

@@ -3775,6 +3783,9 @@ function trackFlushEnd(): void {
37753783
drainSeq++;
37763784
// The swaps this drain committed have rendered.
37773785
displayFallbacks();
3786+
// Landed first: they settled before anything this drain settles below.
3787+
for (let i = 0; i < landedGraphs.length; i++) trackGraph(landedGraphs[i]);
3788+
landedGraphs.length = 0;
37783789
for (const state of openNavs)
37793790
if (state.open === 0 && !state.held) settleNavigation(state, "committed");
37803791
for (const state of openInteractions) maybeSettleInteraction(state);
@@ -3783,7 +3794,8 @@ function trackFlushEnd(): void {
37833794
function settleNavigation(
37843795
state: NavState,
37853796
outcome: NonNullable<NavigationEvent["outcome"]>,
3786-
hold?: HoldEvent
3797+
hold?: HoldEvent,
3798+
landed?: boolean
37873799
): void {
37883800
if (!openNavs.has(state)) return;
37893801
openNavs.delete(state);
@@ -3797,7 +3809,8 @@ function settleNavigation(
37973809
// route's responsiveness in the feedback tables. Consumers get the record.
37983810
if (event.initial !== true) for (const f of folds) f.navigation?.(event);
37993811
records.emit("navigation", event, undefined);
3800-
trackGraph(event);
3812+
if (landed) landedGraphs.push(event);
3813+
else trackGraph(event);
38013814
// The interaction that performed it may have been waiting only on this.
38023815
const under = openInteractionOf(event.interaction);
38033816
if (under !== undefined) maybeSettleInteraction(under);
@@ -4427,6 +4440,7 @@ function resetTracking(): void {
44274440
stagedFallbacks.delete(DRAIN);
44284441
trackingGen++;
44294442
openNavs.clear();
4443+
landedGraphs.length = 0;
44304444
openInteractions.clear();
44314445
routeCounts.clear();
44324446
drainSeq = 0;

‎packages/signals/tests/attribution-graph-growth.test.ts‎

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import type { GraphEvent, NavigationEvent } from "../src/attribution.js";
1414
import {
1515
createEffect,
1616
createMemo,
17+
createRenderEffect,
1718
createRoot,
1819
createSignal,
1920
flush,
@@ -317,6 +318,79 @@ describe("GRAPH_GROWTH", () => {
317318
dispose();
318319
});
319320

321+
it.each([false, true])(
322+
"a navigation held for its data counts the graph after the landing disposes the page left (#3989, leak: %s)",
323+
async leak => {
324+
const { findings, graphs } = arm({ visits: 3, ratio: 1.25 });
325+
const [shared] = createSignal(0, { name: "shared" });
326+
const [location, setLocation] = createSignal("/", { name: "location" });
327+
const wait = (ms: number) => new Promise<void>(resolve => setTimeout(resolve, ms));
328+
const detached: (() => void)[] = [];
329+
// A big page: rows reading the long-lived signal, so its parked frame is
330+
// reachable through `shared`'s subscribers until the landing disposes it.
331+
const rows = (n: number) => () => {
332+
const list = Array.from({ length: n }, () => createMemo(() => shared()));
333+
return () => list.reduce((sum, row) => sum + row(), 0);
334+
};
335+
// The small page fetches on every visit, so its arrival is held.
336+
const small = () => {
337+
// The positive control: a detached root per visit that nobody disposes.
338+
if (leak)
339+
runWithOwner(null, () =>
340+
createRoot(d => {
341+
detached.push(d);
342+
for (let i = 0; i < 10; i++) createMemo(() => shared());
343+
})
344+
);
345+
const data = createMemo(async () => (await wait(2), 1));
346+
return () => data() + shared();
347+
};
348+
const pages: Record<string, () => () => unknown> = { "/small": small };
349+
for (const n of [20, 40, 80]) pages[`/rows-${n}`] = rows(n);
350+
const dispose = createRoot(dispose => {
351+
const page = createMemo(() => {
352+
const mount = pages[location()];
353+
return mount ? untrack(mount) : () => null;
354+
});
355+
createRenderEffect(
356+
() => page()(),
357+
() => {}
358+
);
359+
return dispose;
360+
});
361+
flush();
362+
const go = async (to: string) => {
363+
OBSERVE!.attribution.withOrigin({ kind: "navigation", name: to, to }, () =>
364+
setLocation(to)
365+
);
366+
flush();
367+
await wait(10);
368+
flush();
369+
return graphSize().computations;
370+
};
371+
const after: number[] = [];
372+
for (const n of [20, 40, 80]) {
373+
await go(`/rows-${n}`);
374+
after.push(await go("/small"));
375+
}
376+
const settles = graphs.filter(g => g.route === "/small");
377+
expect(settles.map(g => g.navigation.outcome)).toEqual(["held", "held", "held"]);
378+
// The settle sees the graph the route settled into, not the page just left.
379+
expect(settles.map(g => g.computations)).toEqual(after);
380+
if (leak) {
381+
expect(after.map(c => c - after[0])).toEqual([0, 10, 20]);
382+
expect(findings).toHaveLength(1);
383+
expect(findings[0].data).toMatchObject({ route: "/small" });
384+
} else {
385+
expect(new Set(after).size).toBe(1);
386+
expect(findings).toHaveLength(0);
387+
}
388+
dispose();
389+
for (const d of detached) d();
390+
flush();
391+
}
392+
);
393+
320394
it("`false` disables the check, and without a listener no walk is made", () => {
321395
const { findings, graphs } = arm(false);
322396
// No graph listener from here on: drop ours and re-enable without one.

‎packages/solid/skills/reactivity-diagnostics/SKILL.md‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -292,9 +292,9 @@ the scope as described under [HUGE_FAN_IN](#huge_fan_in).
292292

293293
### GRAPH_GROWTH
294294

295-
The live graph got bigger on every one of the last `data.history.length`
296-
visits to `data.route`, and it did not shrink on the visits in between —
297-
something each visit creates is never disposed. This is the leak a heap
295+
The live graph, counted when each navigation settles, got bigger on every
296+
one of the last `data.history.length` visits to `data.route` — something
297+
each visit creates is never disposed. This is the leak a heap
298298
snapshot finds; the engine finds it by measuring the reactive graph at each
299299
navigation's settle. `data.grew` says which measure climbed, and that names
300300
the leak: `owners` is an undisposed root or a Portal mounted per visit;

0 commit comments

Comments
 (0)