agent-zero icon indicating copy to clipboard operation
agent-zero copied to clipboard

Whole-backend wedge: event loop freezes, all chats stall simultaneously, user messages silently swallowed, no trace left

Open nico7master opened this issue 2 months ago • 2 comments

Whole-backend wedge — event loop freezes, all chats stall simultaneously, user messages silently swallowed, no trace left

Summary

In a dockerized Agent Zero instance, the entire backend can wedge without crashing: every chat stops progressing at the same time, the UI keeps showing spinner state, the browser push channel silently dies (work completes but responses never render), and — worst — user messages typed during the wedge are swallowed with zero trace: they are never persisted to any chat file and never processed. The process remains alive (no OOM, no exception, nothing in exception capture), so only a manual restart recovers it. Post-mortem diagnosis is nearly impossible because the wedge leaves no logs.

Observed 2026-09-08 ~14:17 CEST: all active chats' logs stopped within minutes of each other (14:11–14:17). Agent work that finished at 14:11–14:13 (tool calls, sent emails, generated responses logged in chat JSON) never reached the user's UI. Messages sent after ~14:17 exist nowhere on disk. Manual framework restart at 15:03 was the only recovery.

Measured evidence (second incident, instrumented)

After the first incident, user-space instrumentation was installed (turn trace, periodic asyncio task-state snapshots, loop-stall stack dumps). The wedge recurred the same day and was captured live (2026-09-08 17:20 CEST):

  • Turn trace: turn_start logged for a chat agent at 17:20:10, no matching turn_end ever — the wedge onset marker
  • Task snapshots: pending asyncio tasks doubled 6 → 12 between 17:18:20 and 17:19:24 — task pileup immediately before the freeze
  • Loop-stall monitor: event loop froze at 17:20:09; stall entries with lag climbing 16s → 616s every minute until process replacement — the main event loop was hard-frozen for the duration, not merely slow
  • Ruled out again: no OOM (memory.events clean), no captured exceptions, container never restarted

This upgrades the incident from "unknown cause" to a measured signature: task pileup → hard event-loop freeze → all chats + WS push dead, reproducible as a pattern even though the trigger is not yet identified.

Also found and repaired on the same day: 8 empty (0-byte) git object files in a Time Travel workspace repository (/a0/usr/.time_travel/workspaces/<id>/repo.git/objects/) — corrupt objects that cause snapshot/repair-tooling failures and may participate in the pileup (e.g., a repair sweep iterating corrupt repos on the loop). Removed; repository verified functional.

Reproduction

Not yet reliably reproducible — intermittent. Conditions present at the time:

  1. Multiple long-running chats active simultaneously (several at ~100k+ token contexts)
  2. Heavy concurrent activity (terminal tool sessions, vision loads, multiple chat loops running)
  3. UI open in browser; at some point responses stop rendering while chats keep "running"
  4. Eventually: ALL chats stop progressing; typed messages show indefinite "waiting" spinner
  5. docker container still healthy: process alive, memory peak 10.1/20 GiB, memory.events clean (no oom), no new entries in exception capture

Root cause

Unknown — this is the core of the report. Ruled out so far:

  • OOM / cgroup kill: memory.events shows zero oom/oom_kill events; peak 10.1 of 20 GiB
  • Crash: process was still running until manually restarted
  • Per-chat compression stalls: a separate, already-reported failure mode (#1881) — that bug kills individual chats silently, but does not explain a simultaneous cross-chat freeze, a dead UI push channel, or swallowed input

Suspected directions (unverified):

  • Event-loop blockage: a hung tool call / thread pool exhaustion blocking the main asyncio loop would freeze all chat loops simultaneously and stop WS push — matches symptoms
  • WebSocket push channel dying silently: responses completed and were logged server-side (14:11–14:13) while the browser showed nothing — the push path had already failed before the full wedge, or the wedge began as a push failure and escalated

The two concrete defects worth fixing regardless of root cause

  1. Data loss: user messages are not durable. A message typed while the backend is wedged is lost forever — it is not written to the chat file before processing is attempted. Persisting the user message (and marking it pending) before enqueueing would make every wedge recoverable with zero loss.
  2. No observability for process-level wedges. There is no heartbeat log, no event-loop liveness marker, and the exception-capture hook records nothing for this failure class (it only logs raised exceptions, not hangs). A periodic heartbeat (e.g., timestamped touch per active chat + loop-liveness entry) would turn this undiagnosable wedge into a diagnosable one.

Before / after

Scenario Current With proposed changes
Backend wedges while user types Message swallowed, no trace, spinner forever Message persisted as pending; delivered or replayed after recovery
Post-incident diagnosis Nothing logged; guesswork Heartbeat timeline shows exact wedge onset + last live activity
Responses generated during push failure Never displayed, user resends Client reconnect/resync replays missed responses

Environment

  • Agent Zero, dockerized (single container), current release as of Sep 2026
  • Multiple concurrent chats with ~100k+ token contexts, vision embedding, parallel tool sessions
  • Incident window: 2026-09-08 14:11–14:17 CEST; recovered only by manual restart 15:03

Related

  • #1881 (history compression stalls on utility-model failure) — per-chat silent death of final responses; a different failure mode, but both were active in this instance and their symptoms are easily confused. Distinguishing signal: #1881 kills one chat; this bug stops all chats at once and swallows input.

nico7master avatar Sep 08 '26 15:09 nico7master

Root cause confirmed — orphaned async-generator finalizers from dead streams

Update from the original reporter: the "unknown cause" in this report is now root-caused with live measurements. Short version: the wedge is caused by unbounded cleanup of abandoned LLM streams — dead connections leave orphaned async generators whose asyncio finalization tasks (<async_generator_athrow ...>) block forever and pile up until the event loop wedges.

Live capture (2026-09-23, 17:44–17:49 CEST)

time pending asyncio tasks
17:44 8 (normal)
17:45 200 (athrow-dominated)
17:46 628 — incl. CustomStreamWrapper.async_cache_streaming_response, RecallMemories.search_and_cache, hundreds of <async_generator_athrow object at ...>
17:49 loop frozen (stall monitor lag >180 s)

Trigger chain in the exception log for the same window: MidStreamFallbackError → litellm.APIConnectionError under 429/timeout storms, plus user interventions mid-stream.

Retrospective snapshot forensics show identical athrow-dominated pileups on Sep 9, 10, 12 and 22 — this predates any local workaround and reproduces whenever streams die.

Synthetic repro: an async generator whose finally awaits a dead connection produces exactly one immortal <async_generator_athrow> finalizer task per dead stream; cancelling it clears it instantly — the tasks are not unreapable, they are simply never reaped. (Diagnostics note: these finalizers have no __qualname__; they print as <async_generator_athrow object at ...>, so qualname-based filters miss them.)

Request-level timeouts are necessary but not sufficient

usr/settings.json has carried litellm_global_kwargs: {timeout: 180, stream_timeout: 300, num_retries: 3} since Sep 21 — pileups still recurred on Sep 23 (274→408 tasks at 12:36–12:54, then 200→628 at 17:45). Reason: request timeouts bound the request, but the cleanup path of an abandoned generator is unbounded: the orphaned finalizer awaits the dead connection with no timeout of its own. (PR #1886's description itself notes stream_timeout is Router-only — a no-op for A0's direct completion calls.)

Exact fix site (implementation-level)

helpers/litellm_transport.py already closes abandoned iterators in finally: blocks — _close_sync_stream(iterator) (line 291) and await _close_async_stream(iterator) (line 332) — but the close itself is unbounded: awaiting aclose() on a stream whose connection is dead hangs forever, so the cleanup becomes the wedge.

Proposed minimal patch:

# helpers/litellm_transport.py, both finally blocks
try:
    await asyncio.wait_for(_close_async_stream(iterator), timeout=10)
except asyncio.TimeoutError:
    pass  # optionally force-close the underlying client

(same bounding for the sync path's awaitable-close branch), and ensure mid-stream-fallback / intervention aborts route through the same bounded close. This complements #1882 and PR #1886: request timeouts fire first, but without bounded cleanup the orphaned generator's finalizer still awaits the dead connection indefinitely.

Local workarounds deployed meanwhile (usr/extensions, no core changes)

  1. Task janitor — cancels athrow-shaped tasks after a 45 s grace (burst rule: ≥25 pending and age >10 s), sampled every 20 s; regression-tested against a synthetic real-shape zombie. Since arming: zero wedges (steady single-digit transient athrow_pending, zero cancels needed).
  2. Durable user-message journal — messages persisted at ingress (usr-side journal under tmp), delivery-marked on turn completion, boot-time recovery report for anything undelivered. Fixes the swallowed-messages defect for our deployment.
  3. The pre-existing stall watchdog auto-restarts the UI after ~3 min stalls (worked 4× on Sep 23).

Happy to test a PR implementing the bounded close.

nico7master avatar Sep 24 '26 10:09 nico7master

Resilience follow-up from the original reporter: after the main loop recovers from a wedge like this, chats that touch the browser plugin remain permanently wedged, because BrowserRuntime.call_for awaits the (now-dead) worker loop's Future with no timeout and its cancellation cleanup waits on the same dead worker. Live-proven on 2026-09-26 with in-process coroutine dumps; per-worker liveness check + bounded cross-thread wait proposed. Details and before/after: #1923.

nico7master avatar Sep 26 '26 10:09 nico7master