Skip to content

[Bug]: idle eviction kills a running conversation-scoped bash command and writes no final BashOutput #5835

Open

Description

@enyst

I'm an AI agent (Claude Code) 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

With conversation_idle_ttl_seconds set, the idle-eviction loop evicts a conversation that has a running conversation-scoped bash command (POST /api/conversations/{id}/bash/start_bash_command). Eviction closes the conversation's scoped BashEventService, which cancels the command's task; the task SIGKILLs the process group and re-raises the cancellation before writing any final BashOutput. The command dies silently, and a client polling bash_events/search?command_id__eq=... (the Python SDK always polls scoped commands) waits for an exit code that never comes.

Expected Behavior

A running conversation-scoped command counts as in-flight work, like an agent run: the conversation is not idle-evictable while it runs, so the command keeps running and its final BashOutput (with an exit code) is written when it ends. Once the command has finished, the conversation becomes evictable again.

Actual Behavior

The first eviction pass after the TTL evicts the conversation, the command's process group is SIGKILLed, and no final BashOutput is stored for it.

Steps to Reproduce

  1. In a checkout of main: uv sync, then start the server with a short TTL, e.g. OH_CONVERSATION_IDLE_TTL_SECONDS=5 SESSION_API_KEY=dev-key uv run python -m openhands.agent_server --host 127.0.0.1 --port 8000.
  2. Start a conversation without running it, then start a long command in its scope: POST /api/conversations/$CID/bash/start_bash_command with {"command": "sleep 600 # MARK"}.
  3. pgrep -f MARK shows the process.
  4. Wait for the next eviction pass (the loop runs about every 60 s): the log shows Evicted idle conversation $CID, pgrep -f MARK finds nothing, and GET /api/conversations/$CID/bash/bash_events/search?command_id__eq=$CMD never returns an output with an exit code.

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

Acceptance Criteria

  • EventService.is_idle_evictable() returns False while the conversation's scoped bash service has a running (or still spawning) command.
  • After the command finishes (and its final BashOutput is written), the conversation is evictable again.
  • A unit test in tests/agent_server/test_conversation_eviction.py covers both.
  • The map bullet F13.scoped-idle-eviction passes with its # bug marker removed.

Installation Method

uv sync in a checkout of main

Version

main at 6e7a853

Additional Context

Root cause:

  • EventService.is_idle_evictable() (openhands-agent-server/openhands/agent_server/event_service.py:2287) checks the agent run, goal loop, rerun flags and external subscribers, but not the conversation's bash_event_service.
  • ConversationService._evict_idle_conversations (openhands-agent-server/openhands/agent_server/conversation_service.py:2101) then calls event_service.__aexit__, and EventService.close() (event_service.py:2061-2063) closes the scoped bash service.
  • BashEventService.close() (openhands-agent-server/openhands/agent_server/bash_service.py:574) cancels every command task; the task's cancellation path kills the process group and re-raises before any final BashOutput is saved.

BashEventService._processes already tracks running and still-spawning commands, so the check can read it.

Out of scope: a command killed by a full server shutdown also gets no final BashOutput (same close() path); that is not changed here.

Related: #5196 (idle eviction of Docker runtimes). Found by the first full agentic run of verify-agent-server; tracked in #5653.


OpenHands AI triage

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

Triage

Scoped bug in the agent server; the reproduction, expected vs. actual behavior, and root-cause trace in the report all match main (6e7a853). EventService.is_idle_evictable() (event_service.py) gates eviction on the run task, goal loop, rerun flags, and external subscribers, but not on the conversation's bash_event_service; _evict_idle_conversations then calls EventService.close(), which closes the scoped BashEventService, whose close() cancels command tasks and whose cancellation path SIGKILLs the process group and re-raises before any final BashOutput is written. BashEventService._processes already tracks running (Process) and still-spawning (None) commands, so the check has an existing authoritative source of truth to reuse — no new mechanism is warranted.

Bounded scope: derive a "has an in-flight scoped bash command" signal from the existing _processes tracking and consult it in EventService.is_idle_evictable() so a conversation with a running or still-spawning scoped command is not idle-evictable. Add regression coverage in the existing tests/agent_server/test_conversation_eviction.py.

Explicit non-goals:

  • A command killed by a full server shutdown still gets no final BashOutput (same close() path); that behavior is unchanged, per the report.
  • Idle eviction of Docker runtimes (fix(agent-server): evict idle Docker conversation runtimes #5196) is unrelated and out of scope.
  • No REST/OpenAPI contract change: is_idle_evictable() is internal and the eviction decision is not a public schema.
  • Crash/kill resilience for arbitrary interruptions of a scoped command is not part of this fix.

Acceptance Criteria

  • With conversation_idle_ttl_seconds configured, a conversation whose scoped BashEventService has a running command (a live process in _processes) or a still-spawning command (_processes entry whose value is None) is not selected by _evict_idle_conversations, so it stays resident and the command's process is not signalled.
  • The same holds for a command started after the idle clock already exceeded the TTL: registration of the command prevents the next eviction pass from evicting that conversation.
  • After the command finishes and its final BashOutput (with an exit code) is written, the conversation is evictable again on the next pass once it is otherwise idle past the TTL.
  • Other idle conversations in the same eviction pass are still evicted; a conversation with a running scoped command does not block eviction of unrelated conversations.
  • A conversation with only finished scoped commands (empty _processes) remains evictable — no permanent pin from prior command history.
  • End to end against a running server, the reproduced scenario (short TTL, scoped sleep command) shows the command process is alive after the first eviction pass and GET .../bash/bash_events/search?command_id__eq=$CMD eventually returns a BashOutput carrying the command's exit code.
  • A test in tests/agent_server/test_conversation_eviction.py covers both the running/spawning-command non-eviction and the post-completion re-evictability by asserting conversation membership in _event_services after _evict_idle_conversations, following the existing patterns in that file.
  • The F13.scoped-idle-eviction map bullet passes with its # bug marker removed (run via the verify-agent-server skill), and no existing eviction or bash-service test regresses.

Activity

  1. added
    bug
    ready-for-dev
    on Oct 14, 2026
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

    bugready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions