diff --git a/internal/daemon/daemon.go b/internal/daemon/daemon.go index 7052e0e3d..1ea1bb8ea 100644 --- a/internal/daemon/daemon.go +++ b/internal/daemon/daemon.go @@ -1899,6 +1899,10 @@ func (d *Daemon) runViaAgent(ctx context.Context, task *workflow.Task, repo conf } reply, err := d.requestTaskRun(ctx, task.ID, data) + if err != nil && stoppingOnly(ctx) { + d.Log.Info("daemon stopping; task left running for the next daemon to re-queue", "task", task.ID) + return + } if cause := context.Cause(ctx); err != nil && (errors.Is(cause, errContainerExited) || errors.Is(cause, errTaskTimeLimit)) { err = cause } diff --git a/internal/daemon/running.go b/internal/daemon/running.go index 2b427599f..e125e1a4a 100644 --- a/internal/daemon/running.go +++ b/internal/daemon/running.go @@ -2,6 +2,7 @@ package daemon import ( "context" + "errors" "sort" "sync" ) @@ -20,13 +21,22 @@ type runningTasks struct { type runningEntry struct { identity string - cancel context.CancelFunc + cancel context.CancelCauseFunc +} + +// errTaskStopped is the cancellation cause when an operator stops a task. +var errTaskStopped = errors.New("task stopped") + +// stoppingOnly reports whether ctx ended only because the daemon is shutting +// down. Such a task is left running: the next daemon's startup re-queues it. +func stoppingOnly(ctx context.Context) bool { + return ctx.Err() != nil && errors.Is(context.Cause(ctx), context.Canceled) } // begin registers a task as running and returns a context that [stop] can // cancel, together with the function that retires it. func (r *runningTasks) begin(ctx context.Context, id int64, identity string) (context.Context, func()) { - taskCtx, cancel := context.WithCancel(ctx) + taskCtx, cancel := context.WithCancelCause(ctx) r.mu.Lock() if r.entries == nil { @@ -39,7 +49,7 @@ func (r *runningTasks) begin(ctx context.Context, id int64, identity string) (co r.mu.Lock() delete(r.entries, id) r.mu.Unlock() - cancel() + cancel(nil) } } @@ -51,7 +61,7 @@ func (r *runningTasks) stop(id int64) bool { if !ok { return false } - entry.cancel() + entry.cancel(errTaskStopped) return true } diff --git a/internal/daemon/running_test.go b/internal/daemon/running_test.go new file mode 100644 index 000000000..370d54e8e --- /dev/null +++ b/internal/daemon/running_test.go @@ -0,0 +1,48 @@ +package daemon + +import ( + "context" + "testing" +) + +func TestStoppingOnly(t *testing.T) { + tests := []struct { + name string + end func(parent context.CancelFunc, r *runningTasks, run context.Context) context.Context + want bool + }{ + {"daemon shutdown", func(parent context.CancelFunc, _ *runningTasks, run context.Context) context.Context { + parent() + return run + }, true}, + {"operator stop", func(_ context.CancelFunc, r *runningTasks, run context.Context) context.Context { + r.stop(1) + return run + }, false}, + {"container exit", func(_ context.CancelFunc, _ *runningTasks, run context.Context) context.Context { + exited := make(chan struct{}) + close(exited) + ctx, _ := withContainerExit(run, closedContainer{exited}) + <-ctx.Done() + return ctx + }, false}, + {"still running", func(context.CancelFunc, *runningTasks, context.Context) context.Context { return context.Background() }, false}, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + parent, cancel := context.WithCancel(context.Background()) + defer cancel() + r := &runningTasks{} + run, done := r.begin(parent, 1, "") + defer done() + if got := stoppingOnly(tt.end(cancel, r, run)); got != tt.want { + t.Fatalf("stoppingOnly = %v, want %v", got, tt.want) + } + }) + } +} + +type closedContainer struct{ exited chan struct{} } + +func (c closedContainer) Exited() <-chan struct{} { return c.exited } +func (closedContainer) ExitDetail() string { return "" }