Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions sandbox/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
45 changes: 45 additions & 0 deletions sandbox/docker_cleanup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down
Loading