From 0764d95a03df6801f38e2b625f9b69b5313d1c73 Mon Sep 17 00:00:00 2001 From: oilbeater Date: Wed, 7 Feb 2018 18:02:36 +0800 Subject: [PATCH] defer in loop may cause resource leak defer statement executes only when function return, and the resource still be hold during loop. Release the resource manually when not needed. --- pkg/plugin/installer/http_installer.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/pkg/plugin/installer/http_installer.go b/pkg/plugin/installer/http_installer.go index 203f038f2..91d497651 100644 --- a/pkg/plugin/installer/http_installer.go +++ b/pkg/plugin/installer/http_installer.go @@ -193,10 +193,11 @@ func (g *TarGzExtractor) Extract(buffer *bytes.Buffer, targetDir string) error { if err != nil { return err } - defer outFile.Close() if _, err := io.Copy(outFile, tarReader); err != nil { + outFile.Close() return err } + outFile.Close() default: return fmt.Errorf("unknown type: %b in %s", header.Typeflag, header.Name) }