Skip to content

Commit

Permalink
Merge pull request moby#46687 from thaJeztah/more_nocancel
Browse files Browse the repository at this point in the history
daemon: use context.WithoutCancel in more places
  • Loading branch information
thaJeztah authored Oct 20, 2023
2 parents f800215 + aad51c0 commit 164167e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
7 changes: 5 additions & 2 deletions daemon/daemon.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ import (
"github.com/docker/docker/dockerversion"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/image"
"github.com/docker/docker/internal/compatcontext"
"github.com/docker/docker/layer"
libcontainerdtypes "github.com/docker/docker/libcontainerd/types"
"github.com/docker/docker/libnetwork"
Expand Down Expand Up @@ -1212,14 +1213,16 @@ func (daemon *Daemon) waitForStartupDone() {
}

func (daemon *Daemon) shutdownContainer(c *container.Container) error {
ctx := compatcontext.WithoutCancel(context.TODO())

// If container failed to exit in stopTimeout seconds of SIGTERM, then using the force
if err := daemon.containerStop(context.TODO(), c, containertypes.StopOptions{}); err != nil {
if err := daemon.containerStop(ctx, c, containertypes.StopOptions{}); err != nil {
return fmt.Errorf("Failed to stop container %s with error: %v", c.ID, err)
}

// Wait without timeout for the container to exit.
// Ignore the result.
<-c.Wait(context.Background(), container.WaitConditionNotRunning)
<-c.Wait(ctx, container.WaitConditionNotRunning)
return nil
}

Expand Down
14 changes: 7 additions & 7 deletions daemon/stop.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/docker/docker/api/types/events"
"github.com/docker/docker/container"
"github.com/docker/docker/errdefs"
"github.com/docker/docker/internal/compatcontext"
"github.com/moby/sys/signal"
"github.com/pkg/errors"
)
Expand Down Expand Up @@ -41,13 +42,12 @@ func (daemon *Daemon) ContainerStop(ctx context.Context, name string, options co
return nil
}

// containerStop sends a stop signal, waits, sends a kill signal.
func (daemon *Daemon) containerStop(_ context.Context, ctr *container.Container, options containertypes.StopOptions) (retErr error) {
// Deliberately using a local context here, because cancelling the
// request should not cancel the stop.
//
// TODO(thaJeztah): pass context, and use context.WithoutCancel() once available: https://github.com/golang/go/issues/40221
ctx := context.Background()
// containerStop sends a stop signal, waits, sends a kill signal. It uses
// a [context.WithoutCancel], so cancelling the context does not cancel
// the request to stop the container.
func (daemon *Daemon) containerStop(ctx context.Context, ctr *container.Container, options containertypes.StopOptions) (retErr error) {
// Cancelling the request should not cancel the stop.
ctx = compatcontext.WithoutCancel(ctx)

if !ctr.IsRunning() {
return nil
Expand Down

0 comments on commit 164167e

Please sign in to comment.