diff --git a/commands/inspect.go b/commands/inspect.go index 72d74c9a06eb..537cccab7f5c 100644 --- a/commands/inspect.go +++ b/commands/inspect.go @@ -38,22 +38,24 @@ func runInspect(ctx context.Context, dockerCli command.Cli, in inspectOptions) e return err } - timeoutCtx, cancel := context.WithCancelCause(ctx) - if in.timeout > 0 { - timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent + imageVerifier := policy.DefaultImageVerifier(confutil.NewConfig(dockerCli)) + loadNodes := func() ([]builder.Node, error) { + timeoutCtx, cancel := context.WithCancelCause(ctx) + if in.timeout > 0 { + timeoutCtx, _ = context.WithTimeoutCause(timeoutCtx, in.timeout, errors.WithStack(context.DeadlineExceeded)) //nolint:govet // no need to manually cancel this context as we already rely on parent + } + defer func() { cancel(errors.WithStack(context.Canceled)) }() + return b.LoadNodes(timeoutCtx, builder.WithData(), builder.WithImageVerifier(imageVerifier)) } - defer func() { cancel(errors.WithStack(context.Canceled)) }() - imageVerifier := policy.DefaultImageVerifier(confutil.NewConfig(dockerCli)) - nodes, err := b.LoadNodes(timeoutCtx, builder.WithData(), builder.WithImageVerifier(imageVerifier)) + nodes, err := loadNodes() if in.bootstrap { var ok bool - ok, err = b.Boot(ctx) - if err != nil { + if ok, err = b.Boot(ctx); err != nil { return err } if ok { - nodes, err = b.LoadNodes(timeoutCtx, builder.WithData(), builder.WithImageVerifier(imageVerifier)) + nodes, err = loadNodes() } }