diff --git a/internal/container/pool.go b/internal/container/pool.go index 29980de1b..04b6635c8 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -221,6 +221,26 @@ func (p *Pool) Acquire(ctx context.Context, mounts []storage.Mount, env []string return &Container{ID: resp.ID}, nil } +// releaseDecision reports how Release should tear a container down: whether +// to honour the configured post-completion grace period, and what Docker +// stop timeout to request (nil means the container's configured timeout or +// the engine default; a non-nil zero means stop immediately with SIGKILL, +// skipping the graceful SIGTERM wait). +// +// A cancelled ctx means the release is happening because the task was +// stopped, not because it finished -- /stop is an emergency brake, so +// neither the grace period (meant for a task that completed on its own and +// might get a follow-up) nor a graceful stop (meant to let a healthy +// process wind down) apply. A still-live ctx is a normal completion, so +// both keep their existing meaning. +func releaseDecision(ctx context.Context, grace time.Duration) (honorGrace bool, stopTimeout *int) { + if ctx.Err() != nil { + zero := 0 + return false, &zero + } + return grace > 0, nil +} + // Release stops and removes a container after a task completes. If // GracePeriod is configured, the container stays alive for that duration // before being killed -- the agent can handle follow-ups (gate re-runs, @@ -229,8 +249,8 @@ func (p *Pool) Release(ctx context.Context, c *Container) { if c == nil { return } - // Honour the post-completion grace period. - if p.cfg.GracePeriod > 0 { + honorGrace, stopTimeout := releaseDecision(ctx, p.cfg.GracePeriod) + if honorGrace { p.log.Info("container keeping alive for grace period", "id", c.ID[:12], "grace", p.cfg.GracePeriod) time.Sleep(p.cfg.GracePeriod) } @@ -244,7 +264,7 @@ func (p *Pool) Release(ctx context.Context, c *Container) { stopCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) defer cancel() - if _, err := p.cli.ContainerStop(stopCtx, c.ID, client.ContainerStopOptions{}); err != nil { + if _, err := p.cli.ContainerStop(stopCtx, c.ID, client.ContainerStopOptions{Timeout: stopTimeout}); err != nil { p.log.Warn("container stop failed", "id", c.ID[:12], "err", err) } if _, err := p.cli.ContainerRemove(context.WithoutCancel(ctx), c.ID, client.ContainerRemoveOptions{Force: true}); err != nil { diff --git a/internal/container/pool_test.go b/internal/container/pool_test.go index e86b1434b..f358ae7de 100644 --- a/internal/container/pool_test.go +++ b/internal/container/pool_test.go @@ -1,6 +1,7 @@ package container import ( + "context" "encoding/json" "os" "path/filepath" @@ -49,6 +50,61 @@ func TestContainerSupportsGracePeriod(t *testing.T) { _ = cfg.GracePeriod } +// ── regression: /stop must not wait out a grace period or a graceful +// SIGTERM timeout it never asked for ───────────────────────────────────── + +// TestReleaseDecisionSkipsGracePeriodWhenCancelled: /stop cancels the +// task's context before Release runs. A container being torn down because +// it was cancelled must not sit through a configured post-completion grace +// period meant for normal follow-ups (gate re-runs, human replies) -- that +// window is for a task that finished on its own, not one that was killed. +func TestReleaseDecisionSkipsGracePeriodWhenCancelled(t *testing.T) { + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + honorGrace, timeout := releaseDecision(ctx, 5*time.Minute) + + if honorGrace { + t.Error("a cancelled release must not honour the grace period") + } + if timeout == nil || *timeout != 0 { + t.Errorf("a cancelled release must stop immediately (Timeout 0), got %v", timeout) + } +} + +// TestReleaseDecisionHonoursGracePeriodOnNormalCompletion: a task that +// finished on its own (ctx still live) with a configured grace period +// should keep the behaviour PRD section 1 describes -- stay alive for +// follow-ups, then stop gracefully. +func TestReleaseDecisionHonoursGracePeriodOnNormalCompletion(t *testing.T) { + ctx := context.Background() + + honorGrace, timeout := releaseDecision(ctx, 5*time.Minute) + + if !honorGrace { + t.Error("normal completion with a configured grace period must honour it") + } + if timeout != nil { + t.Errorf("normal completion must use the default graceful stop timeout, got %v", timeout) + } +} + +// TestReleaseDecisionNoGracePeriodConfigured: no grace period configured +// (the zero value, and the only value ever wired in production today) must +// never sleep, on a live or cancelled context alike. +func TestReleaseDecisionNoGracePeriodConfigured(t *testing.T) { + ctx := context.Background() + + honorGrace, timeout := releaseDecision(ctx, 0) + + if honorGrace { + t.Error("a zero grace period must never be honoured") + } + if timeout != nil { + t.Errorf("normal completion must use the default graceful stop timeout, got %v", timeout) + } +} + func TestWriteTaskJSONProducesValidFile(t *testing.T) { dir := t.TempDir() payload := TaskPayload{