Skip to content

[Bug]: Closing a terminal does not stop TerminalSession.execute() waiting on a running command #6139

Closed

Description

@VascoSch92

Is there an existing issue for the same bug?

Bug Description

Closing a terminal does not stop TerminalSession.execute() from waiting on a command that was running. The wait loop (openhands-tools/openhands/tools/terminal/terminal/terminal_session.py, the while True after the command is sent) only returns when the screen shows the command finishing, at the no-change timeout, or at the command's hard timeout. A closed terminal never shows the command finishing, and a command with a timeout skips the no-change timeout, so the loop keeps polling the dead pane until that timeout, however long the model made it.

LocalConversation.close() closes the tool executors (the tmux pool kills its session, which does stop the command), but the thread inside execute() stays blocked. Everything that waits for the run waits with it:

  • interrupt() followed by close() (timeout or cancel of a run mid-command): arun() does not return until the tool call's thread does, so a caller's own deadline is overrun.
  • Process shutdown: the tool call's thread is a ThreadPoolExecutor worker, which the interpreter joins at exit, so the process cannot exit until the timeout either and gets killed (docker stop ends in SIGKILL).

Expected Behavior

Once its session or terminal is closed, execute() stops waiting and returns an error observation, so closing a conversation releases its running tool calls.

Actual Behavior

execute() keeps polling the closed terminal until the command's timeout (or the 30 s no-change timeout for a command without one).

Steps to Reproduce

import os, threading, time
from openhands.tools.terminal.definition import TerminalAction
from openhands.tools.terminal.terminal import create_terminal_session

session = create_terminal_session(work_dir=os.getcwd(), terminal_type="tmux")  # or "subprocess"
session.initialize()
waiter = threading.Thread(target=session.execute, args=(TerminalAction(command="sleep 30", timeout=60),))
waiter.start()
time.sleep(2)
session.close()
waiter.join(timeout=10)
print("still waiting:", waiter.is_alive())  # True on main

Seen end to end in an A2A agent that runs LocalConversation.arun() in a worker thread (the OpenHands agent for AgentEnv, scaleapi/agentenv-framework#281): with the model running sleep 120 with a long timeout, an 8 s turn timeout and docker stop both waited for the command's timeout, and docker stop ended in SIGKILL after its 45 s grace period.

Acceptance Criteria

  • TerminalSession.execute() returns promptly with an error observation once the session or its terminal is closed, for the tmux and subprocess backends.
  • Closing a pooled TerminalExecutor releases a call waiting on a command.

SDK Version

main at 1d057fe; also 1.57.0.


OpenHands AI triage

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

Triage

Reproduced on current main (1d057fe era, SDK 1.57.0) with the reporter's snippet: after session.close() from another thread, the execute() waiter is still alive on both the tmux and subprocess backends, and a pooled TerminalExecutor.close() leaves a call blocked the same way. The wait loop in openhands-tools/openhands/tools/terminal/terminal/terminal_session.py only exits on prompt/boundary detection, the no-change timeout, or the hard timeout; a closed backend never reaches any of those, and a command carrying timeout skips the no-change branch. This is the review checklist item "cancellation must stop underlying work", and it blocks process shutdown because the call runs on a ThreadPoolExecutor worker the interpreter joins at exit.

Scope: make TerminalSession.execute() stop waiting and return an error observation once its session or terminal is closed, and make TerminalExecutor.close() release a call waiting on a checked-out pooled pane. Both are owned by this repository (openhands-tools).

Non-goals: changing no-change/hard timeout semantics or polling cadence for live terminals; changing interrupt()/pause() behavior; changing backend close() semantics or pool replacement/recovery; adding new public API, action, or observation fields; and explicitly verifying the PowerShell/Windows backend, which the issue does not name (its TerminalSession wait loop is shared, so it must not regress but is not a target).

Acceptance Criteria

  • With a command that has a long timeout running (e.g. sleep 30, timeout=60), calling TerminalSession.close() from another thread makes the blocked execute() call return within a few poll intervals, not at the command's timeout, on both the tmux and subprocess backends.
  • The observation returned in that case is an error observation (is_error=True) whose text says the terminal/session was closed, and it is not reported as a normal completion, a no-change timeout, or a hard timeout.
  • Calling execute() on a session that is already closed returns an error observation rather than blocking, hanging, or raising an uncaught backend exception (currently RuntimeError: PTY terminal is not initialized on subprocess).
  • TerminalExecutor.close() in pool mode releases a call blocked in execute() on a checked-out pane within a few poll intervals; that call returns an error observation instead of hanging or returning "previous command is still running".
  • The waiting thread terminates, so LocalConversation.close()/interrupt() no longer overrun a caller's deadline and process shutdown is not held until the command's timeout.
  • Repeated or concurrent close() stays idempotent and does not raise when a command is running.
  • Behavior is unchanged when the terminal is not closed: completed commands, the 30 s no-change timeout for commands without timeout, and the hard timeout for commands with timeout still return their existing observations, and the existing terminal test suite passes.
  • TerminalAction/TerminalObservation schemas and TerminalSession.execute()'s signature and return type are unchanged.
  • Automated coverage in tests/tools/terminal/ asserts the above for the tmux and subprocess backends and for a pooled TerminalExecutor; the reporter's reproducer prints still waiting: False and the process exits promptly.

Activity

  1. added
    bug
    on Oct 21, 2026
  2. added
    priority:medium
    ready-for-dev
    on Oct 21, 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

    bugpriority:mediumready-for-dev

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions