Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(llm): stop the chat SSE heartbeat as soon as the client disconnects
ltomazetto0e0ee3cdb104
diff --git a/apps/server/src/routes/api/llm_chat.spec.ts b/apps/server/src/routes/api/llm_chat.spec.tsindex 57660d352c..84b24881f6 100644--- a/apps/server/src/routes/api/llm_chat.spec.ts+++ b/apps/server/src/routes/api/llm_chat.spec.ts@@ -168,5 +168,24 @@ describe("streamChat", () => {'data: {"type":"done"}\n\n']);});++ it("stops the heartbeat when the client disconnects while the provider is still waiting", async () => {+ state.chunks = [{ type: "done" }];+ state.delaysMs = [SSE_HEARTBEAT_MS * 2];+ const r = fakeRes({ withFlush: true });+ const finished = llmChatRoute.streamChat(chatReq(), r.res);+ await vi.advanceTimersByTimeAsync(0);++ r.listeners.close();+ expect(state.signal?.aborted).toBe(true);++ await vi.advanceTimersByTimeAsync(SSE_HEARTBEAT_MS);+ expect(r.writes).toEqual([]);++ await vi.advanceTimersByTimeAsync(SSE_HEARTBEAT_MS);+ await finished;+ expect(r.writes).toEqual([]);+ expect(r.ended).toBe(true);+ });});});diff --git a/apps/server/src/routes/api/llm_chat.ts b/apps/server/src/routes/api/llm_chat.tsindex dc8d872bb8..b1f23d709c 100644--- a/apps/server/src/routes/api/llm_chat.ts+++ b/apps/server/src/routes/api/llm_chat.ts@@ -48,10 +48,10 @@ async function streamChat(req: Request, res: Response) {// Type assertion for flush method (available when compression is used)const flushableRes = res as Response & { flush?: () => void };- // Stop the turn when the client disconnects mid-stream, so a closed tab- // doesn't leave an agent loop running against the provider.+ // Abort the provider turn when the client disconnects, so a closed tab+ // does not leave an agent loop running. Aborting `runChat` does not always+ // settle at once, so the heartbeat stops here rather than in `finally`.const abortController = new AbortController();- res.on("close", () => abortController.abort());let stopped = false;const writeFrame = (frame: string) => {@@ -65,6 +65,11 @@ async function streamChat(req: Request, res: Response) {};const heartbeat = startSseHeartbeat(() => writeFrame(SSE_HEARTBEAT_FRAME));+ res.on("close", () => {+ stopped = true;+ heartbeat.stop();+ abortController.abort();+ });try {// Imported here rather than at module scope so the chat pipeline and