diff --git a/.changeset/calm-producers-close.md b/.changeset/calm-producers-close.md new file mode 100644 index 00000000..3d01f1a6 --- /dev/null +++ b/.changeset/calm-producers-close.md @@ -0,0 +1,5 @@ +--- +"@solidjs/router": patch +--- + +Fix liveQuery leaving its producer open after a server render: a request-scoped channel read one value ahead of its consumers, so a producer that waits for a change after its first yield was parked in that wait when the document took the first value and left, and the closing `return()` queued behind the pending `next()` without ever running the producer's `finally`. Request-scoped channels now pull from the producer only when a consumer is waiting for a value, so the producer rests at its `yield` and closes with its last consumer. diff --git a/src/data/liveQuery.ts b/src/data/liveQuery.ts index 14b2573c..bffa5b37 100644 --- a/src/data/liveQuery.ts +++ b/src/data/liveQuery.ts @@ -57,6 +57,8 @@ type Channel = { retain: boolean; /** the hydrated value the first connection resumes from (see LIVE_RESUME_FROM) */ resume?: any; + /** releases a pump parked for demand (request-scoped channels only) */ + pull?: () => void; }; const channelMap = new Map(); @@ -130,6 +132,14 @@ async function connect(ch: Channel) { } catch {} }; while (true) { + // Request-scoped channels pull on demand, not ahead: the producer + // rests at its yield between values, where the closing return() + // reaches it. A read-ahead next() would park the producer in its + // wait for a change, and return() queues behind that call for good. + if (ch.retain && !ch.waiters.size) { + await new Promise(resolve => (ch.pull = resolve)); + if (gen !== ch.gen || ch.done) return; + } const r = await it.next(); if (gen !== ch.gen || ch.done) return; if (r.done) break; @@ -258,7 +268,10 @@ function subscriberIterable(open: (resume?: any) => Channel) { release(); return { done: true, value: undefined }; } - await new Promise(resolve => ch.waiters.add(resolve)); + await new Promise(resolve => { + ch.waiters.add(resolve); + ch.pull?.(); + }); } }; return { diff --git a/test/server/live-query-ssr.spec.ts b/test/server/live-query-ssr.spec.ts index 331d3597..186c254d 100644 --- a/test/server/live-query-ssr.spec.ts +++ b/test/server/live-query-ssr.spec.ts @@ -70,6 +70,43 @@ describe("liveQuery on the server", () => { }); }); + test("request scope: the producer closes with its last consumer", async () => { + const { provideRequestEvent } = await import("@solidjs/web/storage"); + let open = 0; + const lq = liveQuery(async function* () { + open++; + try { + yield "current"; + await new Promise(() => {}); // waits for a change that never comes + } finally { + open--; + } + }, "ssr-finalize"); + const event = { request: new Request("http://localhost/"), locals: {} } as any; + await provideRequestEvent(event, async () => { + const it = (lq() as any)[Symbol.asyncIterator](); + expect((await it.next()).value).toBe("current"); + expect(open).toBe(1); + await it.return(); + await new Promise(r => setTimeout(r, 0)); // deferred teardown + expect(open).toBe(0); + }); + }); + + test("request scope: a consumer that keeps pulling receives each value, then the end", async () => { + const { provideRequestEvent } = await import("@solidjs/web/storage"); + const lq = liveQuery(async function* () { + yield "first"; + yield "second"; + }, "ssr-demand"); + const event = { request: new Request("http://localhost/"), locals: {} } as any; + await provideRequestEvent(event, async () => { + const seen: string[] = []; + for await (const v of lq() as any) seen.push(v); + expect(seen).toEqual(["first", "second"]); + }); + }); + test("separate requests do not share channels", async () => { const { provideRequestEvent } = await import("@solidjs/web/storage"); let invocations = 0;