fix(container): stop a cancelled task's container immediately

Release() always slept the configured GracePeriod and always used
Docker's default ~10s graceful SIGTERM-then-SIGKILL timeout, even
when the release was happening because /stop had just cancelled the
task's context. A cancelled task's container -- and the in-flight
generation/tool calls inside it -- could sit doing nothing for up to
GracePeriod+~10s before actually dying, so /stop reported success
while work visibly continued for tens of seconds to a couple minutes.

releaseDecision(ctx, grace) now skips the grace sleep and requests an
immediate SIGKILL (Timeout: 0) when ctx is already cancelled; a live
ctx (normal completion) keeps the original behaviour byte for byte.

Closes archie-core-1786749700580-5-e3878224.
This commit is contained in:
sam committed 2026-08-18 09:05:21 +10:00
1 parent 79a92fd63d
commit 0b5c808400
2 files changed
+79 -3

No files matched your search

+23 -3
View File
@@ -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 {
+56
View File
@@ -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{