Skip to content

Commit ddaa1d2

Browse files
committed
fix(signals): a render effect's first run goes through under an async gate (#4343)
A render effect's synchronous first run on creation is not a queued run and goes through behind a fallback (D-19), but runEffect asked the boundary hook on every call. Under `<Loading> > <Show when={ready()}> > <Loading>`, a `ready` async memo settling mounts the inner content while the outer boundary is still on its fallback in that flush, so the first run was held there, then held again behind the inner fallback until the inner data landed. With `ready` a signal no fallback was up at creation and the effect ran at once. The creation call now passes type 0 and the hook is asked only for queued runs; user effects still wait for the reveal. Fixes #4343 Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 56a982c commit ddaa1d2

5 files changed

Lines changed: 232 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+
A render effect's first run goes through when its subtree mounts, whatever boundary above is still on its fallback in that flush (D-19). Under `<Loading> > <Show when={ready()}> > <Loading>`, a render effect in the inner content ran at once while the inner fallback showed when `ready` was a signal, but waited for the inner data when `ready` was an async memo settling: the outer boundary had not swapped to its content yet when the effect was created, and its first run was held like a queued one. Both gates now run it with the mount (#4343). User effects still wait for the reveal.

‎packages/signals/docs/RULES-DERIVED.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ On first load, a fallback that reads pending data is the parent's pending: the p
162162

163163
Every _queued_ run behind a fallback — render-effect updates and user effects, `Portal`'s `schedule: true` render effect included — waits in the boundary and is released at the reveal, so nothing behind a fallback reaches `document.body` early. The synchronous first render on creation goes through.
164164

165-
**Collapses:** §27 7g (2026-10-02, "we definitely shouldn't be showing portals early. Flip it."), B-QUEUED, recovery 9.5. **Status:** standing.
165+
**Collapses:** §27 7g (2026-10-02, "we definitely shouldn't be showing portals early. Flip it."), B-QUEUED, recovery 9.5. **Status:** standing. The first render goes through whichever boundary above is on its fallback when the effect is created — an outer one about to reveal in the same flush included, so a subtree opened by an async memo settling runs its render effects with the mount, as one opened by a signal does (#4343, `tests/render-effect-under-async-gate-4343.test.ts`).
166166

167167
### D-21. An armed boundary lets a lane reveal: the async under the fallback does not hold it
168168

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

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ export function effect<T>(
7979
!node._x?._transaction?._lane &&
8080
(node._type === EFFECT_USER || options?.schedule
8181
? globalQueue.enqueue(node._type, runEffect.bind(null, node))
82-
: runEffect(node, node._type));
82+
: // Type 0: the synchronous first render, not a queued run (D-19).
83+
runEffect(node, 0));
8384
if (__DEV__ && !node._parent) {
8485
const message =
8586
"[NO_OWNER_EFFECT] Effects created outside a reactive context will never be disposed";
@@ -176,10 +177,12 @@ function runEffect(node: Effect<any>, type: number): void {
176177
// effect's update too (maintainer, 2026-10-02: "we held the queue but let
177178
// the sync renders through… we definitely shouldn't be showing portals
178179
// early") — a Portal's or head-tag's render effect writes outside the
179-
// hidden subtree. The synchronous first render on creation is not a queued
180-
// run and goes through. `_modified` stays set; the boundary re-queues the
181-
// run (`release`).
182-
if (heldRunHook?.(node)) return;
180+
// hidden subtree. The synchronous first render on creation (`type` 0) is
181+
// not a queued run and goes through, whichever boundary above is on its
182+
// fallback then — an outer one about to reveal in this flush included
183+
// (#4343). `_modified` stays set; the boundary re-queues the run
184+
// (`release`).
185+
if (type && heldRunHook?.(node)) return;
183186
// Error arm (#2840), user effects only: a compute-phase error that is still
184187
// the node's settled state at effect time runs the bundle's error handler in
185188
// this same imperative, writable scope. Unwrap the StatusError used for
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
/**
2+
* #4343: how a subtree was opened does not change when its render effects
3+
* run. `<Loading fallback="outer"> > <Show when={ready()}> >
4+
* <Loading fallback="inner"> > Screen`, where Screen binds a pending async
5+
* memo (`<p>{data()}</p>`, caught by the inner boundary) and has a render
6+
* effect that reads nothing pending. That effect's first run goes through
7+
* with the mount (D-19), beside the inner fallback, whether `ready` is a
8+
* signal set later or an async memo that settles. Before, under the async
9+
* gate the effect waited for `data` to land. A user effect's first run is
10+
* queued and waits for the reveal under both gates.
11+
*/
12+
import { describe, expect, it } from "vitest";
13+
import {
14+
createEffect,
15+
createLoadingBoundary,
16+
createMemo,
17+
createRenderEffect,
18+
createRoot,
19+
createSignal,
20+
flush,
21+
untrack
22+
} from "../src/index.js";
23+
24+
function deferred<T>() {
25+
let resolve!: (v: T) => void;
26+
const promise = new Promise<T>(r => (resolve = r));
27+
return { promise, resolve };
28+
}
29+
30+
const settle = async () => {
31+
for (let i = 0; i < 3; i++) {
32+
await new Promise(r => setTimeout(r, 0));
33+
flush();
34+
}
35+
};
36+
37+
async function run(gate: "signal" | "async") {
38+
const log: string[] = [];
39+
const readyGate = deferred<boolean>();
40+
const dataGate = deferred<string>();
41+
const [open, setOpen] = createSignal(false);
42+
let slot: unknown;
43+
const screens: unknown[] = [];
44+
const snap = () => screens.push([slot, [...log]]);
45+
46+
createRoot(() => {
47+
const ready = gate === "async" ? createMemo(() => readyGate.promise) : open;
48+
const screen = () => {
49+
const data = createMemo(async () => {
50+
const v = await dataGate.promise;
51+
log.push("data");
52+
return v;
53+
});
54+
createRenderEffect(
55+
() => 0,
56+
() => {
57+
log.push("effect");
58+
}
59+
);
60+
// A user effect's first run is queued: it waits behind the fallback.
61+
createEffect(
62+
() => 0,
63+
() => {
64+
log.push("user");
65+
}
66+
);
67+
// <p>{data()}</p>
68+
return () => data();
69+
};
70+
const outer = createLoadingBoundary(
71+
() => {
72+
// <Show when={ready()}>
73+
const condition = createMemo(() => !!ready());
74+
return createMemo(() =>
75+
condition() ? untrack(() => createLoadingBoundary(screen, () => "inner")) : undefined
76+
);
77+
},
78+
() => "outer"
79+
);
80+
createRenderEffect(outer, v => {
81+
slot = v;
82+
});
83+
});
84+
flush();
85+
snap();
86+
87+
if (gate === "signal") {
88+
setOpen(true);
89+
flush();
90+
} else readyGate.resolve(true);
91+
await settle();
92+
snap();
93+
94+
dataGate.resolve("v");
95+
await settle();
96+
snap();
97+
return screens;
98+
}
99+
100+
describe("#4343: a render effect under a nested boundary runs when its subtree mounts", () => {
101+
it("gate: a signal set later", async () => {
102+
expect(await run("signal")).toEqual([
103+
[undefined, []],
104+
["inner", ["effect"]],
105+
["v", ["effect", "data", "user"]]
106+
]);
107+
});
108+
109+
it("gate: an async memo that settles", async () => {
110+
expect(await run("async")).toEqual([
111+
["outer", []],
112+
["inner", ["effect"]],
113+
["v", ["effect", "data", "user"]]
114+
]);
115+
});
116+
});
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
/**
2+
* @jsxImportSource @solidjs/web
3+
* @vitest-environment jsdom
4+
*/
5+
import { describe, expect, it } from "vitest";
6+
import {
7+
Loading,
8+
Show,
9+
createEffect,
10+
createMemo,
11+
createRenderEffect,
12+
createSignal,
13+
flush
14+
} from "solid-js";
15+
import { render } from "../src/index.js";
16+
17+
// #4343: `<Loading> > <Show when={ready()}> > <Loading fallback="inner"> > Screen`,
18+
// where Screen reads a pending async memo and has a render effect that reads
19+
// nothing pending. The inner boundary catches Screen's pending read; the
20+
// render effect's first run goes through when its subtree mounts (D-19),
21+
// whether `ready` is a signal set later or an async memo that settles. A
22+
// user effect's first run is queued and waits for the reveal under both.
23+
function deferred<T>() {
24+
let resolve!: (v: T) => void;
25+
const promise = new Promise<T>(r => (resolve = r));
26+
return { promise, resolve };
27+
}
28+
29+
async function settle() {
30+
for (let i = 0; i < 8; i++) await Promise.resolve();
31+
flush();
32+
}
33+
34+
async function run(gate: "signal" | "async") {
35+
const el = document.createElement("div");
36+
const log: string[] = [];
37+
const screens: [string, string[]][] = [];
38+
const snap = () => screens.push([el.textContent!, [...log]]);
39+
const readyGate = deferred<boolean>();
40+
const dataGate = deferred<string>();
41+
const [open, setOpen] = createSignal(false);
42+
43+
function Screen() {
44+
const data = createMemo(async () => {
45+
const v = await dataGate.promise;
46+
log.push("data");
47+
return v;
48+
});
49+
createRenderEffect(
50+
() => 0,
51+
() => {
52+
log.push("effect");
53+
}
54+
);
55+
createEffect(
56+
() => 0,
57+
() => {
58+
log.push("user");
59+
}
60+
);
61+
return <p>{data()}</p>;
62+
}
63+
64+
const dispose = render(() => {
65+
const ready = gate === "async" ? createMemo(() => readyGate.promise) : open;
66+
return (
67+
<Loading fallback="outer">
68+
<Show when={ready()}>
69+
<Loading fallback="inner">
70+
<Screen />
71+
</Loading>
72+
</Show>
73+
</Loading>
74+
);
75+
}, el);
76+
flush();
77+
snap();
78+
79+
if (gate === "signal") {
80+
setOpen(true);
81+
flush();
82+
} else readyGate.resolve(true);
83+
await settle();
84+
snap();
85+
86+
dataGate.resolve("v");
87+
await settle();
88+
snap();
89+
dispose();
90+
return screens;
91+
}
92+
93+
describe("#4343: a render effect under a nested <Loading> runs when its subtree mounts", () => {
94+
for (const gate of ["signal", "async"] as const)
95+
it(`runs while the inner fallback shows (gate: ${gate})`, async () => {
96+
expect(await run(gate)).toEqual([
97+
[gate === "signal" ? "" : "outer", []],
98+
["inner", ["effect"]],
99+
["v", ["effect", "data", "user"]]
100+
]);
101+
});
102+
});

0 commit comments

Comments
 (0)