Skip to content

fix(sandbox): remove workers on failed execs and task deletion - #764

Merged
BaaRai merged 1 commit into
BaaRai:mainfrom
renaesop:codex/runner-exec-cleanup
Dec 1, 2026
Merged

BaaRai merged 1 commit into
BaaRai:mainfrom
renaesop:codex/runner-exec-cleanup

Conversation

@renaesop

@renaesop renaesop commented Dec 1, 2026

Copy link
Copy Markdown
Contributor

Deleting a task can leave its worker container attached to the task network: the process scan stops application processes but preserves PID 1 and the network endpoint. Exec timeouts, interrupted streams, and unverifiable completion responses can also return without removing the worker that ran the command.

This change removes the worker before its proxy and network on task deletion. For execution cleanup, it captures the container ID and force-removes that container after a timeout, cancellation, stream/start error, or a completion status that cannot be confirmed. Removal uses a fresh bounded context; failures remain visible and retain the state needed for retry. Task creation and cleanup share the lifecycle lock, and delayed cleanup preserves replacement workers.

Task-level behavior: removing a worker terminates all concurrent commands and background services in that task. Other tasks keep running, /workspace is preserved, and the next exec recreates the worker. Successful commands retain their worker. This behavior is documented in docs/SANDBOX.md.

Validation on macOS with Go 1.26.11:

  • TMPDIR=/tmp go test -race -count=1 ./sandbox ./cmd/artex-runner ./cmd/artex-egress-proxy ./cmd/artex-exec-wrap: passed; 22 top-level sandbox tests passed, 3 skipped. The command packages have no test files.
  • Concurrent sibling termination, another task remaining active, deletion versus recreation, and delayed cleanup preserving a replacement worker are covered. The three concurrency/replacement tests also passed 10 repetitions under -race.
  • The same focused cleanup regression tests fail on unmodified 613ae14; the successful-exec control passes on both versions.
  • go vet on the affected packages, gofmt, and git diff --check passed. The packages build for Linux amd64/arm64, macOS amd64/arm64, and Windows amd64 with CGO_ENABLED=0.

Not yet validated locally: Docker Engine and the Docker CLI are unavailable here, so the two live Docker tests and the Compose check were skipped. The new opt-in TestDockerTimeoutRemovesDetachedChild uses a local file canary to check that a detached child stops after timeout cleanup. Full repository CI has not been run locally.

@thecorporatetractor

Copy link
Copy Markdown
Contributor

Ran go test -race -count=1 ./sandbox/ on the branch: pass. Against Docker Engine 27.5.1, TestDockerSandboxE2EContainment and the new TestDockerTimeoutRemovesDetachedChild both pass, and Delete no longer fails on the task network (it fails on main with "has active endpoints").

One behaviour change worth a look. sandbox/docker.go:703-704 treats any exit code 124 as a runner timeout and now removes the worker:

timedOut := exitCode == 124
if timedOut || ctx.Err() != nil { removeExecWorker(...) }

124 is also what coreutils timeout returns, and the agent uses timeout N <tool> a lot. Repro on this branch, live Docker, exec budget 30s:

Exec(Argv: ["timeout","1","sleep","5"], TimeoutMS: 30000)
-> {ExitCode:124 TimedOut:true DurationMS:1570}, err=nil
-> GET /containers/<worker>/json: 404

So an ordinary inner timeout tears down the worker and every background job/service of the task. The TimedOut:true misreport was already there on main; removing the container is new. Could exec-wrap report its own timeout out-of-band (a distinct exit code or marker), or could the runner at least require DurationMS >= TimeoutMS before removing?

Two smaller points:

  • ensureTask now inserts a placeholder taskContainers into b.tasks before anything is created (docker.go:446). If creation fails, the entry stays and WorkerCount() counts it. That is harmless for Delete, and it arguably helps by keeping the network around for cleanup, but it is worth a comment.
  • fix(sandbox): remove the worker container in Delete #778 fixes the same Delete bug with a 7-line change. If this lands, fix(sandbox): remove the worker container in Delete #778's hunk becomes a second removeContainer on an already-cleared ID (the branches merge without a conflict).

@BaaRai
BaaRai merged commit 44f6f03 into BaaRai:main Dec 1, 2026
8 checks passed
@thecorporatetractor

Copy link
Copy Markdown
Contributor

Follow-up for the exit 124 case above: #796 keeps the worker when the command itself exits 124, and keeps the removal on real timeouts.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants