Skip to content

Commit 6b2a19e

Browse files
committed
fix(signals): distinguish signed zero throughout staged netting
Guard netting with SameValue as well as strict equality. Apply it to fast and strict reads, verdict routing and the seat oracle, preserving accepted NaN rewrites. Cover pending/reveal atomicity with plain and latest readers across action write locations.
1 parent 0c6eafe commit 6b2a19e

5 files changed

Lines changed: 141 additions & 14 deletions

File tree

‎.changeset/staged-signed-zero.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
"@solidjs/signals": patch
33
---
44

5-
Preserve signed zero when a derivation reads a staged signal write accepted by a custom comparator.
5+
Preserve signed zero and pending-state atomicity when a derivation reads a staged signal write accepted by a custom comparator.

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

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2093,19 +2093,17 @@ export function read<T>(el: Signal<T> | Computed<T>): T {
20932093
// proposal, so not a read of the frame's future — nor, for a verdict
20942094
// reader, a stale read of the tick's hold re-run at its landing (#4485).
20952095
// (A plain signal is never uninitialized — the carve-out is `serve`'s.)
2096-
if (el._pendingValue === el._value) {
2097-
// Serve the staging itself: `===` also identifies -0 and +0, which a
2098-
// custom comparator can admit as different values.
2099-
if (__TEST__) shadowActual(SLOT_P);
2100-
return el._pendingValue as T;
2101-
}
2096+
// `===` alone identifies opposite signed zeros. Require SameValue too;
2097+
// keep the strict check so an accepted NaN rewrite remains a proposal.
2098+
const netted = el._pendingValue === el._value && Object.is(el._pendingValue, el._value);
21022099
// Lane work over a folded lane (D-34), once the frame has a transaction,
21032100
// reads the screen too.
21042101
if (
2105-
((observeFlightHook && c._config & CONFIG_VERDICT) || (laneStageHook && passLane?.folded)) &&
2106-
stagedScreen(c as Computed<any>, el)
2102+
netted ||
2103+
(((observeFlightHook && c._config & CONFIG_VERDICT) || (laneStageHook && passLane?.folded)) &&
2104+
stagedScreen(c as Computed<any>, el))
21072105
) {
2108-
if (__TEST__) shadowActual(SLOT_C);
2106+
if (__TEST__) shadowActual(netted ? SLOT_P : SLOT_C);
21092107
return el._value as T;
21102108
}
21112109
stagedRead(c as Computed<any>);
@@ -2402,7 +2400,12 @@ export function serve(el: Signal<any> | Computed<any>, c: Computed<any> | null):
24022400
// frame's future. Only the strict `untrack` read skips the fast block in
24032401
// dev (its `strictRead` test); a hydration snapshot or a promoted write
24042402
// reaches here in dev and prod alike, and keeps the mark.
2405-
if (!(__DEV__ && strictRead) || (el as Computed<any>)._fn || el._pendingValue !== el._value)
2403+
if (
2404+
!(__DEV__ && strictRead) ||
2405+
(el as Computed<any>)._fn ||
2406+
el._pendingValue !== el._value ||
2407+
!Object.is(el._pendingValue, el._value)
2408+
)
24062409
stagedRead(c!, el);
24072410
// D-41: the staged answer is an error (`stageError`).
24082411
if (el._config & CONFIG_STAGED_ERROR) {

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -508,7 +508,12 @@ export function slot(
508508
* that netted, no proposal (`read`'s plain arm; #4452, #4485). A window's
509509
* read of it is the seam's verdict's rows below (`provisionalVerdict`
510510
* answers it final and routes it as they say). */
511-
const netted = staged && !isFn && !laneNode && el._pendingValue === el._value;
511+
const netted =
512+
staged &&
513+
!isFn &&
514+
!laneNode &&
515+
el._pendingValue === el._value &&
516+
Object.is(el._pendingValue, el._value);
512517
/** The staging as a slot: its value, or the error it stages (D-41). */
513518
const stagedSlot = stagedError ? SLOT_E : SLOT_P;
514519
/** The screen as a slot: the committed value, or the error still shown

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -572,7 +572,10 @@ function verdictValue(el: Signal<any> | Computed<any>, c: Computed<any> | null):
572572
// reader is still the tick's verdict reader, shown now (D-25: the
573573
// posture is the pass's). A signal's staging only, as `read`'s arm.
574574
else if (tracked && globalQueue._running)
575-
flushTransaction && !(el as any)._fn && el._pendingValue === el._value
575+
flushTransaction &&
576+
!(el as any)._fn &&
577+
el._pendingValue === el._value &&
578+
Object.is(el._pendingValue, el._value)
576579
? route(c!, flushTransaction)
577580
: provisionalVerdict(c!, !latestActive && !uninitialized);
578581
// Outside a flush, the staging is an unflushed write (A28): the probe

‎packages/signals/tests/staged-signed-zero.test.ts‎

Lines changed: 117 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
11
import { describe, expect, it } from "vitest";
2-
import { createMemo, createRenderEffect, createRoot, createSignal, flush } from "../src/index.js";
2+
import {
3+
action,
4+
createMemo,
5+
createRenderEffect,
6+
createRoot,
7+
createSignal,
8+
flush,
9+
isPending,
10+
latest,
11+
untrack
12+
} from "../src/index.js";
313

414
describe.each([
515
["Object.is", Object.is],
@@ -30,6 +40,80 @@ describe.each([
3040
dispose();
3141
}
3242
});
43+
44+
for (const where of ["in-action", "before-action"] as const)
45+
for (const strict of [false, true])
46+
it.each([
47+
["-0 to +0", -0, 0],
48+
["+0 to -0", 0, -0]
49+
] as const)(
50+
`keeps %s pending until reveal (${where}, strict: ${strict})`,
51+
async (_, initial, next) => {
52+
const gate = Promise.withResolvers<void>();
53+
const state = createRoot(dispose => {
54+
const [value, setValue] = createSignal(initial, { equals });
55+
const [other, setOther] = createSignal(0);
56+
const plain: [number, boolean][] = [];
57+
const ahead: [boolean, number][] = [];
58+
createRenderEffect(other, () => {});
59+
createRenderEffect(
60+
() => {
61+
value();
62+
const v = strict ? untrack(value, "signed-zero") : value();
63+
return [1 / v, isPending(value)] as [number, boolean];
64+
},
65+
v => void plain.push(v)
66+
);
67+
createRenderEffect(
68+
() => [isPending(value), 1 / latest(value)] as [boolean, number],
69+
v => void ahead.push(v)
70+
);
71+
return { dispose, value, setValue, setOther, plain, ahead };
72+
});
73+
try {
74+
flush();
75+
if (where === "before-action") state.setValue(next);
76+
action(function* () {
77+
state.setOther(1);
78+
if (where === "in-action") state.setValue(next);
79+
yield gate.promise;
80+
})();
81+
flush();
82+
83+
expect(state.value()).toBe(initial);
84+
expect(isPending(state.value)).toBe(true);
85+
expect(state.plain).toEqual([
86+
[1 / initial, false],
87+
[1 / initial, true]
88+
]);
89+
expect(state.ahead).toEqual([
90+
[false, 1 / initial],
91+
[true, 1 / next]
92+
]);
93+
94+
gate.resolve();
95+
await new Promise<void>(resolve => setTimeout(resolve, 0));
96+
flush();
97+
98+
expect(state.value()).toBe(next);
99+
expect(isPending(state.value)).toBe(false);
100+
expect(state.plain).toEqual([
101+
[1 / initial, false],
102+
[1 / initial, true],
103+
[1 / next, false]
104+
]);
105+
expect(state.ahead).toEqual([
106+
[false, 1 / initial],
107+
[true, 1 / next],
108+
[false, 1 / next]
109+
]);
110+
} finally {
111+
gate.resolve();
112+
state.dispose();
113+
flush();
114+
}
115+
}
116+
);
33117
});
34118

35119
it("keeps the default comparator's signed-zero equality", () => {
@@ -53,3 +137,35 @@ it("keeps the default comparator's signed-zero equality", () => {
53137
dispose();
54138
}
55139
});
140+
141+
it("keeps an accepted NaN rewrite as a proposal", async () => {
142+
const gate = Promise.withResolvers<void>();
143+
const state = createRoot(dispose => {
144+
const [value, setValue] = createSignal(NaN, { equals: false });
145+
const seen: boolean[] = [];
146+
createRenderEffect(
147+
() => isPending(value),
148+
v => void seen.push(v)
149+
);
150+
return { dispose, value, setValue, seen };
151+
});
152+
try {
153+
flush();
154+
action(function* () {
155+
state.setValue(NaN);
156+
yield gate.promise;
157+
})();
158+
flush();
159+
expect(state.seen).toEqual([false, true]);
160+
expect(isPending(state.value)).toBe(true);
161+
162+
gate.resolve();
163+
await new Promise<void>(resolve => setTimeout(resolve, 0));
164+
flush();
165+
expect(state.seen).toEqual([false, true, false]);
166+
} finally {
167+
gate.resolve();
168+
state.dispose();
169+
flush();
170+
}
171+
});

0 commit comments

Comments
 (0)