From 0dccb9160f937396bb15ae0e1a8565897b60c3a3 Mon Sep 17 00:00:00 2001 From: SREsAreHumanToo <88474954+sresarehumantoo@users.noreply.github.com> Date: Tue, 1 Dec 2026 14:19:18 -0500 Subject: [PATCH] fix(sandbox): remove the worker container in Delete --- sandbox/docker.go | 7 ++++++ sandbox/docker_cleanup_test.go | 45 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) diff --git a/sandbox/docker.go b/sandbox/docker.go index 663261571..e4e6fdd9f 100644 --- a/sandbox/docker.go +++ b/sandbox/docker.go @@ -831,7 +831,14 @@ func (b *DockerBackend) Delete(ctx context.Context, taskID string) error { return fmt.Errorf("delete task %s: %w", taskID, err) } + // A successful proc-scan kill leaves the worker running for reuse, so it + // must be removed here or it keeps the task network busy. var errs []error + if err := b.removeContainer(cctx, tc.workerID); err != nil { + errs = append(errs, err) + } else { + b.clearWorkerID(tc, tc.workerID) + } if err := b.removeContainer(cctx, tc.proxyID); err != nil { errs = append(errs, err) } else { diff --git a/sandbox/docker_cleanup_test.go b/sandbox/docker_cleanup_test.go index 3cfc2db12..3a32c7201 100644 --- a/sandbox/docker_cleanup_test.go +++ b/sandbox/docker_cleanup_test.go @@ -198,6 +198,51 @@ func TestDeleteKeepsStateWhenProxyRemovalFails(t *testing.T) { } } +// TestDeleteRemovesWorkerBeforeNetwork: a successful proc-scan kill leaves the +// worker running, so Delete must still remove it, and do so before the network, +// which Docker refuses to delete while the worker is attached. +func TestDeleteRemovesWorkerBeforeNetwork(t *testing.T) { + f := &fakeDocker{} + b := newFakeDockerBackend(t, f) + seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1") + + if err := b.Delete(context.Background(), "t1"); err != nil { + t.Fatalf("Delete: %v", err) + } + f.mu.Lock() + workerIdx, netIdx := -1, -1 + for i, c := range f.calls { + if c == "DELETE /containers/worker-1" { + workerIdx = i + } + if c == "DELETE /networks/artex-task-t1" { + netIdx = i + } + } + f.mu.Unlock() + if workerIdx == -1 || netIdx == -1 || workerIdx > netIdx { + t.Fatalf("Delete did not remove the worker before the network: %v", f.calls) + } +} + +// TestDeleteKeepsStateWhenWorkerRemovalFails: a worker that cannot be removed +// must keep the task and its worker ID for a retry. +func TestDeleteKeepsStateWhenWorkerRemovalFails(t *testing.T) { + f := &fakeDocker{deleteCode: map[string]int{"worker-1": http.StatusInternalServerError}} + b := newFakeDockerBackend(t, f) + tc := seedTask(b, "t1", "worker-1", "proxy-1", "artex-task-t1") + + if err := b.Delete(context.Background(), "t1"); err == nil { + t.Fatal("Delete reported success although the worker container could not be removed") + } + if b.tasks["t1"] != tc { + t.Fatal("Delete unpublished the task after a failed worker removal") + } + if tc.workerID != "worker-1" { + t.Fatalf("Delete cleared workerID after a failed removal: %q", tc.workerID) + } +} + // TestDeleteUnpublishesOnlyAfterFullSuccess covers the happy path. func TestDeleteUnpublishesOnlyAfterFullSuccess(t *testing.T) { f := &fakeDocker{}