From 117530b0e6784be38e5f874d7ed88971fa49db9b Mon Sep 17 00:00:00 2001 From: Delweng Date: Mon, 27 Mar 2023 19:44:41 +0800 Subject: [PATCH] metrics/librato: ensure resp.body closed (#26969) This change ensures that we call Close on a http response body, in various places in the source code (mostly tests) --- graphql/graphql_test.go | 3 +++ metrics/librato/client.go | 4 +++- node/node_test.go | 1 + node/rpcstack_test.go | 1 + p2p/simulations/adapters/exec.go | 4 +++- p2p/simulations/mocker_test.go | 5 ++++- rpc/http_test.go | 1 + 7 files changed, 16 insertions(+), 3 deletions(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 46acd1529..30b4e7c35 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -156,6 +156,7 @@ func TestGraphQLBlockSerialization(t *testing.T) { t.Fatalf("could not post: %v", err) } bodyBytes, err := io.ReadAll(resp.Body) + resp.Body.Close() if err != nil { t.Fatalf("could not read from response body: %v", err) } @@ -239,6 +240,7 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) { t.Fatalf("could not post: %v", err) } bodyBytes, err := io.ReadAll(resp.Body) + resp.Body.Close() if err != nil { t.Fatalf("could not read from response body: %v", err) } @@ -263,6 +265,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } + resp.Body.Close() // make sure the request is not handled successfully assert.Equal(t, http.StatusNotFound, resp.StatusCode) } diff --git a/metrics/librato/client.go b/metrics/librato/client.go index 729c2da9a..f1b9e1e91 100644 --- a/metrics/librato/client.go +++ b/metrics/librato/client.go @@ -87,9 +87,11 @@ func (c *LibratoClient) PostMetrics(batch Batch) (err error) { req.Header.Set("Content-Type", "application/json") req.SetBasicAuth(c.Email, c.Token) - if resp, err = http.DefaultClient.Do(req); err != nil { + resp, err = http.DefaultClient.Do(req) + if err != nil { return } + defer resp.Body.Close() if resp.StatusCode != http.StatusOK { var body []byte diff --git a/node/node_test.go b/node/node_test.go index 560d487fa..04810a815 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -615,6 +615,7 @@ func doHTTPRequest(t *testing.T, req *http.Request) *http.Response { if err != nil { t.Fatalf("could not issue a GET request to the given endpoint: %v", err) } + t.Cleanup(func() { resp.Body.Close() }) return resp } diff --git a/node/rpcstack_test.go b/node/rpcstack_test.go index 4d10e61e2..0790dddec 100644 --- a/node/rpcstack_test.go +++ b/node/rpcstack_test.go @@ -320,6 +320,7 @@ func baseRpcRequest(t *testing.T, url, bodyStr string, extraHeaders ...string) * if err != nil { t.Fatal(err) } + t.Cleanup(func() { resp.Body.Close() }) return resp } diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index 7bfa8aab6..1d812514d 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -428,9 +428,11 @@ func execP2PNode() { // Send status to the host. statusJSON, _ := json.Marshal(status) - if _, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)); err != nil { + resp, err := http.Post(statusURL, "application/json", bytes.NewReader(statusJSON)) + if err != nil { log.Crit("Can't post startup info", "url", statusURL, "err", err) } + resp.Body.Close() if stackErr != nil { os.Exit(1) } diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 56d81942b..0112ee5cf 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -124,6 +124,7 @@ func TestMocker(t *testing.T) { if err != nil { t.Fatalf("Could not start mocker: %s", err) } + resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode) } @@ -145,15 +146,17 @@ func TestMocker(t *testing.T) { if err != nil { t.Fatalf("Could not stop mocker: %s", err) } + resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode) } //reset the network - _, err = http.Post(s.URL+"/reset", "", nil) + resp, err = http.Post(s.URL+"/reset", "", nil) if err != nil { t.Fatalf("Could not reset network: %s", err) } + resp.Body.Close() //now the number of nodes in the network should be zero nodesInfo, err = client.GetNodes() diff --git a/rpc/http_test.go b/rpc/http_test.go index 528e1bcfc..584842a9a 100644 --- a/rpc/http_test.go +++ b/rpc/http_test.go @@ -94,6 +94,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body if err != nil { t.Fatalf("request failed: %v", err) } + resp.Body.Close() confirmStatusCode(t, resp.StatusCode, expectedStatusCode) }