CLI pending queue pump retries indefinitely on 401, causing server unresponsiveness
Summary
The CLI daemon's pending queue polling loop does not distinguish between "nothing to materialize" and "auth failure (401)". When a daemon holds a stale/expired token, the pending pump retries POST /v2/sessions/:id/pending/materialize-next every ~1 second indefinitely, generating millions of failed requests that eventually cause the server (especially SQLite-backed light deployments) to become unresponsive.
Root Cause
In apps/cli/src/api/session/pendingQueueV2Transport.ts:79-106, tryMaterializeNextViaHttp has a catch-all that swallows all errors including 401:
} catch {
return { ok: false };
}
The caller (materializeNextPendingQueueV2Message) treats { ok: false } as "try again later", and the pending pump loop in createAcpRuntime.ts retries on the next poll interval. A 401 is indistinguishable from a transient network error — the pump never stops.
The same pattern exists in tryMaterializeNextViaSocket (line 62-77).
Impact (observed on production light server)
- 1.6M failed 401 requests to a single session's
materialize-nextendpoint - 1.2 GB log file (22M lines, mostly these 401s)
- SQLite write contention: constant request volume causes legitimate DB operations (
usageReport.upsert,session.updateduringsession-end) to hit Prisma "Socket timeout" errors - Server becomes unresponsive to real client requests, requiring a container reboot to recover
Reproduction
- Start a Happier light server (SQLite-backed)
- Start a CLI daemon that connects and begins polling pending queue for a session
- Invalidate the daemon's auth token (e.g., restart the server, or let the token expire)
- Observe:
materialize-nextreturns 401 every ~1 second indefinitely - After hours/days, server responsiveness degrades
Suggested Fix
CLI — pending queue transport
In apps/cli/src/api/session/pendingQueueV2Transport.ts, distinguish 401/403 from transient errors in both HTTP and socket paths. Propagate auth failure so the pending pump can stop or re-authenticate instead of retrying.
CLI — pending pump loop
In apps/cli/src/agent/acp/runtime/createAcpRuntime.ts, the pending pump should abort the loop on auth failure rather than treating it as "nothing to do, try again."
Server — defense in depth (optional)
The materialize-next rate limit (pendingRoutes.ts:384-389, 120/min) only applies after auth succeeds. 401 responses bypass it since app.authenticate rejects before the handler runs. Consider rate-limiting repeated 401s at the auth/middleware level.
Server — shutdown race (optional)
The session-end handler in sessionUpdateHandler.ts:322-363 performs DB writes not wrapped in keepAlive(). During shutdown, these race with Prisma disconnect, causing "Socket timeout" log noise. Non-fatal but noisy.
Files
| File | Change |
|---|---|
apps/cli/src/api/session/pendingQueueV2Transport.ts |
Distinguish 401/403 from transient errors |
apps/cli/src/agent/acp/runtime/createAcpRuntime.ts |
Stop pending pump on auth failure |
apps/server/sources/app/api/socket/sessionUpdateHandler.ts |
Optional: wrap session-end DB ops in keepAlive() |
Workaround
- Find and kill the rogue daemon process on the machine polling with a stale token
- Truncate the log:
truncate -s 0 ~/.happier/stacks/main/logs/happier-stack.out.log - Server recovers without reboot
Thank you very much for the detailed report @danljungstrom !
As already discussed on Discord, it should now be fixed on dev following your report :)