From 8070ff4899b9b8503d9b93aeb894e210dd1d77ba Mon Sep 17 00:00:00 2001 From: Marco Azarmaneche <306425250+thecorporatetractor@users.noreply.github.com> Date: Tue, 1 Dec 2026 23:58:47 +0200 Subject: [PATCH] fix(sandbox): keep the Docker worker when a command exits 124 by itself the timeout by exit code 124. artex-exec-wrap returns 124 when it enforces timeout_ms, but the command can also exit 124 itself: coreutils `timeout 1 sleep 5` does. The runner then removed the worker and stopped the task's background jobs after an ordinary inner timeout. Count an exit 124 as the runner's timeout only when the exec ran for at least timeout_ms, which is the only way the wrapper can return it. --- CHANGELOG.md | 1 + sandbox/docker.go | 7 ++++- sandbox/docker_cleanup_test.go | 3 ++ sandbox/docker_timeout_e2e_test.go | 47 ++++++++++++++++++++++++++++++ sandbox/exec_cleanup_test.go | 32 +++++++++++++++++--- sandbox/exec_concurrency_test.go | 3 +- 6 files changed, 87 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d21e99130..e538087e6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ All notable changes to this project are documented in this file. The format is b ### Fixed +- **A command that exits 124 no longer removes the Docker worker.** The runner treated every exit code 124 as its own timeout and removed the task's worker container, which also stopped the task's background jobs. Commands such as `timeout 5 curl ...` exit 124 by themselves. An exit 124 now counts as a runner timeout only when the command ran for at least `timeout_ms`. - **An `exact_ip` task allow rule grants sandbox egress**: the egress allowlist read task allow rules of kind `ip`, which the rule tables never store, so an `exact_ip` allow rule granted nothing. A task scoped to one private address (`10.0.0.5`) could not reach it from the sandbox, directly or through a name resolving only to it. `exact_ip` allow rules now join the allowed CIDRs as a host prefix, like `cidr` allow rules. - **`GET /api/tasks/{id}/scope` reports scope approval**: the query behind it did not select `approved_by_operator` or `expanded`, so every row came back with `approved_by_operator: false` and `expanded: false`, including operator-approved rows that grant sandbox egress. The rows now carry both columns as stored, for the task's own scope and for its source tasks' scope. - **The OpenAI Responses format no longer defaults to `gpt-5`**: OpenAI shuts `gpt-5` down on 2026-12-11 and lists `gpt-6.2-sol` as its replacement, so `ARTEX_LLM_PROVIDER=openai-responses` without `ARTEX_LLM_MODEL` (or a profile saved through the API without a model) would start failing then. The default is now `gpt-6.2-sol`; a model set in the profile or in `ARTEX_LLM_MODEL` is kept as is. A gateway set with `ARTEX_LLM_BASE_URL` that does not serve `gpt-6.2-sol` needs `ARTEX_LLM_MODEL` set explicitly. diff --git a/sandbox/docker.go b/sandbox/docker.go index b197276d4..1e4b3adb3 100644 --- a/sandbox/docker.go +++ b/sandbox/docker.go @@ -665,6 +665,7 @@ func (b *DockerBackend) Exec(ctx context.Context, taskID string, req ExecRequest execCtx, cancel := context.WithTimeout(ctx, time.Duration(req.TimeoutMS)*time.Millisecond+12*time.Second) defer cancel() + execStart := time.Now() startResp, err := b.dockerDo(execCtx, http.MethodPost, "/exec/"+url.PathEscape(created.ID)+"/start", map[string]any{ "Detach": false, "Tty": false, @@ -700,7 +701,11 @@ func (b *DockerBackend) Exec(ctx context.Context, taskID string, req ExecRequest } exitCode := *ins.ExitCode - timedOut := exitCode == 124 + // artex-exec-wrap exits 124 when it enforces timeout_ms, but the command + // itself can also exit 124 (coreutils `timeout N cmd` does). Only the + // wrapper's own timeout can take timeout_ms or longer, so a 124 that came + // back sooner is the command's exit code: keep the worker. + timedOut := exitCode == 124 && time.Since(execStart) >= time.Duration(req.TimeoutMS)*time.Millisecond if timedOut || ctx.Err() != nil { // Process-group timeout cannot kill setsid/double-fork descendants. // Destroy the captured container, including its PID namespace and cgroup. diff --git a/sandbox/docker_cleanup_test.go b/sandbox/docker_cleanup_test.go index 9eae6410b..eef79bfc3 100644 --- a/sandbox/docker_cleanup_test.go +++ b/sandbox/docker_cleanup_test.go @@ -9,6 +9,7 @@ import ( "strings" "sync" "testing" + "time" ) // fakeDocker is a minimal Docker Engine API stub over a unix socket. It records @@ -29,6 +30,7 @@ type fakeDocker struct { inspectBody string streamReady chan struct{} streamStarted bool + execDelay time.Duration // delay before the exec stream answers, as a running command would } func (f *fakeDocker) ServeHTTP(w http.ResponseWriter, r *http.Request) { @@ -51,6 +53,7 @@ func (f *fakeDocker) ServeHTTP(w http.ResponseWriter, r *http.Request) { w.WriteHeader(f.execStartCode) return } + time.Sleep(f.execDelay) w.WriteHeader(http.StatusOK) f.mu.Lock() firstStream := f.streamReady != nil && !f.streamStarted diff --git a/sandbox/docker_timeout_e2e_test.go b/sandbox/docker_timeout_e2e_test.go index eb770abd2..a8401264e 100644 --- a/sandbox/docker_timeout_e2e_test.go +++ b/sandbox/docker_timeout_e2e_test.go @@ -70,3 +70,50 @@ time.sleep(10)` t.Fatalf("child continued after timeout: %v", err) } } + +// A command that exits 124 by itself must not be treated as the runner's +// timeout: the worker, and with it the task's background jobs, stays. +func TestDockerCommandExit124KeepsWorker(t *testing.T) { + if os.Getenv("ARTEX_SANDBOX_DOCKER_TEST") != "1" { + t.Skip("requires live Docker and built sandbox images") + } + ws, au := t.TempDir(), t.TempDir() + id := fmt.Sprintf("exit124-%d", time.Now().UnixNano()) + egressNet := "artex-test-egress-" + id + for _, dir := range []string{ws, au, filepath.Join(ws, id), filepath.Join(au, id)} { + if err := os.MkdirAll(dir, 0o777); err != nil { + t.Fatal(err) + } + if err := os.Chmod(dir, 0o777); err != nil { + t.Fatal(err) + } + } + b := NewDockerBackend(DockerConfig{WorkspaceRoot: ws, HostWorkspaceRoot: ws, AuditRoot: au, HostAuditRoot: au, + TaskNetPrefix: "artex-test-task-", EgressNet: egressNet}) + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + t.Cleanup(func() { + _ = b.Delete(context.Background(), id) + _ = b.removeNetwork(context.Background(), egressNet) + }) + if err := b.Ping(ctx); err != nil { + t.Fatal(err) + } + tc, err := b.ensureTask(ctx, id, nil, nil) + if err != nil { + t.Fatal(err) + } + worker := tc.workerID + res, err := b.Exec(ctx, id, ExecRequest{Argv: []string{"timeout", "1", "sleep", "5"}, Workdir: "/workspace", TimeoutMS: 30000}) + if err != nil || res.ExitCode != 124 || res.TimedOut { + t.Fatalf("response=%+v error=%v", res, err) + } + r, err := b.dockerDo(ctx, http.MethodGet, "/containers/"+worker+"/json", nil) + if err != nil { + t.Fatal(err) + } + defer r.Body.Close() + if r.StatusCode != http.StatusOK { + t.Fatalf("worker removed after the command's own exit 124: HTTP %d", r.StatusCode) + } +} diff --git a/sandbox/exec_cleanup_test.go b/sandbox/exec_cleanup_test.go index 7ec568ca1..4c7e92209 100644 --- a/sandbox/exec_cleanup_test.go +++ b/sandbox/exec_cleanup_test.go @@ -13,7 +13,7 @@ func TestExecFailsClosedAndRemovesWorker(t *testing.T) { fake *fakeDocker wantErr bool }{ - {"timeout", &fakeDocker{execExitCode: 124}, false}, + {"timeout", &fakeDocker{execExitCode: 124, execDelay: 20 * time.Millisecond}, false}, {"still_running", &fakeDocker{execRunning: true}, true}, {"truncated_stream", &fakeDocker{execStream: []byte{1, 0, 0}}, true}, {"truncated_payload", &fakeDocker{execStream: []byte{1, 0, 0, 0, 0, 0, 0, 4, 'x'}}, true}, @@ -27,7 +27,11 @@ func TestExecFailsClosedAndRemovesWorker(t *testing.T) { b := newFakeDockerBackend(t, test.fake) tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1") tc.scopeHash = scopeFingerprint(nil, nil) - res, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 1000}) + timeoutMS := 1000 + if test.fake.execDelay > 0 { + timeoutMS = 10 + } + res, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: timeoutMS}) if (err != nil) != test.wantErr { t.Fatalf("response=%+v error=%v", res, err) } @@ -70,11 +74,11 @@ func TestExecCancellationCleansUpWithFreshContext(t *testing.T) { } func TestExecTimeoutRemovalFailureKeepsWorker(t *testing.T) { - f := &fakeDocker{execExitCode: 124, deleteCode: map[string]int{"worker-1": 500}} + f := &fakeDocker{execExitCode: 124, execDelay: 20 * time.Millisecond, deleteCode: map[string]int{"worker-1": 500}} b := newFakeDockerBackend(t, f) tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1") tc.scopeHash = scopeFingerprint(nil, nil) - if _, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 1000}); err == nil { + if _, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"true"}, TimeoutMS: 10}); err == nil { t.Fatal("cleanup failure hidden") } if tc.workerID != "worker-1" { @@ -106,3 +110,23 @@ func TestDelayedExecCleanupPreservesReplacementWorker(t *testing.T) { t.Fatal("late cleanup targeted or forgot replacement worker") } } + +// A command that exits 124 by itself (coreutils `timeout 1 sleep 5`) did not +// hit the runner's timeout: report its exit code and keep the worker, so the +// task's background jobs survive. +func TestExecCommandExit124BeforeTimeoutKeepsWorker(t *testing.T) { + f := &fakeDocker{execExitCode: 124} + b := newFakeDockerBackend(t, f) + tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1") + tc.scopeHash = scopeFingerprint(nil, nil) + res, err := b.Exec(context.Background(), "t1", ExecRequest{Argv: []string{"timeout", "1", "sleep", "5"}, TimeoutMS: 30000}) + if err != nil { + t.Fatal(err) + } + if res.ExitCode != 124 || res.TimedOut { + t.Fatalf("command exit 124 reported as runner timeout: %+v", res) + } + if tc.workerID != "worker-1" || f.seen("DELETE /containers/worker-1") { + t.Fatal("worker removed after the command's own exit 124") + } +} diff --git a/sandbox/exec_concurrency_test.go b/sandbox/exec_concurrency_test.go index 66af87186..b8b9fd700 100644 --- a/sandbox/exec_concurrency_test.go +++ b/sandbox/exec_concurrency_test.go @@ -50,6 +50,7 @@ func TestExecTimeoutStopsSiblingsButPreservesOtherTask(t *testing.T) { } } case p == "/exec/timeout/start": + time.Sleep(20 * time.Millisecond) w.WriteHeader(http.StatusOK) case p == "/exec/sibling/json": http.Error(w, "worker removed", http.StatusNotFound) @@ -89,7 +90,7 @@ func TestExecTimeoutStopsSiblingsButPreservesOtherTask(t *testing.T) { waitSignal(t, ctx, siblingStarted) go run("b", "other", otherDone) waitSignal(t, ctx, otherStarted) - res, err := b.Exec(ctx, "a", ExecRequest{Argv: []string{"timeout"}, TimeoutMS: 1000}) + res, err := b.Exec(ctx, "a", ExecRequest{Argv: []string{"timeout"}, TimeoutMS: 10}) if err != nil || !res.TimedOut { t.Fatalf("timeout response=%+v err=%v", res, err) }