Skip to content

Self-hosted, Self-Improving, Federated Everything App — dev management, agent orchestration, persistent mind, digital twin, second brain, and creative pipeline from a single dashboard over Tailscale to port the user into the OS

fix([llm-playground-cancel-in-flight]): guard AbortSignal.any for Node 18

[._.]/ Adam Eivy04043b12d31b
diff --git a/server/lib/README.md b/server/lib/README.md
index a43d49365d..4709da5b56 100644
--- a/server/lib/README.md
+++ b/server/lib/README.md
@@ -123,7 +123,7 @@ The barrel `server/lib/index.js` is a machine-checkable enumeration of every pub
|---|---|
| `httpClient.js` | Fetch-based HTTP client factory (axios.create replacement). |
| `fetchWithTimeout.js` | `fetch` wrapper with AbortController timeout. |
-| `requestAbort.js` | `abortSignalFromResponse(res)` — AbortSignal that fires only when an Express client disconnects *before the response finishes*, so a streaming handler can tear down its upstream read on cancel (keyed off `res` close + `writableEnded`, not `req`). |
+| `requestAbort.js` | `abortSignalFromResponse(res)` — AbortSignal that fires only when an Express client disconnects *before the response finishes* (keyed off `res` close + `writableEnded`). Plus `anyAbortSignal(signals)` — combine several signals into one (native `AbortSignal.any` with a Node-18 fallback). |
| `readResponseJson.js` | Read a `Response` body as JSON, tolerating a non-JSON/HTML error page (no `Unexpected token <` crash). Object callers need no opts; pass `{ fallback, emptyValue }` for arrays or to surface the raw error text. |
| `peerHttpClient.js` | Federation HTTP/Socket.IO client (TLS validation off — Tailnet is the trust boundary). |
| `peerSelfHost.js` | Tailscale-issued hostname this PortOS sends in federation. |
diff --git a/server/lib/requestAbort.js b/server/lib/requestAbort.js
index a1170e9cc8..ef25dfd0f9 100644
--- a/server/lib/requestAbort.js
+++ b/server/lib/requestAbort.js
@@ -15,6 +15,30 @@
//
// The listener only calls `controller.abort()`, which can't throw — safe to attach
// outside the request lifecycle without a try/catch.
+// Combine several AbortSignals into one that fires when any of them aborts.
+// Prefers the native `AbortSignal.any` (Node 20.3+ / 18.17+) and falls back to a
+// manual fan-in for older Node 18 builds — PortOS's `package.json` engines accept
+// Node 18, and `AbortSignal.any` is absent on the early-18 line. Mirrors the guard
+// in `fetchWithTimeout.js`. The fallback's listeners are `{ once: true }` and self-
+// removing on first abort, so there's nothing to clean up.
+export function anyAbortSignal(signals) {
+ const live = signals.filter(Boolean);
+ if (live.length === 0) return undefined;
+ if (live.length === 1) return live[0];
+ if (typeof AbortSignal !== 'undefined' && typeof AbortSignal.any === 'function') {
+ return AbortSignal.any(live);
+ }
+ const controller = new AbortController();
+ for (const signal of live) {
+ if (signal.aborted) {
+ controller.abort();
+ break;
+ }
+ signal.addEventListener('abort', () => controller.abort(), { once: true });
+ }
+ return controller.signal;
+}
+
export function abortSignalFromResponse(res) {
const controller = new AbortController();
if (res?.writableEnded) return controller.signal; // finished normally — never a cancel
diff --git a/server/lib/requestAbort.test.js b/server/lib/requestAbort.test.js
index 0dd733f083..0b400f5bf3 100644
--- a/server/lib/requestAbort.test.js
+++ b/server/lib/requestAbort.test.js
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest';
import { EventEmitter } from 'node:events';
-import { abortSignalFromResponse } from './requestAbort.js';
+import { abortSignalFromResponse, anyAbortSignal } from './requestAbort.js';
// Minimal stand-in for an Express response: an EventEmitter carrying the
// `writableEnded` flag the helper keys off.
@@ -43,3 +43,31 @@ describe('abortSignalFromResponse', () => {
expect(abortSignalFromResponse(undefined).aborted).toBe(false);
});
});
+
+describe('anyAbortSignal', () => {
+ it('returns undefined when no live signals are given', () => {
+ expect(anyAbortSignal([])).toBeUndefined();
+ expect(anyAbortSignal([null, undefined])).toBeUndefined();
+ });
+
+ it('returns the sole signal unchanged when only one is live', () => {
+ const c = new AbortController();
+ expect(anyAbortSignal([null, c.signal])).toBe(c.signal);
+ });
+
+ it('fires when any source signal aborts', () => {
+ const a = new AbortController();
+ const b = new AbortController();
+ const combined = anyAbortSignal([a.signal, b.signal]);
+ expect(combined.aborted).toBe(false);
+ b.abort();
+ expect(combined.aborted).toBe(true);
+ });
+
+ it('is already aborted when a source was aborted before combining', () => {
+ const a = new AbortController();
+ const b = new AbortController();
+ a.abort();
+ expect(anyAbortSignal([a.signal, b.signal]).aborted).toBe(true);
+ });
+});
diff --git a/server/services/localLlmPlayground.js b/server/services/localLlmPlayground.js
index 095c9be226..72036ede00 100644
--- a/server/services/localLlmPlayground.js
+++ b/server/services/localLlmPlayground.js
@@ -5,6 +5,7 @@ import { getProviderById } from './providers.js';
import { markProviderAvailable } from './providerStatus.js';
import { ensureProviderReady as ensureOllamaProviderReady } from './ollamaManager.js';
import { readResponseJson } from '../lib/readResponseJson.js';
+import { anyAbortSignal } from '../lib/requestAbort.js';
const PROVIDER_BY_BACKEND = { ollama: 'ollama', lmstudio: 'lmstudio' };
@@ -210,14 +211,12 @@ export async function runLocalLlmTest({
// The timeout controller aborts the upstream read (its plain AbortError keeps
// the "Timed out after Xms" mapping below). A client disconnect — the user hit
- // Cancel, closing the browser fetch — aborts `clientSignal`; AbortSignal.any
+ // Cancel, closing the browser fetch — aborts `clientSignal`; `anyAbortSignal`
// composes both so whichever fires first tears down the upstream reader instead
// of running on to the full timeout with no one listening.
const timeoutController = new AbortController();
const timeoutHandle = setTimeout(() => timeoutController.abort(), timeoutMs);
- const signal = clientSignal
- ? AbortSignal.any([clientSignal, timeoutController.signal])
- : timeoutController.signal;
+ const signal = anyAbortSignal([clientSignal, timeoutController.signal]);
const text = await streamChatCompletion({
provider,
backend,