Repository navigation
Description
[2.0.4] createOptimisticStore: a third guess on an array is lost or shows a hole when an earlier guess changed the length
Status: fixed on
main(unreleased). It reproduces on the publishedsolid-js@2.0.4/@solidjs/signals@2.0.4.
It no longer reproduces onmain(6181f4b). Bisected to #4906 ("run stores on the thin engine"), which removed the code path at fault.
This report asks for a regression test and for the release (#5217 /@solidjs/signals@2.0.5).
Versions
solid-js2.0.4,@solidjs/signals2.0.4 (latest on npm)- Node 24.9, browser builds (
dist/solid.jsanddist/solid.dev.jsboth reproduce)
Reproduction
https://playground.solidjs.com/anonymous/51434956-1026-4de6-aa6b-d894e1d89bf1
import { action, createOptimisticStore, createRoot, flush, untrack } from 'solid-js';
const at = (rows, id) => rows.findIndex((r) => r.id === id);
const moveToFront = (id) => (rows) => { const [r] = rows.splice(at(rows, id), 1); rows.unshift(r); };
const push = (id) => (rows) => { rows.push({ id }); };
const swap01 = (rows) => { const a = rows[0]; rows[0] = rows[1]; rows[1] = a; };
const ids = (rows) => [...rows].map((r) => r?.id ?? '<hole>').join(',');
function scenario(name, edits) {
const [store, setStore] = createRoot(() =>
createOptimisticStore({ rows: [{ id: 'r0' }, { id: 'r1' }] }),
);
flush();
const never = new Promise(() => {}); // the server hasn't answered yet
const edit = action(function* (fn) {
setStore((d) => fn(d.rows));
yield never;
});
const model = { rows: [{ id: 'r0' }, { id: 'r1' }] };
for (const fn of edits) {
fn(model.rows);
edit(fn);
flush();
const shown = untrack(() => ids(store.rows));
const expected = ids(model.rows);
console.log(`${name} shown(${shown}) expected(${expected})${shown === expected ? '' : ' -- MISMATCH'}`);
}
}
scenario('A', [moveToFront('r1'), push('x'), moveToFront('r0')]); // third guess lost
scenario('B', [push('x'), moveToFront('r1'), push('y')]); // hole at index 3
scenario('C', [swap01, push('x'), swap01]); // same as A, plain index writesActual (2.0.4)
A shown(r1,r0) expected(r1,r0)
A shown(r1,r0,x) expected(r1,r0,x)
A shown(r1,r0,x) expected(r0,r1,x) -- MISMATCH
B shown(r0,r1,x) expected(r0,r1,x)
B shown(r1,r0,x) expected(r1,r0,x)
B shown(r1,r0,x,<hole>) expected(r1,r0,x,y) -- MISMATCH
C shown(r1,r0) expected(r1,r0)
C shown(r1,r0,x) expected(r1,r0,x)
C shown(r1,r0,x) expected(r0,r1,x) -- MISMATCH
Expected
Each guess composes on the one before it, so the store shows the same thing as the plain-array model: r0,r1,x for A and C, and r1,r0,x,y for B. In B, store.rows.length is 4 but store.rows[3] is undefined, so the reads disagree with each other.
Notes
- Concurrent actions aren't required. One action that makes all three writes, with a
yieldbetween each, gives the same result. splice/unshiftaren't required. Scenario C uses only index assignment andpush.- The trigger: three guesses on the same array while all are pending, where the second changes
lengthand the third writes keys that only the first wrote. Two guesses never fail. Three pushes, and swap/push or push/swap pairs, all behave correctly. - A plain
createStorerunning the same edits matches the model in every scenario.
Cause (2.0.4 code, optimistic.ts)
I traced this by instrumenting a copy of dist/dev.js and dumping each target's guess carriers after every write. In scenario C:
| after write | carriers (wk = keys written, g = arrangement) |
indices/length read from |
|---|---|---|
| 1 (swap) | #0 wk={0,1} g=r1,r0 |
#0 |
| 2 (push) | #0 wk={0,1} g=r1,r0; #1 wk={2,length} g=r1,r0,x |
#1 |
| 3 (swap) | #0 wk={0,1} g=r0,r1,x; #1 wk={2,length} g=r1,r0,x |
#1 (stale) |
- The write goes to an older carrier.
guessCarriergives a write the first live carrier whose record shares one of its keys. The third write's keys are{0,1}, so it lands on #0, beneath JSXFragment / jsx-dom-expressions #1. - The read comes from the newer carrier.
guessForscans from newest to oldest. Any carrier with an arrangement (g !== undefined) claims every index key andlengthunless that key is in itsborrowedset. So JSXFragment / jsx-dom-expressions #1 answers every index read from its frame, which no longer matches #0's. - The hole in B. In B, JSXFragment / jsx-dom-expressions #1's
borrowedset was computed when JSXFragment / jsx-dom-expressions #1 was created, from #0's keys at that time ({2, length}).lengththerefore falls through to #0 and reads 4. Index3was written to #0 later, so JSXFragment / jsx-dom-expressions #1 still claims it, and JSXFragment / jsx-dom-expressions #1's frame has no index 3. The result is a torn read:lengthcomes from one frame and the indices from another.
Status on main
| commit | mismatches (A, B, C × separate actions / one action) |
|---|---|
72b5111f6 Version packages (#5082) = 2.0.4 |
6 / 6 |
46e665b75^ (#5216, just before the thin engine) |
6 / 6 |
46e665b75 #4906 thin engine |
0 / 6 |
6181f4b main HEAD |
0 / 6 |
These runs used the signals source with bun --define __DEV__=true --define __OBSERVE__=true --define __TEST__=false.
Asks:
- Add scenarios A/B/C as a regression test, since feat(signals): run stores on the thin engine (one signal per touched key), delete the deep engine #4906 fixed them as a side effect of the rewrite.
- Publish the pending release (Release packages #5217) so 2.0.x users get the fix.
Activity