mirror of
https://github.com/docker/compose.git
synced 2026-11-18 14:13:49 +00:00
fix(up): don't derive an already-expired context for the final wait check
docker-agent review (strong confidence): exec.run only bounds OpWaitCondition nodes with the wait deadline, not create/start/hook ops (see the previous commit) -- a plan whose non-wait work alone takes longer than --wait-timeout can still return nil past the deadline. context.WithDeadline on an already-past deadline silently produces an immediately-canceled context, failing the next daemon call (getContainers) with a bare context.DeadlineExceeded indistinguishable from "health never came up" -- misleading, since health was never actually checked. Extracted the deadline-wrapping into applyRemainingDeadline, which checks the remaining budget first and fails immediately with a message that says what actually happened, instead of letting a confusing daemon-call failure stand in for it. Pinned by three unit tests (zero deadline is a no-op, a future deadline wraps ctx normally, a past deadline fails immediately with the expected message) -- the past-deadline case verified by ablation to reproduce a false "nil error" without the check. Signed-off-by: Nicolas De Loof <nicolas.deloof@gmail.com>
This commit is contained in:
parent
e7463d69a9
commit
f067cc9a70
2 changed files with 71 additions and 4 deletions
|
|
@ -122,11 +122,20 @@ func (s *composeService) upDetached(ctx context.Context, project *types.Project,
|
|||
// (when set) rather than opening a fresh WaitTimeout window of its own
|
||||
// -- one overall budget for "becoming healthy", covering both the
|
||||
// plan's own waits and this final check, not one budget per wait.
|
||||
if !deadline.IsZero() {
|
||||
withDeadline, cancel := context.WithDeadline(ctx, deadline)
|
||||
defer cancel()
|
||||
ctx = withDeadline
|
||||
//
|
||||
// exec.run only bounds OpWaitCondition nodes with that deadline (see
|
||||
// above): a plan whose create/start/hook ops alone take longer than
|
||||
// WaitTimeout can still return nil past it. applyRemainingDeadline
|
||||
// catches that case explicitly instead of deriving an already-expired
|
||||
// context.WithDeadline, which would fail getContainers below before it
|
||||
// ever touches the daemon -- "not healthy" when health was never
|
||||
// actually checked.
|
||||
var cancel context.CancelFunc
|
||||
ctx, cancel, err = applyRemainingDeadline(ctx, deadline, options.Start.WaitTimeout)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer cancel()
|
||||
|
||||
containers, err := s.getContainers(ctx, project.Name, oneOffExclude, true)
|
||||
if err != nil {
|
||||
|
|
@ -152,6 +161,27 @@ func (s *composeService) upDetached(ctx context.Context, project *types.Project,
|
|||
return nil
|
||||
}
|
||||
|
||||
// applyRemainingDeadline derives ctx bound by deadline, the no-op
|
||||
// cancel/unmodified ctx when deadline is zero (no --wait-timeout set).
|
||||
// Deadline may already be in the past -- the plan's create/start/hook ops
|
||||
// aren't bound by it (see upDetached), so they can run long enough on their
|
||||
// own to exhaust the whole --wait-timeout budget before this is ever called.
|
||||
// context.WithDeadline on an already-past deadline would silently produce an
|
||||
// immediately-canceled context, failing the caller's very next daemon call
|
||||
// with a bare context.DeadlineExceeded that looks identical to "health never
|
||||
// came up" -- misleading when health was never actually checked. Returning
|
||||
// the familiar message directly here, instead, keeps that distinction clear.
|
||||
func applyRemainingDeadline(ctx context.Context, deadline time.Time, waitTimeout time.Duration) (context.Context, context.CancelFunc, error) {
|
||||
if deadline.IsZero() {
|
||||
return ctx, func() {}, nil
|
||||
}
|
||||
if time.Until(deadline) <= 0 {
|
||||
return nil, nil, fmt.Errorf("application not healthy after %s: deadline exceeded during plan execution", waitTimeout)
|
||||
}
|
||||
withDeadline, cancel := context.WithDeadline(ctx, deadline)
|
||||
return withDeadline, cancel, nil
|
||||
}
|
||||
|
||||
// upSession carries the state shared between the goroutines driving an
|
||||
// interactive `compose up` once services are created: the errgroup running
|
||||
// them, the collected errors, and the application exit status.
|
||||
|
|
|
|||
|
|
@ -297,3 +297,40 @@ func TestWithNavigationMenu(t *testing.T) {
|
|||
assert.NilError(t, WithNavigationMenu(func(bool, bool, chan<- os.Signal) NavigationMenu { return nil })(s))
|
||||
assert.Assert(t, s.navigationMenu != nil)
|
||||
}
|
||||
|
||||
// TestApplyRemainingDeadline_ZeroDeadlineIsNoOp covers upDetached's
|
||||
// --wait-timeout-unset path: no deadline, ctx passed through unchanged.
|
||||
func TestApplyRemainingDeadline_ZeroDeadlineIsNoOp(t *testing.T) {
|
||||
got, cancel, err := applyRemainingDeadline(t.Context(), time.Time{}, 0)
|
||||
defer cancel()
|
||||
assert.NilError(t, err)
|
||||
_, hasDeadline := got.Deadline()
|
||||
assert.Assert(t, !hasDeadline)
|
||||
}
|
||||
|
||||
// TestApplyRemainingDeadline_FutureDeadlineWraps covers the common case:
|
||||
// budget remains, ctx comes back bound by it.
|
||||
func TestApplyRemainingDeadline_FutureDeadlineWraps(t *testing.T) {
|
||||
deadline := time.Now().Add(time.Minute)
|
||||
got, cancel, err := applyRemainingDeadline(t.Context(), deadline, 5*time.Second)
|
||||
defer cancel()
|
||||
assert.NilError(t, err)
|
||||
gotDeadline, hasDeadline := got.Deadline()
|
||||
assert.Assert(t, hasDeadline)
|
||||
assert.Equal(t, gotDeadline, deadline)
|
||||
}
|
||||
|
||||
// TestApplyRemainingDeadline_PastDeadlineFailsImmediately is a regression
|
||||
// test for a docker-agent review finding on #14290: a plan whose
|
||||
// create/start/hook ops alone (unbounded by the wait deadline -- see
|
||||
// upDetached) take longer than --wait-timeout can still return nil past it.
|
||||
// Without this check, context.WithDeadline on an already-past deadline
|
||||
// would silently produce an immediately-canceled context, and the next
|
||||
// daemon call (getContainers) would fail with a bare context.DeadlineExceeded
|
||||
// indistinguishable from "health never came up" -- misleading, since health
|
||||
// was never actually checked. This must fail immediately with a message
|
||||
// that says so, before any daemon call happens.
|
||||
func TestApplyRemainingDeadline_PastDeadlineFailsImmediately(t *testing.T) {
|
||||
_, _, err := applyRemainingDeadline(t.Context(), time.Now().Add(-time.Second), 5*time.Second)
|
||||
assert.ErrorContains(t, err, "application not healthy after 5s: deadline exceeded during plan execution")
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue