From 719c92bb9c243226860ecbdab4933a51cf74da31 Mon Sep 17 00:00:00 2001 From: Bastian Date: Tue, 29 Sep 2020 12:44:44 +0200 Subject: [PATCH] Close open connections and files (#135) Ensure that the client and file is closed either in error cases or once cleanup() is called so no file descriptors are leaked. --- image/daemon/image.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/image/daemon/image.go b/image/daemon/image.go index 514293ec3..8fdb20448 100644 --- a/image/daemon/image.go +++ b/image/daemon/image.go @@ -38,6 +38,11 @@ func Image(ref name.Reference) (v1.Image, func(), error) { if err != nil { return nil, cleanup, xerrors.Errorf("failed to initialize a docker client: %w", err) } + defer func() { + if err != nil { + c.Close() + } + }() inspect, _, err := c.ImageInspectWithRaw(context.Background(), ref.Name()) if err != nil { @@ -50,6 +55,8 @@ func Image(ref name.Reference) (v1.Image, func(), error) { } cleanup = func() { + c.Close() + f.Close() _ = os.Remove(f.Name()) }