Skip to content

switch_llm deadlocks the conversation runtime (regression in v1.23.0, async LLM completion #3284) #3485

Closed

Description

@VascoSch92

Summary

Invoking the switch_llm tool mid-conversation deadlocks the conversation runtime. The tool call never completes: no SwitchLLMObservation is emitted, execution_status stays running indefinitely, the agent produces no further output, and any UI/WebSocket client hangs with no result. The process sits at 0% CPU with no LLM request in flight.

This is a regression: it works on v1.22.1 and is broken on v1.23.0, v1.23.1, v1.24.0.

Impact

Mid-conversation model switching via the switch_llm tool is unusable on agent-server ≥ 1.23.0. Every switch attempt hangs the conversation (8/8 in our testing; 0 succeeded). The same flow worked 10/10 on 1.22.1.

Environment

  • openhands-agent-server / openhands-sdk / openhands-tools / openhands-workspace: 1.24.0 (also reproduced on 1.23.1; last good 1.22.1)
  • Python 3.13.11, macOS (ARM64), local conversation (LocalConversation), run via uvx
  • switch_llm tool enabled; ≥1 valid LLM profile configured

Reproduction

  1. Run a local agent-server with the switch_llm tool enabled and ≥2 valid LLM profiles.
  2. In a running conversation, have the agent invoke switch_llm (e.g., ask it to switch profile).
  3. The conversation hangs: last event is SwitchLLMAction, execution_status stays running, no SwitchLLMObservation is ever emitted, and the agent stops responding.

Observed error variants (depending on timing / manual interrupt):

  • 1.24.0: AgentErrorEvent: "A restart occurred while this tool was in progress. This may indicate a fatal memory error or system crash. The tool execution was interrupted and did not complete."
  • 1.23.1: ~60s hang, then (after manual stop) AgentErrorEvent: "Tool call interrupted before completion. The conversation was paused."

Expected vs actual

  • Expected: switch_llm completes, emits SwitchLLMObservation, the agent continues on the new LLM.
  • Actual: the tool call deadlocks; no observation; conversation stuck running.

Evidence it's a deadlock (not a slow/unreachable model)

  • sample <pid> of the agent-server: 0% CPU, sleeping. Main thread parked in the asyncio loop (kevent); multiple threads parked acquiring a Python lock (lock_PyThread_acquire_lock → _PyMutex_LockTimed → _PyParkingLot_Park → __psynch_cvwait); worker threads idle on _queue_SimpleQueue_get.
  • lsof -p <pid>: no outbound connection to the LLM endpoint — it never reaches the model call. The LLM proxy itself responds in ~0.4s (reachable).

Root cause (analysis)

Two pre-existing pieces that only deadlock once completion became async:

  • LocalConversation.run() holds the conversation FIFO state lock (with self._state:) across the entire agent step, and the default persistence callback runs under that lock.
  • EventService._emit_event_from_thread() re-acquires that same lock on a different thread:
    def locked_on_event():
        with conversation._state:
            conversation._on_event(event)
    self._main_loop.run_in_executor(None, locked_on_event)
    (openhands-agent-server/openhands/agent_server/event_service.py)

The FIFO lock is reentrant for the same thread, so synchronous in-thread emission was fine. With async LLM completion, completion/log/streaming events now fire from background threads. switch_llm swaps the agent (agent.model_copy → state.agent = …), which re-initializes the agent (observable as a skills reload) and bursts off-thread events while run() still holds _state. Those locked_on_event tasks block on _state on the shared default executor and never acquire it; the run loop can't complete the step → deadlock, no observation.

Relevant code:

  • openhands-agent-server/openhands/agent_server/event_service.py — _emit_event_from_thread (the cross-thread with conversation._state: dispatched to the default executor)
  • openhands-sdk/openhands/sdk/conversation/impl/local_conversation.py — run() (state lock held across the step), switch_llm, default persistence callback

Regression / bisect

First broken in v1.23.0, introduced by #3284 "feat: add async LLM completion support across the SDK" (commit 3996252c7).

  • git merge-base --is-ancestor 3996252c7 v1.22.1 → false (not in last-good 1.22.1)
  • git tag --contains 3996252c7 → first release tag is v1.23.0

Before #3284, completion was synchronous, so step events were emitted in-thread under the reentrant lock and never deadlocked.

Suggested fix

Avoid dispatching _state-acquiring callbacks onto the shared executor while run() holds _state across the (now async) step. Options:

  • Persist events on a dedicated single worker thread rather than re-acquiring _state on the default executor, or
  • Don't re-acquire _state in _emit_event_from_thread (use a separate event-log lock / rely on the lock the run loop already holds), or
  • Scope/release _state around the async completion so off-thread event emission can proceed.

Workaround

Pin agent-server to 1.22.1 (the last version before #3284).

Note

The exact frozen Python frame of the lock holder was not captured (py-spy requires root on macOS); the diagnosis above is from the native sample (lock-park + 0% CPU + no LLM socket) plus source analysis. Happy to attach a py-spy dump if useful.

Activity

  1. self-assigned this
    on Jun 3, 2026
  2. added
    bug
    and removed on Jun 3, 2026
  3. added a commit that references this issue on Nov 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions