liveQuery SSR: the producer of a `live()` server function is never finalized after the response ends (the `return()` queues behind a read-ahead `next()`)
メンテナーはふだん 1 日以内に返信
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 48/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 技術スタック
- typescript
調査の方向性
Start by tracing the server-side liveQuery channel lifecycle through connect(), teardown(), and ch.close(), then inspect how live() in-process server functions receive cancellation signals. The report identifies a read-ahead next() that can block return() and provides a minimal repro and workaround. Confirm the lifecycle with the repro in both stream and async render modes; done means the producer's finally runs after the response ends without waiting for another yield.
索引モデルが issue の本文から書いたものです。
説明
Versions: solid-js 2.0.4, @solidjs/web 2.0.4, @solidjs/router 2.0.1, @solidjs/vite-plugin 3.0.1 (start mode, ssr: true, nodeServer: true), vite 8.4.0, filesystem-routing 0.5.0, Node 22.23.0. These are the current latest tags.
What happens
A route reads a liveQuery whose producer is a live() async-generator server function. On the server, the page renders the first value and the response ends normally. The producer's finally never runs, so each rendered page leaves one suspended generator for the life of the process, along with anything it holds (subscriptions, DB cursors, listeners).
Counting the calls the producer receives shows the cause. Per request, the channel calls next() twice and then return() once. The second next() comes from connect() reading ahead (while (true) { const r = await it.next(); ... }), and it moves the generator into its "wait for the next change" await. The return() from teardown() → ch.close() then queues behind that pending next(). An async generator can't be returned while it is executing, so finally waits until the next yield. With a watch-style wait (the README's own for await (const change of db.messages.watch(room))), that yield never comes.
The producer can't stop itself either. In SSR the call is in-process, and getRequestEvent().request.signal does not abort when the document response finishes. The invoke() docs say in-process work "runs on unless the function observes a signal itself", but there is no signal for it to observe.
Expected
Once the server render has released the channel and the response has ended, the producer is finalized (its finally runs). What I measured is that the in-process SSR call offers the producer no signal that fires when the document ends, and return() alone cannot stop a generator parked in an await. Possible fixes: don't read ahead past the first value on the document face, or offer the in-process call a signal that aborts when the render or response ends (and abort it on release).
Measured (20 plain-HTTP requests, no browser, counter read 15 s later)
| render mode | producer wait | open producers after 15 s |
|---|---|---|
| stream | never settles | 20 |
| async | never settles | 20 |
| stream / async | 1000 ms timer | 0 (only after the timer, plus one extra iteration each) |
| either + workaround below | never settles | 0 |
getRequestEvent().request.signal aborted: 0 of 20. Producer next() calls: 40, return() calls: 20.
Minimal repro
npx degit solidjs/templates/solid-v2/fullstack app && cd app. Pin the versions above (the currentlatest), then runnpm install.- Add a live server function and a page:
// src/lib/live.ts import { liveQuery } from '@solidjs/router'; import { live } from '@solidjs/web/server-functions'; export const counters = ((globalThis as any).__live ??= { open: 0 }); export const tick = live(async function* (room: string) { 'use server'; counters.open++; try { yield { room, n: 0 }; await new Promise(() => {}); // wait for a change that does not come } finally { counters.open--; } }); export const getTick = liveQuery(tick, 'tick');// src/routes/live.tsx import { createMemo } from 'solid-js'; import { getTick } from '../lib/live'; export default function Live() { const value = createMemo(() => getTick('lobby')); return <p>{value().room}: {value().n}</p>; }// src/routes/api/open.ts import type { APIHandler } from 'filesystem-routing/api'; import { counters } from '../../lib/live'; export const GET: APIHandler = () => Response.json(counters); npx vite build && SESSION_SECRET=$(openssl rand -base64 32) PORT=3000 node dist/server/node.jsfor i in $(seq 20); do curl -s localhost:3000/live > /dev/null; done; sleep 5; curl localhost:3000/api/openprints{"open":20}. Expected:{"open":0}.
Workaround
Add a middleware that puts an AbortSignal on event.locals, wraps response.body, and aborts the signal when the body finishes or is canceled. The producer then races its wait against that signal and returns when it aborts:
const abortWhenResponseEnds: StartMiddleware = async (event, next) => {
const controller = new AbortController();
event.locals.responseEnded = controller.signal;
const response = await next();
if (!response.body) { controller.abort(); return response; }
const reader = response.body.getReader();
return new Response(new ReadableStream({
async pull(c) {
const { done, value } = await reader.read();
if (done) { c.close(); controller.abort(); } else c.enqueue(value);
},
cancel(reason) { controller.abort(); return reader.cancel(reason); },
}), response);
};
With it, all 20 producers are finalized when their responses end, in both render modes.
Related: solidjs/solid#3626 and solidjs/solid#3628 close serialized iterators when a render is abandoned. This case is a normally ended render and the router's request-scoped producer. solidjs/solid#3125 covers HTTP response-path teardown, and solidjs/solid#3057 per-call cancellation (invoke), whose docs say in-process work "runs on unless the function observes a signal itself").
Sent using @Claude
- 主要言語
- TypeScript
- スター
- 1.3k
- フォーク
- 193
- 平均マージ
- 8時間 48分
- マージ済み PR(30日)
- 21
環境構築
- Dockerfile・Docker Compose ファイルなし
- プルリクエストのテンプレートなし
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
solidjs/solid-router のほかの issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
solidjs/solid-router#757 ·
メンテナーはふだん 1 日以内に返信
-
[2.0.1] Internal links do a full page load on a custom scheme (Electron app://)対応中かも @saudademjj が 1 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 20/100
solidjs/solid-router#755 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
[2.0.1] diagnostics: a request for // crashes the dev server (Invalid URL in describeInitial)対応中かも @bryanlundberg が 4 日前に担当しました。 オープン
難易度 2/5 1〜3時間 初心者へのやさしさ 35/100
solidjs/solid-router#752 ·
メンテナーはふだん 1 日以内に返信
-
[2.0.1] hashHistory: link claims and useLinkState ignore the hash, so aria-current/data-active are wrong on every link対応中かも @clean99 が 9 日前に担当しました。 オープン
難易度 3/5 1〜2日 初心者へのやさしさ 72/100
solidjs/solid-router#746 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 48/100
solidjs/solid-router#736 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
solidjs/solid-router の issue をすべて見る
似ている issue
-
area:api bug good first issue priority:high
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
Doezer/Questarr#1486 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
area:import difficulty:good-first good first issue help wanted priority:P1 roadmap size:S type:test
難易度 2/5 1〜3時間 初心者へのやさしさ 88/100
Sergey-Bar/Penny#245 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
mihazs/lazysh#3377 · コメント 2 件 ·
メンテナーはふだん 1 日以内に返信
-
Approve the Cloudflare agent plugin for harness sessions対応中かも @joshtsch が今日担当しました。 オープンenhancement ready-for-agent
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信