happier icon indicating copy to clipboard operation
happier copied to clipboard

CLI pending queue pump retries indefinitely on 401, causing server unresponsiveness

Open danljungstrom opened this issue 7 months ago • 1 comments

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-next endpoint
  • 1.2 GB log file (22M lines, mostly these 401s)
  • SQLite write contention: constant request volume causes legitimate DB operations (usageReport.upsert, session.update during session-end) to hit Prisma "Socket timeout" errors
  • Server becomes unresponsive to real client requests, requiring a container reboot to recover

Reproduction

  1. Start a Happier light server (SQLite-backed)
  2. Start a CLI daemon that connects and begins polling pending queue for a session
  3. Invalidate the daemon's auth token (e.g., restart the server, or let the token expire)
  4. Observe: materialize-next returns 401 every ~1 second indefinitely
  5. 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

  1. Find and kill the rogue daemon process on the machine polling with a stale token
  2. Truncate the log: truncate -s 0 ~/.happier/stacks/main/logs/happier-stack.out.log
  3. Server recovers without reboot

danljungstrom avatar Apr 16 '26 09:04 danljungstrom

Thank you very much for the detailed report @danljungstrom !

As already discussed on Discord, it should now be fixed on dev following your report :)

leeroybrun avatar May 11 '26 18:05 leeroybrun