From f49de2f44dbaa3cd5e02d54b0e2332c4d989a512 Mon Sep 17 00:00:00 2001 From: ^_^void Date: Wed, 27 Mar 2019 20:34:04 +0800 Subject: [PATCH] ensure resp.Body closed In the old code, `resp` defined at line 215 will be rewrite at line 241. So that the `resp` after `defer` at both line 219 and 245 will operate the same one. And the other on leaks. --- clientbase/common.go | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/clientbase/common.go b/clientbase/common.go index 2a1331fc..e1ab8f93 100644 --- a/clientbase/common.go +++ b/clientbase/common.go @@ -7,6 +7,7 @@ import ( "encoding/base64" "encoding/json" "fmt" + "io" "io/ioutil" "net/http" "net/url" @@ -216,7 +217,9 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) { if err != nil { return result, err } - defer resp.Body.Close() + defer func(closer io.Closer) { + closer.Close() + }(resp.Body) if resp.StatusCode != 200 { return result, NewAPIError(resp, opts.URL) @@ -242,7 +245,9 @@ func NewAPIClient(opts *ClientOpts) (APIBaseClient, error) { if err != nil { return result, err } - defer resp.Body.Close() + defer func(closer io.Closer) { + closer.Close() + }(resp.Body) if resp.StatusCode != 200 { return result, NewAPIError(resp, schemasURLs)