Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

fix(license): time out a reply whose BODY never arrives

enesb3b954a46e92
diff --git a/src/core/license.test.ts b/src/core/license.test.ts
index 511ff287b..1dece6df5 100644
--- a/src/core/license.test.ts
+++ b/src/core/license.test.ts
@@ -529,6 +529,44 @@ describe('license detail + release', () => {
expect((await detail()).error).toBe('unauthorized')
})
+ it('times out a response whose BODY never arrives, instead of hanging the handler forever', async () => {
+ // The 8s abort has to cover the body, not just the headers. A captive portal or a proxy that
+ // holds the connection open answers 200 and then stalls; with the timer cleared as soon as the
+ // headers land, this handler never settles — and the renderer's Release button sits on
+ // "Releasing…" for the rest of the session, with no failure to report and no way back.
+ storeToken()
+ // Captured while setTimeout is still real (this describe fakes only setInterval + Date), so
+ // the guard below can fail FAST if the handler hangs rather than idling until vitest's own
+ // test timeout.
+ const realSetTimeout = globalThis.setTimeout
+ let aborted = false
+ await boot(
+ vi.fn(async (_url: string, init: { signal: AbortSignal }) => ({
+ ok: true,
+ status: 200,
+ // A real fetch body rejects when the request is aborted; before that it just never settles.
+ json: () =>
+ new Promise((_resolve, reject) => {
+ init.signal.addEventListener('abort', () => {
+ aborted = true
+ reject(new Error('The operation was aborted'))
+ })
+ })
+ }))
+ )
+
+ vi.useFakeTimers({ toFake: ['setTimeout', 'clearTimeout', 'setInterval', 'Date'] })
+ const pending = release()
+ await vi.advanceTimersByTimeAsync(8000)
+ const guard = new Promise<'hung'>((resolve) => realSetTimeout(() => resolve('hung'), 500))
+ const outcome = await Promise.race([pending, guard])
+
+ expect(outcome, 'the release handler never settled').not.toBe('hung')
+ expect(aborted).toBe(true)
+ // A read that could not run is an error — never a license with no devices.
+ expect(outcome).toEqual({ key: null, used: 0, seats: 0, source: null, error: 'network' })
+ })
+
it('reports an error instead of zero devices when the read fails', async () => {
storeToken()
await boot(
diff --git a/src/core/license.ts b/src/core/license.ts
index 04b4e7d90..8ace406e1 100644
--- a/src/core/license.ts
+++ b/src/core/license.ts
@@ -141,38 +141,53 @@ export function licensedSeats(): number {
return seatsFrom(verify(load().token))
}
+/**
+ * How long a license request may take IN TOTAL — headers and body.
+ *
+ * The timer must outlive the `fetch` promise and be cleared only once the body has been read.
+ * Clearing it when the headers arrive (`fetch(…).finally(clearTimeout)`) disarms the abort while
+ * the interesting half is still outstanding, and a response whose body never completes — a captive
+ * portal, a proxy that holds the connection open — then hangs the awaiting IPC handler FOREVER:
+ * the Release button sits on "Releasing…" with no failure and no way back.
+ */
+const REQUEST_TIMEOUT_MS = 8000
+
async function call(path: string, body: unknown): Promise<{ token?: string; error?: string }> {
if (!allowed()) return { error: 'disabled' }
+ const ctrl = new AbortController()
+ const t = setTimeout(() => ctrl.abort(), REQUEST_TIMEOUT_MS)
try {
- const ctrl = new AbortController()
- const t = setTimeout(() => ctrl.abort(), 8000)
const res = await fetch(`${API_BASE}${path}`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify(body),
signal: ctrl.signal
- }).finally(() => clearTimeout(t))
+ })
if (res.status === 204) return {}
const json = (await res.json().catch(() => ({}))) as { token?: string; error?: string }
if (!res.ok) return { error: json.error ?? 'network' }
return json
} catch {
return { error: 'offline' }
+ } finally {
+ clearTimeout(t)
}
}
// GET helper for the device-bound status poll.
async function getJson(path: string): Promise<{ active?: boolean; token?: string; error?: string }> {
if (!allowed()) return { error: 'disabled' }
+ const ctrl = new AbortController()
+ const t = setTimeout(() => ctrl.abort(), REQUEST_TIMEOUT_MS)
try {
- const ctrl = new AbortController()
- const t = setTimeout(() => ctrl.abort(), 8000)
- const res = await fetch(`${API_BASE}${path}`, { signal: ctrl.signal }).finally(() => clearTimeout(t))
+ const res = await fetch(`${API_BASE}${path}`, { signal: ctrl.signal })
const json = (await res.json().catch(() => ({}))) as { active?: boolean; token?: string; error?: string }
if (!res.ok) return { error: json.error ?? 'network' }
return json
} catch {
return { error: 'offline' }
+ } finally {
+ clearTimeout(t)
}
}
@@ -234,15 +249,17 @@ export function initLicense(onChange?: () => void): void {
const token = load().token
if (!token) return { ...EMPTY_DETAIL, error: 'unauthorized' }
if (!allowed()) return { ...EMPTY_DETAIL, error: 'disabled' }
+ // The timer covers the BODY too — see REQUEST_TIMEOUT_MS. This is the route the Release
+ // button awaits, so a request that never finishes leaves that button spinning for good.
+ const ctrl = new AbortController()
+ const t = setTimeout(() => ctrl.abort(), REQUEST_TIMEOUT_MS)
try {
- const ctrl = new AbortController()
- const t = setTimeout(() => ctrl.abort(), 8000)
const res = await fetch(`${API_BASE}${route}`, {
method: 'POST',
headers: { 'content-type': 'application/json' },
body: JSON.stringify({ entitlement: token }),
signal: ctrl.signal
- }).finally(() => clearTimeout(t))
+ })
const json = (await res.json().catch(() => ({}))) as {
key?: unknown
used?: unknown
@@ -283,6 +300,8 @@ export function initLicense(onChange?: () => void): void {
}
} catch {
return { ...EMPTY_DETAIL, error: 'offline' }
+ } finally {
+ clearTimeout(t)
}
}