Skip to content

Commit 7ce4011

Browse files
committed
fix(solid): a served pass never swallows a client write
A served answer's arrival is hydration's own pass (NO_CLAIM): a latched node re-running there re-serves its server answer. A client write carried by that same flush must still make the node compute. - Client writes already made (pending, unflushed) flush first, outside the pass (`servedPass`). The async runtime's landing flushes synchronously, and that flush would otherwise carry them. - The pass covers only the flush round its served write lands in (`_servedAt = clock`). An effect's write runs in a later round of the same flush, and a latched node re-run there computes. The latch tests `clock > _servedAt`. `@solidjs/signals` exports its flush-round counter (`clock`), an internal live binding, at 0 B. Landings cannot be coalesced into one pass per microtask batch without changing signals. `asyncWrite` already flushes synchronously on every landing (`schedule(); flush()`) at the #4308 head, so a served pass adds no flush per landing. Its trailing `flush()` is a no-op after a value landing. Tests: in served-source-landing, a write before the flush and an effect's write in the landing's flush both give the written value with 1 ask. Both fail on 2b2990a. Co-Authored-By: Claude Opus 5.5 <[email protected]> Claude-Session: https://claude.ai/code/session_015hPxMkC3jg3Uivh65gvaf2
1 parent 2b2990a commit 7ce4011

7 files changed

Lines changed: 111 additions & 30 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"solid-js": patch
3+
"@solidjs/signals": patch
4+
---
5+
6+
A served answer's arrival during hydration no longer swallows a client write made in the same flush. Writes made before the arrival flush first, outside hydration's own pass, and a write made during it (an effect's, a later round of the flush) makes a node it reaches compute, so the node shows the written value instead of re-serving the server's answer. `@solidjs/signals` exports its internal flush-round counter (`clock`) for this.

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ export {
5656
type QueueCallback
5757
} from "./scheduler.js";
5858
export type { InteractionRef, NavigationRef } from "./attribution-hooks.js";
59-
export { ROOT_ERROR_HOOK } from "./scheduler.js";
59+
export { ROOT_ERROR_HOOK, clock } from "./scheduler.js";
6060
export {
6161
configureClientErrors,
6262
type ClientErrorContext,

‎packages/signals/src/index.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ export {
3838
* outside dev builds.
3939
*/
4040
export { setConsoleFooter } from "./core/index.js";
41+
/** @internal The flush round counter (scheduler.ts): hydration's served pass tells its own round from a later one. */
42+
export { clock } from "./core/index.js";
4143
/** @internal The pre-write hook (`setWriteHook` in core/core.ts); the server-components client's deferred hydration. */
4244
/** @internal Plain-signal identities and the dev read watch (dependency edges; the server-components client). */
4345
export { setWriteHook, setSignalIdentities, setReadWatch, signalName } from "./core/index.js";

‎packages/solid/src/client/hydration.ts‎

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import {
2020
createRenderEffect as coreRenderEffect,
2121
createEffect as coreEffect,
2222
setSnapshotCapture,
23+
clock,
2324
markSnapshotScope,
2425
releaseSnapshotScope,
2526
clearSnapshots,
@@ -311,7 +312,7 @@ let _claimOwner: Owner | null = null;
311312
// (readSerializedOrCompute) — a hydrated failure's content run (a hydrated
312313
// `<Errored>`'s, hydratedCreateErrorBoundary; a rejected fragment's fresh
313314
// render over the outcome the server drew for it, resumeBoundaryHydration),
314-
// and a served answer's arrival (`arrive`).
315+
// and a served answer's arrival (`servedPass`).
315316
const NO_CLAIM = {} as Owner;
316317
function isClaiming(): boolean {
317318
if (!_claimOwner) return true;
@@ -585,21 +586,29 @@ export function syncThenable(value: any) {
585586
};
586587
}
587588

589+
// The flush round a served pass's served answer lands in.
590+
let _servedAt: number | undefined;
588591
/**
589-
* The async runtime's landing of a served answer that was still in flight
590-
* (readHydratedValue). The arrival and the flush that carries it, run here
591-
* at once, are hydration's own pass (NO_CLAIM):
592-
* a dependent that adopted its own server answer re-runs there because the
593-
* server's answer for a source arrived, not because a client write diverged
594-
* it, and re-serves its answer (readSerializedOrCompute). The landing runs
595-
* from a settled promise, outside any claim window.
592+
* A served pass: `write(x)` lands a served answer — the server's answer for a
593+
* node arriving (the async runtime writes it and flushes, synchronously),
594+
* or a rejected fragment's fresh render over the outcome the server drew
595+
* (the boundary's trigger) — and the flush round that carries it is
596+
* hydration's own pass (NO_CLAIM). A node that adopted its own server answer
597+
* re-runs there because the server's answer for something it reads arrived,
598+
* not because a client write diverged it, and re-serves that answer
599+
* (readSerializedOrCompute). No client write is swallowed: the ones already
600+
* made flush first, outside the pass, and one made during it (an effect's)
601+
* runs in a later round of the flush, past `_servedAt`, where a node it
602+
* reaches computes. Runs from a settled promise, outside any claim window.
596603
*/
597-
const arrive = (f: any) => (x: any) => {
604+
function servedPass(write: (x?: any) => void, x?: any) {
605+
flush();
598606
_claimOwner = NO_CLAIM;
599-
f(x);
607+
_servedAt = clock;
608+
write(x);
600609
flush();
601-
_claimOwner = null;
602-
};
610+
_claimOwner = _servedAt = undefined as any;
611+
}
603612

604613
/**
605614
* Unwrap a serialized hydration map entry. Only call when the map HAS an
@@ -632,10 +641,12 @@ export function readHydratedValue(initP: any, refresh: () => void, options?: any
632641
}
633642
if (initP.s === 1) return initP.v;
634643
}
635-
// Still in flight (or a loading window's): the runtime lands it, its
636-
// arrival marked (`arrive`).
637-
if (typeof initP.then === "function")
644+
// Still in flight (or a loading window's): the runtime lands it, in a
645+
// served pass for the node it lands.
646+
if (typeof initP.then === "function") {
647+
const arrive = (f: any) => (x: any) => servedPass(f, x);
638648
return { then: (a: any, b: any) => initP.then(arrive(a), arrive(b)) };
649+
}
639650
}
640651
return initP;
641652
}
@@ -713,7 +724,9 @@ export function readSerializedOrCompute(compute: (prev: any) => any, prev: any,
713724
// retries an errored async node (PJ-R35) with nothing changed. No client
714725
// write diverged it there: it re-serves its own answer, an error
715726
// included, and arms nothing — no ask, no loading flash (#4287, #4306).
716-
if (options?.ssrSource !== "hybrid" && _claimOwner !== NO_CLAIM) {
727+
// A later round of an arrival's flush is past that pass (`servedPass`):
728+
// an effect's write re-ran the node there.
729+
if (options?.ssrSource !== "hybrid" && (_claimOwner !== NO_CLAIM || clock > _servedAt!)) {
717730
let p: Owner | null = o;
718731
while (p && !(p as any)._hp) p = p._parent;
719732
if (!p && !(sharedConfig.hydrating && isClaiming())) return compute(prev);
@@ -2304,15 +2317,9 @@ function resumeBoundaryHydration(
23042317
// The client renders the boundary fresh (recover): no claim. Over the
23052318
// outcome the server drew for the failure (an `<Errored>` fallback the
23062319
// swap brought, listed in the reveal record, #4287) it is that
2307-
// failure's content run, as a hydrated `<Errored>`'s is (NO_CLAIM). A
2308-
// resume runs from a settled promise, never inside a claim window, so
2309-
// there is no claim owner to restore — an arrival's pass (`arrive`)
2310-
// that is still marked has had its flush, and does not cover this one.
2311-
_claimOwner = (globalThis as any)._$HY?.v?.[id]?.length ? NO_CLAIM : null;
2320+
// failure's content run, as a hydrated `<Errored>`'s is: a served pass.
23122321
_hydratingValue = false;
2313-
set();
2314-
flush();
2315-
_claimOwner = null;
2322+
(globalThis as any)._$HY?.v?.[id]?.length ? servedPass(set) : (set(), flush());
23162323
}
23172324
// Hydration mode is off once a boundary has resumed — whatever the flag
23182325
// read before (a resume never runs inside a root's synchronous pass; the
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
{
22
"name": "served-source-landing",
3-
"shell": "<div _hk=2><!--$--><template id=\"pl-3\"></template><i _hk=30>loading</i><!--pl-3--><!--/--><span>tail</span></div><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n\tconst resolver = {\n\t\tp: 0,\n\t\ts: 0,\n\t\tf: 0\n\t};\n\tresolver.p = new Promise((resolve, reject) => {\n\t\tresolver.s = resolve;\n\t\tresolver.f = reject;\n\t});\n\treturn resolver;\n})()).p;_$HY.r[\"1\"]=$R[3]=($R[4]=$R[2]()).p;_$HY.r[\"3_fr\"]=$R[5]=($R[6]=$R[2]()).p;</script>",
4-
"rest": "<script>($R[7]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[1],\"k1\");</script><template id=\"3\"><span _hk=3000>b:<!--$-->k12<!--/--></span></template><script>$R[7]($R[4],\"k12\");$df(\"3\");function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!==\"pl-\"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,(_$HY.v=_$HY.v||{})[e]=n.hasAttribute(\"e\")?[...n.content.childNodes]:1,o.replaceWith(n.content),n.remove(),_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue===\"pl-\"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute(\"data-dfc\"))&&(t._$dc=1,$dfc(k))}document.addEventListener(\"load\",$dfe,!0);document.addEventListener(\"error\",$dfe,!0);</script><script>$R[7]($R[6],!0);</script>"
3+
"shell": "<div _hk=4><!--$--><template id=\"pl-5\"></template><i _hk=50>loading</i><!--pl-5--><!--/--><span>tail</span></div><script>(self.$R=self.$R||{})[\"\"]=[];_$HY.r[\"0\"]=$R[0]=($R[1]=($R[2]=() => {\n\tconst resolver = {\n\t\tp: 0,\n\t\ts: 0,\n\t\tf: 0\n\t};\n\tresolver.p = new Promise((resolve, reject) => {\n\t\tresolver.s = resolve;\n\t\tresolver.f = reject;\n\t});\n\treturn resolver;\n})()).p;_$HY.r[\"2\"]=$R[3]=($R[4]=$R[2]()).p;_$HY.r[\"5_fr\"]=$R[5]=($R[6]=$R[2]()).p;</script>",
4+
"rest": "<script>($R[7]=(resolver, data) => {\n\tresolver.s(data);\n\tresolver.p.s = 1;\n\tresolver.p.v = data;\n})($R[1],\"k1\");</script><template id=\"5\"><span _hk=5000>b:<!--$-->k12<!--/--></span></template><script>$R[7]($R[4],\"k12\");$df(\"5\");function $df(e){return _$HY.f?_$HY.f(e):$dfr(e)}function $dfr(e,n,o,t){if(!(n=document.getElementById(e)))return 0;if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dq=_$HY.dq||{})[e]=1,0;for(;o&&(8!==o.nodeType||o.nodeValue!==\"pl-\"+e);)t=o.nextSibling,o.remove(),o=t;t=o.parentNode,(_$HY.v=_$HY.v||{})[e]=n.hasAttribute(\"e\")?[...n.content.childNodes]:1,o.replaceWith(n.content),n.remove(),_$HY.fe(e,t),_$HY.hp&&_$HY.hp[e]&&($dh(_$HY.hp[e]),delete _$HY.hp[e]),$dfd();return 1}function $dfl(e,o,n){if(!(o=document.getElementById(\"pl-\"+e)))return(_$HY.dlq=_$HY.dlq||{})[e]=1,0;if(o._$fl)return 1;for(n=o.nextSibling;n;){if(8===n.nodeType&&n.nodeValue===\"pl-\"+e){o.parentNode&&o.parentNode.insertBefore(o.content.cloneNode(!0),n),o._$fl=1,$dfd();return 1}n=n.nextSibling}return 0}function $dflj(e,i){for(i=0;i<e.length;i++)$dfl(e[i])}function $dfd(e,i){if(e=_$HY.dq){_$HY.dq=0;for(i in e)$df(i)}if(e=_$HY.dlq){_$HY.dlq=0;for(i in e)$dfl(i)}}function $dfs(e,c,d){(_$HY.sc=_$HY.sc||{})[e]=c,d&&((_$HY.sd=_$HY.sd||{})[e]=1)}function $dfg(e,g,i,k){if(!(g=_$HY.sg&&_$HY.sg[e]))return;for(i=0;i<g.length;i++)if(_$HY.sc&&_$HY.sc[g[i]]>0)return;for(i=0;i<g.length;i++)k=g[i],delete _$HY.sg[k],$df(k)}function $dfc(e){if(--_$HY.sc[e]<=0){delete _$HY.sc[e],_$HY.sg&&_$HY.sg[e]?$dfg(e):!(_$HY.sd&&_$HY.sd[e])&&$df(e);_$HY.sd&&delete _$HY.sd[e]}}function $dfj(e,i,n){for(i=0;i<e.length;i++)if(_$HY.sc&&_$HY.sc[e[i]]>0){for(n=0;n<e.length;n++)(_$HY.sg=_$HY.sg||{})[e[n]]=e;return}for(i=0;i<e.length;i++)$df(e[i])}function $dfe(e,t,k){(t=e.target)&&t.getAttribute&&!t._$dc&&(k=t.getAttribute(\"data-dfc\"))&&(t._$dc=1,$dfc(k))}document.addEventListener(\"load\",$dfe,!0);document.addEventListener(\"error\",$dfe,!0);</script><script>$R[7]($R[6],!0);</script>"
55
}

‎packages/web/test/harness/scenarios.tsx‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1697,24 +1697,33 @@ function parkedRejectionAsyncFallback(late: boolean) {
16971697
// first and re-runs `b`. Nothing on the client changed — the server's `b`
16981698
// was computed from that same `a` — so `b` re-serves its own server answer
16991699
// instead of asking again; a client write to their input still recomputes
1700-
// both.
1700+
// both. `servedSourceControl.write` makes an effect on `a` write `extra`,
1701+
// which `b` also reads, when `a` lands on the client: a client write in the
1702+
// landing's own flush, which `b` must compute for. `servedSourceControl.b`
1703+
// is `b`'s value as the client last committed it.
17011704
let setServedSourceKey!: (v: string) => void;
17021705
let servedSourceAsks = 0;
17031706
export const servedSourceAskCount = () => servedSourceAsks;
1707+
export const servedSourceControl = { write: false, b: "" };
17041708
function ServedSourceLanding() {
17051709
const [key, setKey] = createSignal("k");
1710+
const [extra, setExtra] = createSignal("");
17061711
setServedSourceKey = setKey;
17071712
const a = createMemo(async () => {
17081713
const k = key();
17091714
await sleep(5);
17101715
return k + "1";
17111716
});
1717+
createEffect(a, () => {
1718+
if (servedSourceControl.write) setExtra("x");
1719+
});
17121720
const b = createMemo(async () => {
1713-
const x = a();
1721+
const x = a() + extra();
17141722
if (!isServer && Promise === RealPromise) servedSourceAsks++;
17151723
await sleep(5);
17161724
return x + "2";
17171725
});
1726+
createEffect(b, v => void (servedSourceControl.b = v));
17181727
return (
17191728
<div>
17201729
<Loading fallback={<i>loading</i>}>

‎packages/web/test/hydration/served-source-landing.spec.tsx‎

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
* answer — nothing is asked again, in either replay order. A client write
1010
* to their input afterwards recomputes them (one real ask for `b`).
1111
*
12+
* The landing never swallows a client write: one made before it in the same
13+
* flush, or one an effect makes in the landing's own flush, reaches `b`,
14+
* which computes the written value (one real ask).
15+
*
1216
* Replays the parity harness artifact (test/server/hydration-harness.spec.tsx).
1317
*/
1418
import { describe, expect, test, vi } from "vitest";
@@ -17,7 +21,7 @@ import { resolve, dirname } from "node:path";
1721
import { fileURLToPath } from "node:url";
1822
import { createRenderEffect, createRoot, createSignal, flush } from "solid-js";
1923
import { hydrate } from "@solidjs/web";
20-
import { scenarios, servedSourceAskCount } from "../harness/scenarios.jsx";
24+
import { scenarios, servedSourceAskCount, servedSourceControl } from "../harness/scenarios.jsx";
2125

2226
const name = "served-source-landing";
2327
const file = resolve(
@@ -91,7 +95,60 @@ async function run(mode: "loaded" | "streamed") {
9195
}
9296
}
9397

98+
// Hydrated mid-stream with a client write in the flush that lands `a`:
99+
// `before` writes their input right after the script that settles `a`,
100+
// before that landing runs; `effect` has an effect on `a` write `extra`,
101+
// which `b` reads, inside the landing's flush.
102+
async function runWrite(kind: "before" | "effect") {
103+
const { shell, rest } = JSON.parse(readFileSync(file, "utf-8"));
104+
const container = document.createElement("div");
105+
document.body.appendChild(container);
106+
(globalThis as any)._$HY = { events: [], completed: new WeakSet(), r: {}, fe() {} };
107+
const warn = vi.spyOn(console, "warn").mockImplementation(() => {});
108+
servedSourceControl.write = kind === "effect";
109+
const asksBefore = servedSourceAskCount();
110+
let dispose: (() => void) | undefined;
111+
try {
112+
applyChunk(container, shell, true);
113+
dispose = hydrate(() => <scenario.App />, container);
114+
flush();
115+
await sleep(10);
116+
flush();
117+
const [first, ...others] = pieces(rest);
118+
expect(first).not.toContain("k12");
119+
applyChunk(container, first, false);
120+
// `a`'s landing is queued: this write precedes it, in the same flush.
121+
if (kind === "before") scenario.update!();
122+
await sleep(20);
123+
flush();
124+
for (const piece of others) {
125+
applyChunk(container, piece, false);
126+
flush();
127+
await sleep(5);
128+
flush();
129+
}
130+
await sleep(30);
131+
flush();
132+
// `b` computed the written value. (The boundary claims the server's
133+
// markup when it resumes after the write — its text stays the served
134+
// `k12`, as it does without this pass; the value is the pin here.)
135+
expect(servedSourceControl.b).toBe(kind === "before" ? "j12" : "k1x2");
136+
expect(servedSourceAskCount()).toBe(asksBefore + 1);
137+
expect(warn).not.toHaveBeenCalled();
138+
} finally {
139+
servedSourceControl.write = false;
140+
warn.mockRestore();
141+
dispose?.();
142+
await sleep(0);
143+
container.remove();
144+
}
145+
}
146+
94147
describe("a served source's answer arriving does not re-ask its dependent", () => {
95148
test("hydrated after the stream", () => run("loaded"));
96149
test("hydrated mid-stream", () => run("streamed"));
150+
test("a client write before the landing, in its flush: computes the written value", () =>
151+
runWrite("before"));
152+
test("an effect's write in the landing's flush: computes the written value", () =>
153+
runWrite("effect"));
97154
});

0 commit comments

Comments
 (0)