[2.0 / main] A guess before the first answer (S-56): crash, empty Loading hole, stuck latest() and a render that never runs

Aperta
#5,118 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
35/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
javascript, typescript
Ambito
frontend, testing

Direzione di ricerca

Start with packages/signals/src/core/guesses.ts at optimisticWrite and inspect the two named S-56 tests in packages/signals. Then run the four repros described in packages/web/test/first-load-guess.spec.tsx and the signals suite. The issue is resolved when the expected behavior for guesses before a node’s first answer is decided and the relevant repros and tests pass.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

On main at 99db9bce4, four fuzz repros come down to S-56: a guess made on an optimistic node before its first answer shows, and the first answer judges it. The four symptoms are a crash, an empty hole in a revealed <Loading>, a render effect that never runs, and a latest() stuck on a reverted guess. Making optimisticWrite (guesses.ts#L96) ignore a guess while the node is still uninitialized fixes all four and fails the 2 tests that pin S-56.

Symptoms (each fails on main and passes with the probe)

repro what happens
resolveTx crash [o, setO] = createOptimistic(() => ask("o:" + latest(s))), read only through createDeferred(() => o()). Steps: setS(2), then an action guesses o, then setS(3). The last flush throws TypeError: Cannot read properties of null (reading '_into') in resolveTx.
H: empty hole q = createOptimistic(() => ask("q:" + a1())) is guessed while a1's first load is out; q is not rendered. When a1 and a2 land, <Loading> reveals [a1=] [a2=a2]: the a1 hole is empty, and a1() read outside throws NotReadyError until q lands.
R4C-1: stuck latest One action guesses an async optimistic node that is still on its first load and a plain createOptimistic(0). After everything lands, the screen shows [o0=0][Lo0=30] for good, while untracked latest(o0) is 0. The stuck signal has no async of its own.
J: never renders During a first load, one action writes s1, guesses o = createOptimistic(() => ask("o:" + latest(s1))) and writes s0. A render effect over q = createSignal(() => ask("q:" + latest(s0))) never runs, although q() is "q:1" and no flight is open.

H, the crash and R4C-1 are web specs with compiled JSX (below). J uses only solid-js primitives (createRenderEffect, createOptimistic, action, latest).

Probe: at the top of optimisticWrite:

if ((n as any)._fn !== undefined && (n as Computed<T>)._statusFlags & STATUS_UNINITIALIZED) return v as T;

All four flip to passing. packages/signals goes from 7242 passed to 2 failed and 7240 passed, both S-56 pins:

  • createOptimistic.test.ts: refreshing an optimistic accessor does not throw upstream pending reads (#2694)
  • uninitialized-visibility.test.ts: optimistic computed: getter throws while uninitialized; a guess before the first answer shows and the answer judges it (S-56)

Question: S-56 replaced the old OPTIMISTIC_WRITE_BEFORE_LOAD error (rc.21) with a guess that shows. These four are what that guess does to nodes around it while their own first loads are still out. Should S-56 stand, with the four fixed inside it? Or should a guess before the first answer go back to being refused, or held until the answer, which the probe approximates? The probe is not a proposed fix. It shows the four share this one entry point.

spec (H, resolveTx, R4C-1), as packages/web/test/first-load-guess.spec.tsx
/**
 * @jsxImportSource @solidjs/web
 * @vitest-environment jsdom
 */
import { expect, test } from "vitest";
import {
  For,
  Loading,
  Show,
  action,
  createDeferred,
  createMemo,
  createOptimistic,
  createOptimisticStore,
  createProjection,
  createSignal,
  flush,
  latest,
  refresh,
  untrack
} from "solid-js";
import { render } from "../src/index.js";

process.on("unhandledRejection", () => {});

const tick = async () => {
  for (let i = 0; i < 4; i++) {
    await new Promise(r => setTimeout(r, 0));
    flush();
  }
};

function desk() {
  const asks: { key: string; res: (v: any) => void }[] = [];
  const ask = (key: string) => new Promise<any>(res => asks.push({ key, res }));
  const land = async (key: string, v: any = key) => {
    const i = asks.findIndex(a => a.key === key);
    if (i < 0) throw new Error(`no ask ${key}; open: ${asks.map(a => a.key)}`);
    asks.splice(i, 1)[0].res(v);
    await tick();
  };
  const drain = async () => {
    for (let n = 0; n < 30 && asks.length; n++) {
      for (const a of asks.splice(0)) a.res(a.key);
      await tick();
    }
  };
  return { ask, land, drain, open: () => asks.map(a => a.key).join(",") };
}

function mount(view: () => any) {
  const container = document.body.appendChild(document.createElement("div"));
  const dispose = render(view, container);
  return {
    frame: () => container.textContent!.replace(/\s+/g, " ").trim(),
    dispose: () => (dispose(), container.remove())
  };
}

test("H: a guess during an unrendered first load does not reveal <Loading> with an empty hole", async () => {
  const d = desk();
  let guess!: (x: string) => void;
  const app = mount(() => {
    const a1 = createMemo(() => d.ask("a1"));
    const a2 = createMemo(() => d.ask("a2"));
    const [, setQ] = createOptimistic(() => d.ask("q:" + a1()));
    guess = action(function* (x: string) {
      setQ(x);
    });
    return (
      <Loading fallback="LOADING">
        <div>
          [a1={a1()}] [a2={a2()}]
        </div>
      </Loading>
    );
  });
  flush();
  await tick();
  guess("G");
  flush();
  await tick();
  await d.land("a1");
  await d.land("a2");
  const mid = app.frame();
  await d.drain();
  app.dispose();
  expect(["[a1=a1] [a2=a2]", "LOADING"]).toContain(mid);
});

test("resolveTx: a guess during a first load read through createDeferred does not crash", async () => {
  const d = desk();
  const errs: string[] = [];
  const origError = console.error;
  console.error = (...a: any[]) => void errs.push(String(a[0]).split("\n")[0] + " " + String(a[1]?.message ?? ""));
  let setS!: (v: number) => void, act!: (v: number) => void;
  const app = mount(() => {
    const [s, b] = createSignal(0);
    setS = b;
    const [o, setO] = createOptimistic(() => d.ask("o:" + latest(s)));
    const f = createDeferred(() => o());
    act = action(function* (v: number) {
      setO("G" + v);
    });
    return <i>[f={f()}]</i>;
  });
  await tick();
  for (const fn of [() => setS(2), () => act(3), () => setS(3)]) {
    try {
      fn();
      flush();
      await tick();
    } catch (e: any) {
      errs.push(e?.message);
    }
  }
  try {
    await d.drain();
  } catch (e: any) {
    errs.push(e?.message);
  }
  console.error = origError;
  const settled = app.frame();
  app.dispose();
  expect({ errs, settled }).toEqual({ errs: [], settled: "[f=o:3]" });
});

test("R4C-1: latest() of a sync optimistic signal reverts when the action also guessed a first-loading node", async () => {
  const d = desk();
  let act!: (v: number) => void, o0!: () => number;
  const app = mount(() => {
    const [, setQ] = createOptimistic(() => d.ask("q0"));
    const [o, setO] = createOptimistic(0);
    o0 = o;
    act = action(function* (v: number) {
      setQ("G");
      setO(v * 10);
      yield d.ask("w");
    });
    return (
      <div>
        [o0={String(o())}][Lo0={String(latest(o))}]
      </div>
    );
  });
  await tick();
  act(3);
  flush();
  await tick();
  await d.drain();
  await tick();
  const settled = app.frame();
  const untracked = untrack(() => latest(o0));
  app.dispose();
  expect({ settled, untracked }).toEqual({ settled: "[o0=0][Lo0=0]", untracked: 0 });
});

How it was found: differential fuzzing of Solid against the ReactiveScript spec interpreter. The four came from separate rounds and were grouped by probing candidate branches against every open repro and the signals suite.

Lingua principale
TypeScript
Stelle
37k
Fork
1.2k
Merge medio
8h 43m
PR unite (30g)
365

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di solidjs/solid

Tutte le issue di solidjs/solid

Issue simili

Altre issue su TypeScript

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.