[PR #53] [MERGED] fix: abort backend generation when user clicks Stop #4423

Closed
opened 2026-10-22 04:07:29 +07:00 by bsakaguchi · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/Pasta-Devs/Marinara-Engine/pull/53
Author: @LukaTheHero
Created: 4/3/2026
Status: ✅ Merged
Merged: 4/3/2026
Merged by: @SpicyMarinara

Base: main ← Head: fix/abort-signal-propagation


📝 Commits (1)

  • 4acfde0 fix: abort backend generation when user clicks Stop

📊 Changes

5 files changed (+109 additions, -2 deletions)

View changed files

📝 packages/client/src/stores/chat.store.ts (+7 -0)
📝 packages/server/src/routes/generate.routes.ts (+63 -1)
📝 packages/server/src/services/llm/providers/anthropic.provider.ts (+10 -0)
📝 packages/server/src/services/llm/providers/google.provider.ts (+10 -0)
📝 packages/server/src/services/llm/providers/openai.provider.ts (+19 -1)

📄 Description

Fixes a bug where clicking Stop in the UI would remove the reply from the chat but the backend (e.g. KoboldCPP) would continue generating tokens until completion, blocking the slot.

Problem

When a user stops generation:

  1. Client aborts the SSE fetch → streaming stops in the UI
  2. Server's req.raw.on("close") event doesn't reliably fire for SSE connections
  3. Backend (KoboldCPP, etc.) keeps generating tokens indefinitely

This affects all local backends that don't automatically stop when the TCP connection drops. Cloud providers (OpenAI, Anthropic) also continued consuming tokens unnecessarily.

Solution

Explicit abort endpoint (/api/generate/abort)

  • New POST endpoint that the client calls when the user clicks Stop
  • Tracks active generations per chat with their abort controllers and backend URLs
  • Sends POST /api/extra/abort to KoboldCPP-compatible backends to free the slot
  • Fire-and-forget on the client side — doesn't block the UI

Provider-level reader cancellation

  • All three LLM providers (OpenAI, Anthropic, Google) now register an abort signal listener that calls reader.cancel() on the active stream reader
  • Ensures the TCP connection to the backend is closed immediately when abort fires
  • Prevents wasted tokens/API credits on cloud providers
  • Cleanup via removeEventListener in finally blocks to prevent memory leaks

Files Changed

  • packages/client/src/stores/chat.store.ts — stopGeneration() now calls /api/generate/abort
  • packages/server/src/routes/generate.routes.ts — new /abort endpoint + active generation tracking
  • packages/server/src/services/llm/providers/openai.provider.ts — reader cancellation on abort
  • packages/server/src/services/llm/providers/anthropic.provider.ts — reader cancellation on abort
  • packages/server/src/services/llm/providers/google.provider.ts — reader cancellation on abort

Testing

Tested with KoboldCPP — clicking Stop now immediately shows "Generation Aborted" in KoboldCPP's terminal and frees the slot for new requests.
Also tested other generations my fix didn't effect anything. but if something were to fail somehow Only potential edge case would be:
The /api/extra/abort call goes to whatever baseUrl the connection is configured with. For cloud providers like https://api.openai.com/v1, it would try to hit https://api.openai.com/api/extra/abort — which doesn't exist. But since the fetch has .catch(() => {}), it silently fails with a 404 and nothing happens. No impact.
Bottom line: The worst case for any provider is a single failed HTTP request that gets silently swallowed. Normal generation flow is completely untouched — these changes only activate when the user clicks Stop.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/Pasta-Devs/Marinara-Engine/pull/53 **Author:** [@LukaTheHero](https://github.com/LukaTheHero) **Created:** 4/3/2026 **Status:** ✅ Merged **Merged:** 4/3/2026 **Merged by:** [@SpicyMarinara](https://github.com/SpicyMarinara) **Base:** `main` ← **Head:** `fix/abort-signal-propagation` --- ### 📝 Commits (1) - [`4acfde0`](https://github.com/Pasta-Devs/Marinara-Engine/commit/4acfde00b4e0b96bf9db98f7c1fe8f51addc7ef6) fix: abort backend generation when user clicks Stop ### 📊 Changes **5 files changed** (+109 additions, -2 deletions) <details> <summary>View changed files</summary> 📝 `packages/client/src/stores/chat.store.ts` (+7 -0) 📝 `packages/server/src/routes/generate.routes.ts` (+63 -1) 📝 `packages/server/src/services/llm/providers/anthropic.provider.ts` (+10 -0) 📝 `packages/server/src/services/llm/providers/google.provider.ts` (+10 -0) 📝 `packages/server/src/services/llm/providers/openai.provider.ts` (+19 -1) </details> ### 📄 Description Fixes a bug where clicking Stop in the UI would remove the reply from the chat but the backend (e.g. KoboldCPP) would continue generating tokens until completion, blocking the slot. ## Problem When a user stops generation: 1. Client aborts the SSE fetch → streaming stops in the UI 2. Server's `req.raw.on("close")` event doesn't reliably fire for SSE connections 3. Backend (KoboldCPP, etc.) keeps generating tokens indefinitely This affects all local backends that don't automatically stop when the TCP connection drops. Cloud providers (OpenAI, Anthropic) also continued consuming tokens unnecessarily. ## Solution ### Explicit abort endpoint (`/api/generate/abort`) - New POST endpoint that the client calls when the user clicks Stop - Tracks active generations per chat with their abort controllers and backend URLs - Sends `POST /api/extra/abort` to KoboldCPP-compatible backends to free the slot - Fire-and-forget on the client side — doesn't block the UI ### Provider-level reader cancellation - All three LLM providers (OpenAI, Anthropic, Google) now register an abort signal listener that calls `reader.cancel()` on the active stream reader - Ensures the TCP connection to the backend is closed immediately when abort fires - Prevents wasted tokens/API credits on cloud providers - Cleanup via `removeEventListener` in `finally` blocks to prevent memory leaks ## Files Changed - `packages/client/src/stores/chat.store.ts` — `stopGeneration()` now calls `/api/generate/abort` - `packages/server/src/routes/generate.routes.ts` — new `/abort` endpoint + active generation tracking - `packages/server/src/services/llm/providers/openai.provider.ts` — reader cancellation on abort - `packages/server/src/services/llm/providers/anthropic.provider.ts` — reader cancellation on abort - `packages/server/src/services/llm/providers/google.provider.ts` — reader cancellation on abort ## Testing Tested with KoboldCPP — clicking Stop now immediately shows "Generation Aborted" in KoboldCPP's terminal and frees the slot for new requests. Also tested other generations my fix didn't effect anything. but if something were to fail somehow Only potential edge case would be: The /api/extra/abort call goes to whatever baseUrl the connection is configured with. For cloud providers like https://api.openai.com/v1, it would try to hit https://api.openai.com/api/extra/abort — which doesn't exist. But since the fetch has .catch(() => {}), it silently fails with a 404 and nothing happens. No impact. Bottom line: The worst case for any provider is a single failed HTTP request that gets silently swallowed. Normal generation flow is completely untouched — these changes only activate when the user clicks Stop. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
bsakaguchi 2026-10-22 04:07:29 +07:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
Pasta-Devs/Marinara-Engine#4423
No description provided.