Skip to content

Commit aca6cda

Browse files
brenelzcursoragent
authored andcommitted
fix(signals): rewriting an orphan override opens no transaction (#3576)
optimisticWrite passed whatever resolveTransition returned to initTransition when the node already carried an override. For an override no transaction owns (an ambient optimistic write, or the latest() shadow's first companion sync) that is null, and initTransition reads null as "open a fresh transaction": the ambient batch was adopted into it, every plain write staged in the same tick was stamped as its pending node, and activeTransition stayed set for the rest of the caller's block. The latest() shadow made this reachable from a plain read: after one latest(() => store.value), the second setStore write in a tick synced the shadow through this path, the store node was stamped held, foldHeld masked the projection's pending backing from context-free readers, and store.value read back 0, 1, 0, 0, 0 where the same writes without the latest() read gave 0, 1, 2, 3, 4. Join a transaction only when one is resolved, as the same-value branch of the write already does. Co-authored-by: Cursor <[email protected]>
1 parent e10a4ba commit aca6cda

3 files changed

Lines changed: 36 additions & 5 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 rewrite of an optimistic node whose override no transaction owns no longer opens a transaction of its own. `optimisticWrite` passed whatever `resolveTransition` returned to `initTransition`, and for an orphan override that is null, which `initTransition` reads as "open a fresh transaction": the ambient batch was adopted into it, every plain write staged in the same tick was stamped as its pending node, and `activeTransition` stayed set for the rest of the caller's block. The `latest()` shadow is such a node: its first companion sync installs the orphan override and the second one hit this path, so after one `latest(() => store.value)` the second and every later `setStore` write in a tick was held by a transaction nobody started, and an untracked read of a projection served the committed value instead of the pending backing (`0, 1, 0, 0, 0` where the same writes without the `latest()` read served `0, 1, 2, 3, 4`). The changed-value branch now joins a transaction only when one is resolved, as the same-value branch already did.

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,11 +98,15 @@ function optimisticWrite<T>(el: Signal<T> | Computed<T>, v: T | ((prev: T) => T)
9898
return v;
9999
}
100100

101-
if (hasOverride) globalQueue.initTransition(resolveTransition(el as any));
102-
// No revert target is stashed: while the override is active every reader
103-
// sees it (A17), so authoritative arrivals commit silently into _value and
104-
// reverting is just dropping the override — _value is already correct.
105-
else globalQueue._batch._optimisticNodes.push(el);
101+
if (hasOverride) {
102+
const transition = resolveTransition(el as any);
103+
if (transition) globalQueue.initTransition(transition);
104+
} else {
105+
// No revert target is stashed: while the override is active every reader
106+
// sees it (A17), so authoritative arrivals commit silently into _value and
107+
// reverting is just dropping the override — _value is already correct.
108+
globalQueue._batch._optimisticNodes.push(el);
109+
}
106110

107111
// Stamp ownership on the node (post-merge, so entangled writers share the
108112
// joint root). resolveTransition prefers this over the lane's _transition,
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { createStore, flush, latest } from "../../src/index.js";
2+
3+
function writeAndReadBack(withLatest: boolean): number[] {
4+
const [store, setStore] = createStore(() => ({ value: 0 }), { value: 0 });
5+
flush();
6+
if (withLatest) latest(() => store.value);
7+
const reads = [store.value];
8+
for (let i = 1; i < 5; i++) {
9+
setStore(v => {
10+
v.value = i;
11+
});
12+
reads.push(store.value);
13+
}
14+
flush();
15+
reads.push(store.value);
16+
return reads;
17+
}
18+
19+
it("untracked reads of a projection see each setter write whether or not latest() read it first (#3576)", () => {
20+
expect(writeAndReadBack(false)).toEqual([0, 1, 2, 3, 4, 4]);
21+
expect(writeAndReadBack(true)).toEqual([0, 1, 2, 3, 4, 4]);
22+
});

0 commit comments

Comments
 (0)