Skip to content

Commit 8f9f369

Browse files
authored
fix(signals): suspend uninitialized async across lanes (#3277)
* fix(signals): suspend uninitialized async across lanes * chore: add changeset for async lane fix --------- Co-authored-by: Xia Chao <[email protected]>
1 parent 8090607 commit 8f9f369

3 files changed

Lines changed: 97 additions & 3 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+
Suspend uninitialized async values across optimistic lanes so `latest()`-conditioned branches wait for their first value instead of rendering `undefined`.

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

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1204,9 +1204,16 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
12041204
console.warn(message);
12051205
}
12061206
// Per-lane suspension lives with the engine (a non-null lane implies it
1207-
// is installed): under a lane, only same-lane pending async without an
1208-
// active override throws.
1209-
if (currentOptimisticLane === null || GlobalQueue._laneSuspends!(owner)) {
1207+
// is installed): under a lane, same-lane pending async without an active
1208+
// override throws; uninitialized async sources always suspend.
1209+
// A lane mismatch can preserve an already committed stale value, but an
1210+
// uninitialized async source has no such value. Never let that path
1211+
// surface `undefined` while a conditional branch is being updated.
1212+
if (
1213+
currentOptimisticLane === null ||
1214+
owner._statusFlags & STATUS_UNINITIALIZED ||
1215+
GlobalQueue._laneSuspends!(owner)
1216+
) {
12101217
if (!tracking && el !== c) link(el, c as Computed<any>);
12111218
throw owner._x?._error;
12121219
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
6+
import { expect, test } from "vitest";
7+
import { createSignal, flush, latest } from "solid-js";
8+
import { render } from "../src/index.js";
9+
10+
function deferred<T>() {
11+
let resolve!: (value: T) => void;
12+
const promise = new Promise<T>(r => (resolve = r));
13+
return { promise, resolve };
14+
}
15+
16+
async function settle() {
17+
await Promise.resolve();
18+
await Promise.resolve();
19+
flush();
20+
}
21+
22+
test("latest() condition does not reveal an unready async branch (#3276)", async () => {
23+
const div = document.createElement("div");
24+
const answer = deferred<string>();
25+
const trace: Array<Record<string, unknown>> = [];
26+
const log = (event: string, details: Record<string, unknown> = {}) => {
27+
trace.push({ event, ...details });
28+
};
29+
let toggle!: () => void;
30+
31+
const dispose = render(() => {
32+
const [entangle, setEntangle] = createSignal(false);
33+
const [asyncValue] = createSignal(async () => {
34+
log("async.compute.start");
35+
const value = await answer.promise;
36+
log("async.compute.resolve", { value });
37+
return value;
38+
});
39+
const a = () => {
40+
const condition = latest(entangle);
41+
log("branch.a", { condition, value: condition ? asyncValue() : "BAD" });
42+
return condition ? asyncValue() : "BAD";
43+
};
44+
const b = () => {
45+
const condition = entangle();
46+
log("branch.b", { condition, value: condition ? asyncValue() : "OK" });
47+
return condition ? asyncValue() : "OK";
48+
};
49+
toggle = () => {
50+
log("toggle.before", { entangle: entangle() });
51+
setEntangle(value => !value);
52+
log("toggle.after", { entangle: entangle(), latest: latest(entangle) });
53+
};
54+
55+
return (
56+
<main>
57+
<button onClick={toggle}>toggle</button>
58+
<p id="a">{a()}</p>
59+
<p id="b">{b()}</p>
60+
</main>
61+
);
62+
}, div);
63+
64+
expect(div.querySelector("#a")!.textContent).toBe("BAD");
65+
expect(div.querySelector("#b")!.textContent).toBe("OK");
66+
67+
toggle();
68+
flush();
69+
if (process.env.SOLID_ISSUE_3276_TRACE)
70+
process.stderr.write(`ISSUE-3276 TRACE ${JSON.stringify(trace)}\n`);
71+
72+
// A must obey the same async gate as B: neither branch may expose an
73+
// uninitialized value before the async computation resolves.
74+
expect(div.querySelector("#a")!.textContent).toBe("BAD");
75+
expect(div.querySelector("#b")!.textContent).toBe("OK");
76+
77+
answer.resolve("ready");
78+
await settle();
79+
expect(div.querySelector("#a")!.textContent).toBe("ready");
80+
expect(div.querySelector("#b")!.textContent).toBe("ready");
81+
dispose();
82+
});

0 commit comments

Comments
 (0)