Skip to content

fix(local): stop the run's entrypoint BashCommand on cancel - #760

Merged
neubig merged 1 commit into
mainfrom
fix/local-cancel-stops-bash-command
Nov 16, 2026
Merged

neubig merged 1 commit into
mainfrom
fix/local-cancel-stops-bash-command

Conversation

@neubig

@neubig neubig commented Nov 14, 2026

Copy link
Copy Markdown
Member

Why

Fixes #514.

cancel_run only cleaned up when run.sandbox_id was set. Local runs have no sandbox, so cancelling a local run changed the row to CANCELLED while the entrypoint started by POST /api/bash/start_bash_command kept running until its own timeout. The maintainer decision on the issue is to stop exactly the run's recorded bash_command_id through the agent-server's per-command stop API (OpenHands/software-agent-sdk#5257). That API ships in the SDK version this repo pins, openhands-sdk==1.62.0 (AsyncRemoteWorkspace.stop_command, POST /api/bash/bash_commands/{id}/stop).

Summary

  • utils/agent_server.stop_bash_command: stops one BashCommand through AsyncRemoteWorkspace.stop_command. The agent-server signals the command's process group (SIGTERM, then SIGKILL) and records its exit code. The server answers 200 for a finished id and 404 for an unknown id, and the SDK treats both as success. Transport errors are logged and do not reach the caller.
  • ExecutionBackend.stop_command defaults to a no-op, and its docstring records the difference between the modes. Cloud cancellation deletes the sandbox, which kills every process in it, and is unchanged. LocalAgentServerBackend.stop_command stops only the given command, so other commands and the agent's TerminalTool activity on the shared server keep running.
  • cancel_run: for a run without a sandbox but with a bash_command_id, after the row is CANCELLED, calls get_backend(run).stop_command(run.bash_command_id) and waits for it. When the response returns, the process has been stopped.
  • dispatcher._execute_run: a cancel can land after the command starts but before its id is stored. In that case update_run_timeout_at reports the run is no longer RUNNING, and the dispatcher now stops the command it just started. Stopping a finished command is a no-op.
  • Unchanged: the complete_run optimistic lock, which keeps a cancelled run CANCELLED against a late callback (already covered in tests/test_router.py). No host-level signal is sent, and no conversation runtime is deleted.

How to Test

uv run pytest tests/test_cancel_run.py

The new tests in tests/test_cancel_run.py run against a small HTTP server on the agent-server stop contract. It runs real processes in their own sessions, as BashEventService does. The tests cover:

  • Cancelling a local run stops its entrypoint and a backgrounded descendant, and a concurrent command keeps running.
  • A finished id and an unknown (404) id both leave the run CANCELLED with HTTP 200.
  • An unreachable agent-server still cancels the run.
  • A run with no recorded command sends no stop request.
  • A cancel during dispatch stops the command once it starts.

Four of these tests fail on main. The cancel, dispatcher, local-mode and watchdog suites pass (186 tests).

End to end (.pr/issue-514/repro_514.sh): run a local installation (agent-server 1.62.0 plus the automation service in local mode with SQLite). Start an unrelated sleep 6001 BashCommand on the agent-server, dispatch a run whose entrypoint is a long-running python3 worker.py, then cancel the run.

Before (main 949c001) After (this PR)
before after

After the fix, the agent-server reports exit code -15 for the entrypoint command, so verify_run_on_agent_server sees a finished command. The unrelated command keeps running.

This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.

Local runs have no sandbox, so cancel_run only flipped the row to CANCELLED
while the entrypoint kept running until its own timeout. Cancelling a local
run now stops exactly that run's recorded bash_command_id through the
agent-server's per-command stop API (SDK 1.62.0), which signals its process
group and leaves other commands on the shared server running. Finished and
unknown ids are already-stopped; a failed stop is logged, not surfaced. A
cancel that lands before the command id is recorded is caught in the
dispatcher, which stops the command once it starts. Cloud cancellation still
deletes the sandbox.

Fixes #514

Co-authored-by: openhands <openhands@all-hands.dev>
@neubig
neubig requested a review from all-hands-bot November 14, 2026 00:27
@github-actions

Copy link
Copy Markdown
Contributor

📁 PR Artifacts Notice

This PR adds or changes temporary PR-specific documents under .pr/. They stay on this branch for review. After this PR merges, the next release PR removes them from main.

@github-actions github-actions Bot added the type: fix A bug fix label Nov 14, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Coverage

Warning

Your comment is too long (maximum is 65536 characters), so the coverage report was not added. See the job log for how to reduce it.

@neubig

neubig commented Nov 14, 2026

Copy link
Copy Markdown
Member Author

@all-hands-bot Please review the latest head 533253b. All CI checks are green and the branch merges cleanly with main. Before/after evidence is in the PR description.

This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.

@all-hands-bot all-hands-bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This review was posted by an AI agent (OpenHands).

Scope gate: passes. The automation service owns run lifecycle, dispatch, and cancellation (AGENTS.md); the change does not reimplement SDK/agent-server behavior — it calls the per-command stop API that ships in the pinned openhands-sdk==1.62.0 (verified in the installed SDK source: AsyncRemoteWorkspace.stop_command → POST /api/bash/bash_commands/{id}/stop, 404 treated as a no-op, matching the issue's maintainer decision). Issue #514 is ready-for-dev and its acceptance list matches this PR.

What I verified on 533253b:

  • cancel_run trace: the guarded UPDATE ... WHERE status IN (PENDING, RUNNING) keeps the terminal transition idempotent; elif run.bash_command_id only fires when sandbox_id is falsy, so cloud runs keep the unchanged fire-and-forget sandbox deletion, and CloudSandboxBackend inherits the base no-op stop_command. Local workspace cleanup still happens via the watchdog's janitor loop; no host-level signal and no conversation-runtime deletion were added.
  • The dispatch race in _execute_run correctly reuses update_run_timeout_at's existing WHERE status == RUNNING guard: a cancel landing after the command starts but before its id is persisted makes that write report "no longer RUNNING", and the dispatcher then stops the command it just started. Persisted status matches the real process outcome; the complete_run optimistic lock that keeps a cancelled run CANCELLED against a late callback is untouched.
  • LocalAgentServerBackend.stop_command targets only the recorded command, so other commands and the agent's TerminalTool activity on the shared server keep running — covered by the tests with a real concurrent process.
  • tests/test_cancel_run.py meets the repo's evidence bar: real process groups (SIGTERM → SIGKILL) behind a real HTTP stop contract rather than call-assertion mocks, covering entrypoint + descendant kill, concurrent-command isolation, finished/unknown-id idempotency, unreachable server, no-recorded-command, and the dispatch race (four of these fail on main). The dispatch-race test passes in this workspace against the pinned SDK.
  • All GitHub Actions checks on 533253b are green (unit-tests, windows-tests, backend, image builds), and pre-commit (ruff/pyright) is clean on the changed files.

Two non-blocking observations, verified in this workspace, with no action requested: on a full connect timeout (agent-server unreachable without refusing the connection) the SDK's stop generator hard-codes a 60 s httpx timeout, so cancel_run can block up to ~60 s before logging and still returning CANCELLED — acceptable for a single-tenant local deployment, and the run always ends CANCELLED; and tests/test_cancel_run.py is Linux-specific (process groups, /proc), but the windows-tests job runs a fixed file list that excludes it.

No material correctness, security, or design findings. The .pr/issue-514/ artifacts are covered by the repository's PR-artifacts workflow notice.

✅ APPROVED

@neubig
neubig requested a review from malhotra5 November 14, 2026 14:49
@neubig
neubig merged commit 8e39f19 into main Nov 16, 2026
10 checks passed
@openhands-release-bot openhands-release-bot Bot added the released: 1.33.0 Shipped in 1.33.0 label Nov 17, 2026
@openhands-release-bot

Copy link
Copy Markdown
Contributor

🚀 Released in 1.33.0.

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

Labels

released: 1.33.0 Shipped in 1.33.0 type: fix A bug fix

Projects

None yet

Development

Successfully merging this pull request may close these issues.

fix(local): stop BashCommand when an automation run is cancelled

3 participants