|
| 1 | +/** |
| 2 | + * The promise-delivery readers — until(), resolve() (and awaitable refresh()'s |
| 3 | + * waiter; CONFIG_DIRECT_COMMIT) — by posture, over a frame HELD under an |
| 4 | + * action's optimism (#3482). |
| 5 | + * |
| 6 | + * The shape: an optimistic row on the store a live stream feeds, confirmed by |
| 7 | + * the server's echo. The server broadcasts before it answers the mutation, so |
| 8 | + * the confirming frame lands while the action is in flight and is staged under |
| 9 | + * its hold — the UI keeps showing the optimistic row until the action settles. |
| 10 | + * |
| 11 | + * Two postures for a reader created after that frame is held: |
| 12 | + * |
| 13 | + * - INSIDE the action, after a bare `yield` re-entered the transaction: the |
| 14 | + * reader is the action's own. `recompute`'s DIRECT_COMMIT arm delivers its |
| 15 | + * result on a microtask under the action's own hold — the predicate sees the |
| 16 | + * held frame, the action settles, the commit reveals frame + overlay revert. |
| 17 | + * This is the documented form (`action()`: code between an `await` and the |
| 18 | + * next `yield` runs outside the transaction; the expression of |
| 19 | + * `yield until(...)` is evaluated BEFORE that yield re-enters). |
| 20 | + * |
| 21 | + * - MAINLINE — outside the action, or inside it from an `await` continuation |
| 22 | + * with no bare `yield` first: the reader is not the action's. It is born |
| 23 | + * held (A29) and replays at the commit: it serves the COMMITTED view, never |
| 24 | + * the foreign hold's unrevealed frame. From outside the action that is the |
| 25 | + * only correct answer — `resolve()` is routinely called outside actions, and |
| 26 | + * an in-flight action's speculative frame must not leak to it. From inside |
| 27 | + * the action without the bare `yield` it is the deadlock #3482 reported: |
| 28 | + * the commit the reader waits for is the settle its own promise holds open. |
| 29 | + * Same mechanism, one posture — which is why an exemption for the |
| 30 | + * direct-commit readers in `enterStagedRead` (proposed in #3482, declined in |
| 31 | + * #3490) is wrong: it cannot tell the two apart and serves the leak to fix |
| 32 | + * the deadlock. |
| 33 | + * |
| 34 | + * Reproduction and analysis by @brenelz (#3482); docs fixed in #3491. |
| 35 | + */ |
| 36 | +import { expect, test } from "vitest"; |
| 37 | +import { |
| 38 | + action, |
| 39 | + createOptimisticStore, |
| 40 | + createRenderEffect, |
| 41 | + createRoot, |
| 42 | + flush, |
| 43 | + resolve, |
| 44 | + TimeoutError, |
| 45 | + until |
| 46 | +} from "../src/index.js"; |
| 47 | + |
| 48 | +type Row = { id: string; status: "pending" | "confirmed" }; |
| 49 | +type Snapshot = { rows: Row[] }; |
| 50 | + |
| 51 | +const settle = async (n = 3) => { |
| 52 | + for (let i = 0; i < n; i++) { |
| 53 | + await new Promise(r => setTimeout(r, 0)); |
| 54 | + flush(); |
| 55 | + } |
| 56 | +}; |
| 57 | + |
| 58 | +// A manually pumped AsyncIterable — what a live() server-function stream |
| 59 | +// materializes as on the client. |
| 60 | +function stream<T>() { |
| 61 | + const buffered: IteratorResult<T>[] = []; |
| 62 | + let waiter: ((r: IteratorResult<T>) => void) | null = null; |
| 63 | + const iterable: AsyncIterable<T> = { |
| 64 | + [Symbol.asyncIterator]: () => ({ |
| 65 | + next: () => |
| 66 | + new Promise<IteratorResult<T>>(res => { |
| 67 | + if (buffered.length) res(buffered.shift()!); |
| 68 | + else waiter = res; |
| 69 | + }), |
| 70 | + return: () => Promise.resolve({ done: true as const, value: undefined }) |
| 71 | + }) |
| 72 | + }; |
| 73 | + return { |
| 74 | + iterable, |
| 75 | + push(value: T) { |
| 76 | + const r = { done: false as const, value }; |
| 77 | + if (waiter) { |
| 78 | + const w = waiter; |
| 79 | + waiter = null; |
| 80 | + w(r); |
| 81 | + } else buffered.push(r); |
| 82 | + } |
| 83 | + }; |
| 84 | +} |
| 85 | + |
| 86 | +const CONFIRMED: Snapshot = { rows: [{ id: "res_1", status: "confirmed" }] }; |
| 87 | + |
| 88 | +/** Optimistic row + live feed, the action's body supplied by the test. */ |
| 89 | +function harness(body: (ctx: { store: Snapshot; mutation: Promise<Row> }) => AsyncGenerator) { |
| 90 | + const feed = stream<Snapshot>(); |
| 91 | + let answer!: (row: Row) => void; |
| 92 | + const mutation = new Promise<Row>(res => (answer = res)); |
| 93 | + const views: string[] = []; |
| 94 | + let run!: () => Promise<unknown>; |
| 95 | + let store!: Snapshot; |
| 96 | + |
| 97 | + createRoot(() => { |
| 98 | + const [s, setStore] = createOptimisticStore<Snapshot>( |
| 99 | + () => feed.iterable, |
| 100 | + { rows: [] }, |
| 101 | + { |
| 102 | + key: "id" |
| 103 | + } |
| 104 | + ); |
| 105 | + store = s; |
| 106 | + run = action(async function* () { |
| 107 | + setStore(d => { |
| 108 | + d.rows.push({ id: "temp", status: "pending" }); |
| 109 | + }); |
| 110 | + yield* body({ store: s, mutation }); |
| 111 | + }); |
| 112 | + createRenderEffect( |
| 113 | + () => s.rows.map(r => `${r.id}:${r.status}`).join(",") || "empty", |
| 114 | + v => { |
| 115 | + views.push(v); |
| 116 | + } |
| 117 | + ); |
| 118 | + }); |
| 119 | + |
| 120 | + return { |
| 121 | + feed, |
| 122 | + answer: () => answer({ id: "res_1", status: "confirmed" }), |
| 123 | + views, |
| 124 | + run, |
| 125 | + get store() { |
| 126 | + return store; |
| 127 | + } |
| 128 | + }; |
| 129 | +} |
| 130 | + |
| 131 | +test("inside the action after a bare yield: until() sees the frame held under its own action", async () => { |
| 132 | + const h = harness(async function* ({ store, mutation }) { |
| 133 | + const saved = await mutation; |
| 134 | + yield; // re-enter the transaction — the documented form |
| 135 | + yield until(() => store.rows.some(r => r.id === saved.id), { timeout: 200 }); |
| 136 | + }); |
| 137 | + h.feed.push({ rows: [] }); |
| 138 | + await settle(); |
| 139 | + const done = h.run().then( |
| 140 | + () => "settled", |
| 141 | + e => e |
| 142 | + ); |
| 143 | + await settle(); |
| 144 | + h.feed.push(CONFIRMED); // broadcast first: held under the action |
| 145 | + await settle(); |
| 146 | + expect(h.views.at(-1)).toBe("temp:pending"); |
| 147 | + h.answer(); |
| 148 | + await settle(); |
| 149 | + expect(await done).toBe("settled"); |
| 150 | + await settle(); |
| 151 | + expect(h.views.at(-1)).toBe("res_1:confirmed"); |
| 152 | +}); |
| 153 | + |
| 154 | +test("inside the action WITHOUT the bare yield: until() is mainline, born held, and times out (#3482 as filed)", async () => { |
| 155 | + const h = harness(async function* ({ store, mutation }) { |
| 156 | + const saved = await mutation; |
| 157 | + // `until(...)` is evaluated in the await continuation, before this yield |
| 158 | + // re-enters — a mainline reader over the action's own hold. |
| 159 | + yield until(() => store.rows.some(r => r.id === saved.id), { timeout: 100 }); |
| 160 | + }); |
| 161 | + h.feed.push({ rows: [] }); |
| 162 | + await settle(); |
| 163 | + const done = h.run().then( |
| 164 | + () => "settled", |
| 165 | + e => e |
| 166 | + ); |
| 167 | + await settle(); |
| 168 | + h.feed.push(CONFIRMED); |
| 169 | + await settle(); |
| 170 | + h.answer(); |
| 171 | + await settle(); |
| 172 | + await new Promise(r => setTimeout(r, 120)); |
| 173 | + await settle(); |
| 174 | + expect(await done).toBeInstanceOf(TimeoutError); |
| 175 | +}); |
| 176 | + |
| 177 | +test("mainline, outside the action: resolve() and until() over a foreign hold serve the committed view, after the commit — never the held frame", async () => { |
| 178 | + let release!: () => void; |
| 179 | + const gate = new Promise<void>(r => (release = r)); |
| 180 | + const h = harness(async function* ({ mutation }) { |
| 181 | + await mutation; |
| 182 | + yield; |
| 183 | + yield gate; // keep the hold open past the answer |
| 184 | + }); |
| 185 | + h.feed.push({ rows: [] }); |
| 186 | + await settle(); |
| 187 | + const done = h.run().then( |
| 188 | + () => "settled", |
| 189 | + e => e |
| 190 | + ); |
| 191 | + await settle(); |
| 192 | + h.feed.push(CONFIRMED); |
| 193 | + await settle(); |
| 194 | + expect(h.views.at(-1)).toBe("temp:pending"); |
| 195 | + |
| 196 | + // Outside the action, after the frame is held. A reader here is not the |
| 197 | + // action's: it must not see the unrevealed frame. |
| 198 | + let resolved: unknown = "pending"; |
| 199 | + let acked: unknown = "pending"; |
| 200 | + resolve(() => h.store.rows.map(r => r.id).join(",") || "empty").then( |
| 201 | + v => (resolved = v), |
| 202 | + e => (resolved = e) |
| 203 | + ); |
| 204 | + until(() => h.store.rows.some(r => r.id === "res_1"), { timeout: 1000 }).then( |
| 205 | + v => (acked = v), |
| 206 | + e => (acked = e) |
| 207 | + ); |
| 208 | + h.answer(); |
| 209 | + await settle(); |
| 210 | + expect(h.views.at(-1)).toBe("temp:pending"); |
| 211 | + expect(resolved).toBe("pending"); |
| 212 | + expect(acked).toBe("pending"); |
| 213 | + |
| 214 | + release(); |
| 215 | + await settle(); |
| 216 | + expect(await done).toBe("settled"); |
| 217 | + await settle(); |
| 218 | + expect(h.views.at(-1)).toBe("res_1:confirmed"); |
| 219 | + expect(resolved).toBe("res_1"); |
| 220 | + expect(acked).toBe(true); |
| 221 | +}); |
0 commit comments