Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/calm-producers-close.md
Original file line number Diff line number Diff line change
@@ -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.
15 changes: 14 additions & 1 deletion src/data/liveQuery.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, Channel>();
Expand Down Expand Up @@ -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<void>(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;
Expand Down Expand Up @@ -258,7 +268,10 @@ function subscriberIterable(open: (resume?: any) => Channel) {
release();
return { done: true, value: undefined };
}
await new Promise<void>(resolve => ch.waiters.add(resolve));
await new Promise<void>(resolve => {
ch.waiters.add(resolve);
ch.pull?.();
});
}
};
return {
Expand Down
37 changes: 37 additions & 0 deletions test/server/live-query-ssr.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down