Skip to content

[Bug]: Failed run blocks the event loop while logging a rich traceback: /alive takes ~5 s, not ~100 ms #5633

Closed

Description

@enyst

I'm an AI agent based on Opus 5.5, helping Engel Nyst (@enyst) with project maintenance.

Is there an existing issue for the same bug?

  • I have searched existing issues and this is not a duplicate.

Bug Description

When a conversation run fails, EventService._run_and_publish logs it with logger.exception(...) on the server's event loop. With default console logging (Rich, LOG_RICH_TRACEBACKS defaults to true), rendering the chained traceback of an ordinary LLM connection error (about 1,000 lines) takes about 5 s, and every request, socket and run on the server waits meanwhile. Any server without LOG_JSON=true (e.g. run from a checkout) hits this on every failed run.

Expected Behavior

GET /alive keeps answering 200 in about 100 ms (never over 1.5 s) while a run fails, and the ConversationErrorEvent reaches socket subscribers without delay.

Actual Behavior

A request sent while the failure is logged waits 4.8-6.6 s before its 200. The ConversationErrorEvent is already stored and the conversation ends execution_status: "error", but the socket's error frames arrive late by the same amount. With LOG_RICH_TRACEBACKS=false or LOG_JSON=true the slowest probe is under 200 ms.

Steps to Reproduce

  1. With LOG_JSON, CI and GITHUB_ACTIONS unset and no session API key: uv run agent-server --host 127.0.0.1 --port 8000
  2. Create a conversation whose LLM points at a closed port, and queue a message:
    WS=$(mktemp -d)
    CID=$(curl -s -X POST localhost:8000/api/conversations -H 'Content-Type: application/json' \
      -d '{"agent":{"llm":{"model":"openai/offline","api_key":"x","base_url":"http://127.0.0.1:9/v1","num_retries":0},"tools":[]},"workspace":{"working_dir":"'"$WS"'"}}' | jq -r .id)
    curl -s -X POST localhost:8000/api/conversations/$CID/events -H 'Content-Type: application/json' \
      -d '{"role":"user","content":[{"type":"text","text":"hi"}],"run":false}'
  3. Run it and probe /alive:
    curl -s -X POST localhost:8000/api/conversations/$CID/run
    for i in $(seq 30); do curl -s -o /dev/null -w '%{http_code} %{time_total}\n' localhost:8000/alive; done
    One probe takes about 5 s while stderr prints the traceback for "Error during conversation run".
  4. Restart with LOG_RICH_TRACEBACKS=false and repeat: every probe is well under 200 ms.

With the verify-agent-server skill (#5621): control-agent-server map run --file F05 --only F05.run-error-responsive --fresh reports the known-bug bullet as an expected failure while the bug lasts.

Acceptance Criteria

  • With default console logging, no /alive probe takes over 1.5 s while a run fails, and socket error frames are not delayed.
  • The run failure is still logged with enough detail to diagnose it.
  • A regression test fails a run on a live server and bounds the latency of a concurrent request.
  • The map bullet F05.run-error-responsive passes; its known-bug marker is removed.

Installation Method

uv sync in a checkout of main

Version

main at 2804c12

Additional Context

  • openhands-agent-server/openhands/agent_server/event_service.py:1546: logger.exception("Error during conversation run") in _run_and_publish, a task on the server loop. Here the exception is a ConversationRunError chained through litellm, openai, httpx and httpcore errors.
  • openhands-sdk/openhands/sdk/logger/logger.py:45 defaults LOG_RICH_TRACEBACKS to true; lines 134-137 install RichHandler(rich_tracebacks=...) as the root console handler unless LOG_JSON or CI is set. RichHandler.emit renders the whole chain with source context synchronously in the caller, here the loop.
  • LocalConversation.arun has already stored the ConversationErrorEvent and set ERROR before raising (openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py:2582-2608), so only the log line and everything else on the loop are delayed.

Suggested fix: keep console rendering off the loop, either with a QueueHandler/QueueListener around the console handler in setup_logging (covers every logger.exception on the loop) or, here, a one-line error (the event already has code and detail) with the traceback rendered via asyncio.to_thread or at DEBUG.

Related: #5373 (same mechanism in sockets.py, a cyclic chain whose render never returns). Its draft fix #5489 bounds traversal but still renders on the loop, so a finite chain like this one would still stall.


OpenHands AI triage

The following comments and acceptance criteria were added by the OpenHands AI agent.

Triage

Ownership: this repository owns the Agent Server and SDK, and the fix is here. The
reported defect is real and reproduced in the product: while _run_and_publish
logs a failed run via logger.exception(...) on the server event loop
(openhands-agent-server/openhands/agent_server/event_service.py:1546), the
default root RichHandler (installed when neither LOG_JSON nor CI is set,
LOG_RICH_TRACEBACKS defaulting true — openhands-sdk/openhands/sdk/logger/logger.py)
renders the full chained traceback synchronously in the caller, so /alive and
socket delivery stall 4.8-6.6 s on every failed run. Scope: stop that one log
call from blocking the loop while keeping enough diagnostic detail to find the
cause. Non-goals: reworking the rest of the run-error path; changing the
ConversationErrorEvent/status behavior (already correct before the raise);
changing default handling of unbounded/cyclic tracebacks (that is the separate
#5373/#5489 mechanism, which bounds traversal but still renders on the loop).
The fix approach is deliberately not prescribed: two supported shapes (a
QueueHandler/QueueListener around the console handler, or a bounded inline
message with off-loop/DEBUG traceback) both satisfy the criteria.

Acceptance Criteria

  • With default console logging (LOG_JSON unset, LOG_RICH_TRACEBACKS unset/true), a run fails on a live agent server pointed at an unreachable LLM endpoint and every concurrent GET /alive probe returns 200 within 1.5 s; no probe stalls on the run-error log.
  • The same scenario with LOG_RICH_TRACEBACKS=false or LOG_JSON=true remains under the same bound.
  • The ConversationErrorEvent for the failed run reaches subscribers without the log-induced delay (conversation ends execution_status: "error").
  • The run failure is still recorded with the exception type and message at normal log level, and the full traceback remains retrievable off the loop (or at DEBUG) for diagnosis; with LOG_TO_FILE=true the file handler still receives the record.
  • A regression test fails a run on a live server and bounds the latency of a concurrent request, so the stall cannot return unnoticed.
  • The verify-agent-server map bullet F05.run-error-responsive passes and its known-bug marker is removed.
  • The fix does not alter the OpenAPI/REST contract or serialized event shapes and stays compatible with LOG_JSON/CI JSON logging.

Activity

  1. added theissue type on Oct 9, 2026
  2. added
    bug
    on Oct 9, 2026
  3. added
    priority:medium
    ready-for-dev
    on Oct 9, 2026
  4. xxwtiancai commented on Oct 11, 2026

    @xxwtiancai
    Contributor

    I’m working on this with Codex assistance. I’ll first reproduce the failed-run stall on a real CLI server with default Rich and access logging, using an unreachable local provider. The intended scope is the run-error log call: retain useful ERROR diagnostics and a DEBUG traceback, with HTTP/WebSocket responsiveness and shutdown coverage. I’ll leave the cyclic-traceback work in #5489 and the shared logger architecture unchanged.

  5. enyst commented on Oct 11, 2026

    @enyst
    MemberAuthor

    Related to #5373: both involve Rich traceback rendering synchronously on the server event loop, but the triggers and acceptance cases differ.

    This issue reproduces an ordinary finite LLM error chain taking about 5 seconds to render in _run_and_publish. #5373 covers a cyclic/pathological chain on the events-WebSocket error path that never returns and grows memory. Bounding cyclic traversal alone does not address the finite-chain latency here.

    Keeping both reports open with this cross-link; they can share work on the logging path, but both reproductions should remain covered. The relationship is also tracked in #5653.

    Posted by Codex on behalf of @enyst.

  6. enyst commented on Oct 13, 2026

    @enyst
    MemberAuthor

    I'm an AI agent (Claude Code) helping Engel Nyst (@enyst) with project maintenance.

    An independent fix is in #5780. At the maintainer's request, it was written for comparison with #5709 without reading it. Notes on the triage points:

    Decision needed (fix shape). The triage leaves the approach open, so a maintainer should choose. #5780 logs the failure at ERROR with the traceback formatted by traceback.format_exception as plain text in the message, and no exc_info. That keeps the full chain at the normal level, including in LOG_JSON production logs and with LOG_TO_FILE (same record, same level). It costs milliseconds instead of seconds. Trade-offs: the console shows a plain traceback instead of Rich's panel, and in JSON logs the traceback is in message instead of exc_info.
    Alternatives: a one-line ERROR plus a DEBUG traceback (keeps the exc_info field, but production INFO logs lose the traceback), or a QueueHandler/QueueListener in setup_logging (keeps Rich output for every log call, but changes logging for all SDK users).
    One finding that rules out the simplest variant: moving logger.exception to a worker thread does not fix it. I measured about 5 s probes on a live server, because every log call on the loop (e.g. the access log) waits for the console handler's lock while the thread renders.

    Answers to the acceptance criteria:


    Generated by Claude Code

  7. xxwtiancai commented on Oct 13, 2026

    @xxwtiancai
    Contributor

    Thanks @enyst. Keeping the full traceback available at the normal production log level makes sense. #5709 followed the DEBUG-traceback option in the issue, but #5780 avoids needing to enable DEBUG to diagnose a failed run.

    There may be useful test coverage to carry over from #5709. Its live-server regression test covers Rich, plain, and JSON logging, concurrent /alive and /server_info requests, WebSocket error-delivery latency, terminal ERROR state, and file diagnostics.

    Would you be open to combining that coverage with #5780? The test checks externally visible behavior rather than the DEBUG implementation, although I have not run it against #5780 yet. That seems more useful than maintaining two competing fixes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugpriority:mediumready-for-dev

    Type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions