Skip to content

Commit

Permalink
Close docker client (nektos#789)
Browse files Browse the repository at this point in the history
* Close docker client!

* Fix nil dereference

* Update run_context.go

* Bump

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
  • Loading branch information
ChristopherHX and mergify[bot] authored Oct 24, 2021
1 parent f8ddfca commit 83a28d9
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 3 deletions.
1 change: 1 addition & 0 deletions pkg/container/docker_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ func NewDockerBuildExecutor(input NewDockerBuildExecutorInput) common.Executor {
if err != nil {
return err
}
defer cli.Close()

logger.Debugf("Building image from '%v'", input.ContextDir)

Expand Down
1 change: 1 addition & 0 deletions pkg/container/docker_images.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ func ImageExistsLocally(ctx context.Context, imageName string, platform string)
if err != nil {
return false, err
}
defer cli.Close()

filters := filters.NewArgs()
filters.Add("reference", imageName)
Expand Down
1 change: 1 addition & 0 deletions pkg/container/docker_pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ func NewDockerPullExecutor(input NewDockerPullExecutorInput) common.Executor {
if err != nil {
return err
}
defer cli.Close()

imagePullOptions, err := getImagePullOptions(ctx, input)
if err != nil {
Expand Down
11 changes: 11 additions & 0 deletions pkg/container/docker_run.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ type Container interface {
UpdateFromImageEnv(env *map[string]string) common.Executor
UpdateFromPath(env *map[string]string) common.Executor
Remove() common.Executor
Close() common.Executor
}

// NewContainer creates a reference to a container
Expand Down Expand Up @@ -246,6 +247,16 @@ func (cr *containerReference) connect() common.Executor {
}
}

func (cr *containerReference) Close() common.Executor {
return func(ctx context.Context) error {
if cr.cli != nil {
cr.cli.Close()
cr.cli = nil
}
return nil
}
}

func (cr *containerReference) find() common.Executor {
return func(ctx context.Context) error {
if cr.id != "" {
Expand Down
2 changes: 2 additions & 0 deletions pkg/container/docker_volume.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ func NewDockerVolumeRemoveExecutor(volume string, force bool) common.Executor {
if err != nil {
return err
}
defer cli.Close()

list, err := cli.VolumeList(ctx, filters.NewArgs())
if err != nil {
Expand Down Expand Up @@ -43,6 +44,7 @@ func removeExecutor(volume string, force bool) common.Executor {
if err != nil {
return err
}
defer cli.Close()

return cli.VolumeRemove(ctx, volume, force)
}
Expand Down
7 changes: 6 additions & 1 deletion pkg/runner/run_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,12 @@ func (rc *RunContext) Executor() common.Executor {
}
steps = append(steps, rc.stopJobContainer())

return common.NewPipelineExecutor(steps...).If(rc.isEnabled)
return common.NewPipelineExecutor(steps...).Finally(func(ctx context.Context) error {
if rc.JobContainer != nil {
return rc.JobContainer.Close()(ctx)
}
return nil
}).If(rc.isEnabled)
}

func (rc *RunContext) newStepExecutor(step *model.Step) common.Executor {
Expand Down
4 changes: 2 additions & 2 deletions pkg/runner/step_context.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ func (sc *StepContext) runUsesContainer() common.Executor {
stepContainer.Start(true),
).Finally(
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
)(ctx)
).Finally(stepContainer.Close())(ctx)
}
}

Expand Down Expand Up @@ -589,7 +589,7 @@ func (sc *StepContext) execAsDocker(ctx context.Context, action *model.Action, a
stepContainer.Start(true),
).Finally(
stepContainer.Remove().IfBool(!rc.Config.ReuseContainers),
)(ctx)
).Finally(stepContainer.Close())(ctx)
}

func (sc *StepContext) execAsComposite(ctx context.Context, step *model.Step, _ string, rc *RunContext, containerActionDir string, actionName string, _ string, action *model.Action, maybeCopyToActionDir func() error) error {
Expand Down

0 comments on commit 83a28d9

Please sign in to comment.