How Do I Make a Cancel Actually Stop an AI Agent?
Updated November 23, 2026 .md
Short answer: Cancelling a workflow only stops your side from waiting; the command in the sandbox keeps running. Kill it with sbx exec --kill, the SDK's kill() or MCP kill_command, which end its children too. sbx stop only freezes it, and a resume brings it back mid-step. Deleting the sandbox ends everything.
Part of Coding Agents in the Cloud: Parallel, Overnight, Own Keys, Checkpoints.
Your workflow engine's "cancel" ends its own task. It has no idea that the step it was waiting on started a process in a VM. That process belongs to the sandbox, not to your workflow, so it runs until something in the sandbox stops it. There are four levels of "stop", and they do very different things.
Which stop does what#
| Goal | Use | What happens to the agent's processes | The sandbox after |
|---|---|---|---|
| Stop waiting for it | Cancel the workflow, close --attach |
Nothing. They keep running | Running |
| End the work, keep the files to inspect | sbx exec --kill <pid>, SDK kill(), MCP kill_command |
SIGTERM to the command, its process group and its descendants; SIGKILL to what's left after 5 s | Running, with nothing of the agent left in it |
| Pause, maybe continue later | sbx stop --wait |
Frozen with memory saved. sbx resume brings back the same PIDs mid-step |
Stopped |
| Cancel for good | Kill, then sbx delete --yes |
Gone, with the disk and snapshots | 404 |
| Backstop if your cancel handler never runs | A TTL: --ttl on create, sbx extend --ttl |
Frozen at the deadline, as with sbx stop |
Stopped |
Last checked: 2026-11-23, on prod
A real cancel is the fourth row: kill, wait until the command reports it's no longer running, then delete. The copy-paste block at the end does exactly that.
The test agent#
We tested on a small sandbox on prod on November 23, 2026. The "agent" is a script, not a model: a step loop, a long child (a tool call such as a test run) and a second child that calls setsid to leave the process group, the way some dev servers do. On SIGTERM it logs a line and exits.
#!/bin/bash
# A scripted stand-in for an agent: a step loop, a child tool call and a dev server.
trap 'echo "$(date +%T) SIGTERM: saving state" >> agent.log; exit 143' TERM
sleep 3600 & # a long tool call (think: a test run)
setsid sleep 7200 & # a dev server that left the process group
for i in $(seq 1 100000); do echo "$(date +%T) step $i" >> agent.log; sleep 2; doneWe started it the way an agent framework starts a long step, detached:
sbx exec $id --detach ./agent.sh{"ok":true,"type":"command.started","command":"./agent.sh","commandId":"cmd_yihhfxtyjlre","pid":584,"processId":584, …}1. Cancel on the workflow side: it keeps running#
We stood in for the workflow by following the command with --attach, then cancelling the client after 6 seconds:
timeout 6 sbx exec $id --attach 584; echo "client exit: $?"
sbx exec $id --status 584 | jq -c '{status, running}'
sbx exec $id 'ps -o pid,pgid,sid,etime,args -u user | grep -E "agent|sleep (3600|7200)" | grep -v grep; tail -1 agent.log'{"event":"error","error":"context canceled","code":"interrupted"}
client exit: 124
{"status":"running","running":true}
584 584 257 00:14 /bin/bash ./agent.sh
586 584 257 00:14 sleep 3600
587 587 587 00:14 sleep 7200
11:02:56 step 8The client gave up. The agent didn't notice. Note PID 587: setsid gave it its own process group (PGID 587), so a plain kill -- -584 from inside the sandbox would miss it.
2. Kill the command: the agent and its children end#
sbx exec $id --kill 584 | jq -c '{processId, running, signaled, status: .command.status}'
sbx exec $id --status 584 | jq -c '{status, running, exitCode, signal}'
sbx exec $id 'ps -o pid,args -u user | grep -E "agent|sleep (3600|7200)" | grep -v grep || echo none left; tail -n 2 agent.log'{"processId":584,"running":false,"signaled":true,"status":"killed"}
{"status":"exited","running":false,"exitCode":143,"signal":null}
none left
11:03:37 step 16
11:03:38 SIGTERM: saving stateThe kill returned in 0.24 seconds. It reached the agent (its trap ran), the child in its group, and the setsid child outside the group. --kill signals the command's process group and every descendant it can find, including ones that left the group. The CLI reference says "Stop it and its children".
Then a command that ignores SIGTERM:
p=$(sbx exec $id --detach 'trap "" TERM; sleep 600 & wait; sleep 600' | jq -r .processId)
sbx exec $id --kill $p | jq -c '{running, signaled}' # took 5.30 s
sbx exec $id --status $p | jq -c '{status, exitCode, signal}'
sbx exec $id 'pgrep -c -x sleep || true'{"running":false,"signaled":true}
{"status":"exited","exitCode":137,"signal":"SIGKILL"}
0The kill call took 5.30 seconds: SIGTERM was ignored, so SIGKILL came after the 5-second grace. The kill call waits for that, so when it returns the processes are gone. If your agent needs to save state, trap SIGTERM and finish within 5 seconds. The SDKs' kill() does the same, per the Python SDK docs ("SIGTERM, then SIGKILL after 5 s"); the MCP server's kill_command tool stops a detached command by processId (MCP tools). We ran the CLI form.
--kill only stops commands started with --detach. For an agent you drive with sbx prompt, the CLI has sbx interrupt <id>, which stops the running agent work in every conversation, or one with --convo. We read its help but didn't run it here.
3. Stop the sandbox: the agent freezes, it doesn't die#
sbx stop saves memory. It's a pause, not a cancel. We ran it before the kill in step 2, with the agent mid-loop:
sbx stop $id --wait
sbx resume $id --json | tail -1 | jq -c '.sandbox | {state, memoryRestored}'
sbx exec $id --status 584 | jq -c '{status, running, pid}'
sbx exec $id 'grep step agent.log | tail -n 3'{"state":"ready","memoryRestored":true}
{"status":"running","running":true,"pid":584}
11:02:58 step 9
11:03:25 step 10
11:03:27 step 11Same PID 584, and the loop picked up at step 10 after a 27-second gap. If the "cancelled" step was pushing a branch or calling an API, it does that after the resume. That's why a stop alone is the wrong cancel. If a resume lands on a different CPU type, it boots from disk instead (memoryRestored: false) and the processes are gone; resuming after a crash covers writing an agent that handles both.
While stopped, the sandbox refuses commands rather than waking:
{"error":"The sandbox is stopped. Resume it first.","code":"machine_not_running"}We got that from sbx exec --status after both a manual sbx stop and a TTL auto-stop.
4. Delete the sandbox: everything ends#
sbx delete $id --yes
sbx info $id --json{"event":"error","error":"Sandbox not found","code":"not_found","status":404}Deleting removes the processes, the disk and the snapshots. Save what you want first: what to keep before deleting.
The backstop: a TTL, and what idle pause does#
If your handler crashes, nothing kills or deletes the sandbox. A TTL is the deadline that still fires. We restarted the agent, set the idle timeout to 1 minute and the TTL to 150 seconds:
sbx idle $id 1
sbx extend $id --ttl 150
# 2 minutes later, with the agent still looping:
sbx info $id --json | jq -c '.sandbox | {state, idleStopMinutes, lastActivityAt}'
# 75 seconds after that:
sbx info $id --json | jq -c '.sandbox | {state, stop: {reason: .stop.reason}}'{"state":"ready","idleStopMinutes":1,"lastActivityAt":"2026-11-23T11:06:12.058439Z"}
{"state":"archived","stop":{"reason":"auto_stop"}}Two things to take from this:
- Idle pause doesn't stop a working agent. The running command kept
lastActivityAtmoving, so the 1-minute idle timeout never fired. An "orphaned" agent from step 1 keeps its sandbox up until the TTL. - The TTL stops, it doesn't kill. The agent is frozen with its memory, like step 3. We didn't resume it here; per Long-running tasks, a resume after auto-stop brings the job back where it was. Pair the TTL with a watchdog that deletes stopped sandboxes you don't need; capping what agent sandboxes can cost has one.
An idle-paused sandbox is different from one you stopped: per Long-running tasks, it wakes on the next API call or request to its URL, with its processes (idle pause and wake on use shows it). We didn't test the wake on this page. So if your agent was idle (waiting, not working) when it paused, any later call to that sandbox brings the agent back. How long the TTL can be and how to renew it while a job is legitimately running is in how long an agent sandbox can run.
Copy-paste: a cancel handler that kills, waits and deletes#
After curl -fsSL https://sandboxer.com/install | sh and sbx login, with jq installed. It starts a stand-in agent (a loop plus a child and a setsid child), then cancels it.
set -u
id=$(sbx new --name aeo-cancel-2 --type small --ttl 1800 --json | jq -r 'select(.event=="ready") | .id')
# The "agent": a step loop with a child tool call and a dev server that calls setsid.
pid=$(sbx exec "$id" --detach 'sleep 3600 & setsid sleep 7200 & while :; do date +%T >> steps.log; sleep 2; done' | jq -r .processId)
sleep 5
# The cancel handler: kill -> wait -> delete.
cancel() {
sbx exec "$1" --kill "$2" | jq -c '{signaled, status: .command.status}'
until [ "$(sbx exec "$1" --status "$2" | jq -r .running)" = false ]; do sleep 1; done
sbx exec "$1" "pgrep -c sleep || echo 'no agent processes left'"
sbx delete "$1" --yes >/dev/null && echo "deleted $1"
}
cancel "$id" "$pid"
sbx info "$id" --json 2>&1 | jq -c '{code, status}'{"signaled":true,"status":"killed"}
0
no agent processes left
deleted sb_mw6poq9o98
{"code":"not_found","status":404}The 0 is pgrep -c finding no sleep processes. Wire cancel into your workflow's cancel hook, and store the sandbox ID and process ID when the step starts so the hook has them.
FAQ#
Does cancelling my workflow stop the command in the sandbox?#
No. In our test the agent kept running after the client was cancelled, and --status still said running. Only a kill, a stop or a delete reaches it.
Does sbx exec --kill stop child processes?#
Yes. It sends SIGTERM to the command's process group and to descendants that left the group (such as a setsid dev server), then SIGKILL to anything left after 5 seconds. In our test no child survived.
Is sbx stop a safe way to cancel an agent?#
No. It saves memory, and sbx resume brought our agent back with the same PID, continuing its loop. Kill the command, or delete the sandbox, when you mean "cancel".
Will an idle timeout stop an agent I forgot about?#
Not while it's working: a running command counts as activity. Set a TTL as the backstop. When the TTL fires, the sandbox stops with memory, so delete it if you don't plan to resume.
Related#
More guides
For running AI-written code we rank Sandboxer, Freestyle and E2B first: each gives code its own VM with an API or SDK and can pause with memory. Pick Daytona for Windows or GPUs, Modal or Northflank for GPUs at scale, E2B or Northflank to run in your own cloud, and Vercel Sandbox if your app lives on Vercel.
For coding agents we rank Sandboxer first: a full Ubuntu VM with Claude Code and Codex preinstalled, Docker, and stop-and-resume that keeps the agent's session in memory. Freestyle and Amp Orbs also keep memory across pauses. Pick E2B for prebuilt agent templates on open source, Docker Sandboxes for free local runs, and Codespaces if you live in GitHub.
The best Daytona alternatives depend on why you're switching. For a full Linux microVM on every sandbox with memory-saving stop and Docker, we rank Sandboxer first. E2B suits open source and self-hosting, Modal massive short-job fleets, Northflank bring-your-own-cloud with GPUs, and Vercel Sandbox apps on Vercel.
If you're leaving E2B for longer sessions or a fuller machine, we rank Sandboxer first, then Freestyle: both give each sandbox a full Linux VM that keeps memory across pauses, with Docker and no short session cap. Pick Daytona for Windows or GPUs, Northflank for bring-your-own-cloud, and stay on E2B if you need open source.