Repository navigation
fix(local): stop the run's entrypoint BashCommand on cancel - #760
Conversation
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>
|
📁 PR Artifacts Notice This PR adds or changes temporary PR-specific documents under |
|
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. |
|
@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
left a comment
There was a problem hiding this comment.
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_runtrace: the guardedUPDATE ... WHERE status IN (PENDING, RUNNING)keeps the terminal transition idempotent;elif run.bash_command_idonly fires whensandbox_idis falsy, so cloud runs keep the unchanged fire-and-forget sandbox deletion, andCloudSandboxBackendinherits the base no-opstop_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_runcorrectly reusesupdate_run_timeout_at's existingWHERE status == RUNNINGguard: 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; thecomplete_runoptimistic lock that keeps a cancelled runCANCELLEDagainst a late callback is untouched. LocalAgentServerBackend.stop_commandtargets only the recorded command, so other commands and the agent'sTerminalToolactivity on the shared server keep running — covered by the tests with a real concurrent process.tests/test_cancel_run.pymeets 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 onmain). 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
|
🚀 Released in 1.33.0. |
Why
Fixes #514.
cancel_runonly cleaned up whenrun.sandbox_idwas set. Local runs have no sandbox, so cancelling a local run changed the row toCANCELLEDwhile the entrypoint started byPOST /api/bash/start_bash_commandkept running until its own timeout. The maintainer decision on the issue is to stop exactly the run's recordedbash_command_idthrough 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 throughAsyncRemoteWorkspace.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_commanddefaults 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_commandstops only the given command, so other commands and the agent'sTerminalToolactivity on the shared server keep running.cancel_run: for a run without a sandbox but with abash_command_id, after the row isCANCELLED, callsget_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 caseupdate_run_timeout_atreports the run is no longer RUNNING, and the dispatcher now stops the command it just started. Stopping a finished command is a no-op.complete_runoptimistic lock, which keeps a cancelled runCANCELLEDagainst a late callback (already covered intests/test_router.py). No host-level signal is sent, and no conversation runtime is deleted.How to Test
The new tests in
tests/test_cancel_run.pyrun against a small HTTP server on the agent-server stop contract. It runs real processes in their own sessions, asBashEventServicedoes. The tests cover:CANCELLEDwith HTTP 200.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 unrelatedsleep 6001BashCommand on the agent-server, dispatch a run whose entrypoint is a long-runningpython3 worker.py, then cancel the run.After the fix, the agent-server reports exit code
-15for the entrypoint command, soverify_run_on_agent_serversees a finished command. The unrelated command keeps running.This comment/PR was created by an AI agent (OpenHands) on behalf of Graham Neubig.