Skip to content

Commit 9c4e4a8

Browse files
ryansolidclaude
andauthored
fix(signals): don't retain the transaction a latest() read opened after it lands (#4460)
* fix(signals): don't retain the transaction a latest() read opened after it lands `pendingVerdict` (#4453) remembered the flush's transaction a `latest` read of an unheld flight opened in a module-level `latestJoined`, compared only against `flushTransaction` and never cleared: after the seam it kept the last landed transaction (its `_nodes`, `_queues`, `_reruns`) alive until the next such read overwrote it. The mark now lives on the transaction itself (`Transaction.latest`), so it ends with it. Same answer for every probe: a transaction stays the flush's from its opening to the seam, a merge keeps the flush's own, and one merged away is no longer anyone's flush transaction, as before. The property is not `_`-prefixed (as `folded`), so the mangler's assignment is untouched; only bundles importing `latest`/`isPending` change, and they shrink (isPending/latest -13 B brotli, -10 B minified). Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 * test(signals): the #4393/#4400 two-reader specs run on fake timers The #4400 case writes s, waits 3 ms, and writes again while a 10 ms ask is in flight. On CI's coverage run the real gap outlived the ask, which landed before the second write and asked `m1:23` (seen on #4455 and #4460). The gaps are part of each scenario, so the file now advances fake timers instead of sleeping. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2 --------- Co-authored-by: Claude <[email protected]>
1 parent 5f07d04 commit 9c4e4a8

5 files changed

Lines changed: 71 additions & 12 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+
The transaction a `latest()` read of an unheld refetch opens is no longer kept alive after it lands: the mark that it was opened by `latest` lives on the transaction itself instead of in a module-level reference.

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,11 @@ export interface Transaction {
288288
* (scripts/mangle-props.mjs), which costs the floors bytes for a path
289289
* only optimistic stores reach. */
290290
folded?: (c: Computed<any> | null, el: Signal<any> | Computed<any>) => unknown;
291+
/** Opened by a `latest()` read of an unheld flight (verdict.ts
292+
* `pendingVerdict`): it holds nothing of that flight, and a probe in it is
293+
* its verdict lane's with no re-run at its landing (F-6, D-23). Unset
294+
* until then; not `_`-prefixed, as `folded`. */
295+
latest?: 1;
291296
/** `affects()` marks declared in it (affects.ts): released at the landing.
292297
* Not nodes — a mark holds nothing (`blocked` never sees one). */
293298
_marks: Signal<any>[] | null;

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -235,10 +235,6 @@ function heldNotFinal(owner: Computed<any>): boolean {
235235
);
236236
}
237237

238-
/** The flush's transaction when `latest` of an unheld flight opened it
239-
* (`pendingVerdict`): it holds nothing of that flight. */
240-
let latestJoined: Transaction | null = null;
241-
242238
/** `isPending`'s answer for an unheld node whose flight is up (cause ii). */
243239
function pendingVerdict(c: Computed<any> | null, el: Computed<any>): void {
244240
if (el._statusFlags & STATUS_UNINITIALIZED) return;
@@ -255,8 +251,12 @@ function pendingVerdict(c: Computed<any> | null, el: Computed<any>): void {
255251
// re-run at its landing: nothing it holds flips the verdict — the
256252
// flight's own landing re-derives the reader — and it lands at this
257253
// seam, where a re-run would open it again, every flush (F-6, D-23).
258-
if (!probing) flushTransaction || (joinFuture(null), (latestJoined = flushTransaction));
259-
else if (latestJoined && latestJoined === flushTransaction) return route(c, latestJoined);
254+
// (The mark is the transaction's own — `Transaction.latest` — so
255+
// nothing outlives its landing.)
256+
if (!probing)
257+
flushTransaction ||
258+
(joinFuture(null), ((flushTransaction as Transaction | null)!.latest = 1));
259+
else if (flushTransaction?.latest) return route(c, flushTransaction);
260260
provisionalVerdict(c, false); // decided above
261261
}
262262
}

‎packages/signals/tests/gc.test.ts‎

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import {
1313
latest,
1414
mapArray
1515
} from "../src/index.js";
16+
import { flushTransaction, type Transaction } from "../src/core/scheduler.js";
1617

1718
function gc() {
1819
return new Promise(resolve =>
@@ -90,6 +91,48 @@ it("releases an unobserved reader queued by an idle cross-root rejection", async
9091
}
9192
});
9293

94+
it("releases the transaction a latest() read of an unheld refetch opened once it lands", async () => {
95+
// F-6, D-23 (#4453): `pendingVerdict` marks the flush's transaction a
96+
// `latest` read of an unheld flight opened. It holds nothing of the
97+
// flight and lands at that flush's seam — after which nothing may keep it.
98+
const asks: ((v: number) => void)[] = [];
99+
const [s, setS] = createSignal(1);
100+
let armed = false,
101+
opened: Transaction | null = null;
102+
const dispose = createRoot(dispose => {
103+
const m = createMemo(() => {
104+
s();
105+
return new Promise<number>(resolve => asks.push(resolve));
106+
});
107+
createRenderEffect(
108+
() => {
109+
const v = `${latest(m)} ${isPending(m)}`;
110+
if (armed) opened ??= flushTransaction;
111+
return v;
112+
},
113+
() => {}
114+
);
115+
return dispose;
116+
});
117+
flush();
118+
await new Promise<void>(r => setTimeout(r, 0));
119+
asks.shift()!(1);
120+
await new Promise<void>(r => setTimeout(r, 0));
121+
flush();
122+
armed = true;
123+
setS(2);
124+
flush();
125+
expect(opened).not.toBeNull();
126+
const ref = new WeakRef(opened!);
127+
opened = null;
128+
asks.shift()!(2);
129+
await new Promise<void>(r => setTimeout(r, 0));
130+
flush();
131+
dispose();
132+
await collectGarbage();
133+
expect(ref.deref()).toBeUndefined();
134+
});
135+
93136
if (global.gc) {
94137
it("should gc computed if there are no observers", async () => {
95138
const [$x] = createSignal(0),

‎packages/signals/tests/latest-two-readers-one-ask-4393.test.ts‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
// - #4400's "m0:5 asked twice" is a number collision under `1 + latest(s) +
1717
// s()`: the lane question for s=3 and the frame question for s=2 are both
1818
// 5. With a distinct encoding each question is asked once.
19-
import { describe, expect, it } from "vitest";
19+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
2020
import {
2121
createLoadingBoundary,
2222
createMemo,
@@ -30,9 +30,15 @@ import {
3030
} from "../src/index.js";
3131

3232
const sleep = (ms: number) => new Promise(r => setTimeout(r, ms));
33+
// Fake timers: the gaps between writes are part of each scenario, and on a
34+
// loaded runner (CI coverage) a real 3 ms gap outlived a 10 ms ask, landing
35+
// it before the second write (#4400 asked `m1:23`).
36+
const wait = (ms: number) => vi.advanceTimersByTimeAsync(ms);
37+
beforeEach(() => void vi.useFakeTimers());
38+
afterEach(() => void vi.useRealTimers());
3339
async function settle() {
3440
for (let i = 0; i < 4; i++) {
35-
await sleep(15);
41+
await wait(15);
3642
flush();
3743
}
3844
}
@@ -78,7 +84,7 @@ async function run(reader: Reader, gap: number) {
7884
out.length = 0;
7985
setS(2);
8086
flush();
81-
await sleep(gap);
87+
await wait(gap);
8288
flush();
8389
setT(1);
8490
flush();
@@ -190,7 +196,7 @@ describe("#4400: a second write during the hold, distinct encoding", () => {
190196
out.length = 0;
191197
setS(2);
192198
flush();
193-
await sleep(3);
199+
await wait(3);
194200
flush();
195201
setS(3);
196202
flush();
@@ -252,7 +258,7 @@ describe("#4393: a latest() reader's lane answer reaches the screen", () => {
252258
setS(2);
253259
setW(2);
254260
flush();
255-
await sleep(25);
261+
await wait(25);
256262
flush();
257263
expect(out).toEqual(["v22"]);
258264
await settle();
@@ -340,7 +346,7 @@ describe("#4393: a latest() reader's lane answer reaches the screen", () => {
340346
setS(2);
341347
setW(2);
342348
flush();
343-
await sleep(25);
349+
await wait(25);
344350
flush();
345351
expect(out).toEqual(["false,22"]);
346352
await settle();

0 commit comments

Comments
 (0)