From 79fbdc847828b362966e31d81d3ebdbca23b5e90 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 20 Sep 2026 00:20:31 +1000 Subject: [PATCH] fix(container): stop the reaper from deleting teardown bookkeeping early (#893) The reaper's deferred forgetTeardown could delete a container's teardown entry between the reaper's own claim and Release's later claim check. teardownLocked then recreated a fresh, unclosed entry, letting Release believe it still owned teardown and re-run ContainerStop/ContainerRemove. Release is now the sole owner of deleting the entry, since it always runs for every acquired container. --- internal/container/pool.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/container/pool.go b/internal/container/pool.go index 0006c58a9..323829f4a 100644 --- a/internal/container/pool.go +++ b/internal/container/pool.go @@ -256,12 +256,17 @@ type containerTeardown struct { // armMaxUptime schedules a hard stop and remove for a container once its // lifetime cap elapses. The timer never touches p.active: Release (or Close) // remains the only owner of the active slot. +// +// The reaper never calls forgetTeardown itself: Release is always coming for +// this container (the pool guarantees every acquired container is eventually +// released) and is the sole owner of deleting the map entry. Two callers +// deleting it independently is what let a freshly-recreated, unclosed entry +// slip between the reaper's delete and Release's claim check. func (p *Pool) armMaxUptime(ctx context.Context, id string) { timer := time.AfterFunc(p.cfg.MaxUptime, func() { if !p.claimTeardown(id) { return } - defer p.forgetTeardown(id) zero := 0 stopCtx, cancel := context.WithTimeout(context.WithoutCancel(ctx), 10*time.Second) defer cancel() @@ -292,7 +297,9 @@ func (p *Pool) claimTeardown(id string) bool { } // forgetTeardown disarms the reaper and drops the container's bookkeeping, so -// the map does not grow for the life of the pool. +// the map does not grow for the life of the pool. Only Release calls this +// (see armMaxUptime); it is the container's final teardown step, so nothing +// else can race a fresh entry back into existence afterward. func (p *Pool) forgetTeardown(id string) { p.mu.Lock() t := p.teardowns[id]