From fd13ea22d0fc217d5630ae423c8b5a907094bc06 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Thu, 23 Mar 2023 22:17:04 +0800 Subject: [PATCH 01/16] metrics/librato: ensure resp.body closed Signed-off-by: jsvisa --- metrics/librato/client.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/metrics/librato/client.go b/metrics/librato/client.go index 729c2da9a9bc..f1b9e1e91669 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 From ffed3463a62b65d6217f4aa60142e6671bb559a8 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 19:58:51 +0800 Subject: [PATCH 02/16] rpc: ensure resp.body.close Signed-off-by: jsvisa --- rpc/http_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/rpc/http_test.go b/rpc/http_test.go index 528e1bcfc5e7..d89e8daa599a 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) } + defer resp.Body.Close() confirmStatusCode(t, resp.StatusCode, expectedStatusCode) } From ea1c869e081f9ae7101e7be9529c873f1aacdf06 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 19:59:09 +0800 Subject: [PATCH 03/16] node: ensure rpc.body.close Signed-off-by: jsvisa --- node/rpcstack_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/node/rpcstack_test.go b/node/rpcstack_test.go index 4d10e61e2dec..0790dddec411 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 } From 92e8f620964a2125dc6465132b761cd2f739eebc Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:03:03 +0800 Subject: [PATCH 04/16] rpc: read body before close Signed-off-by: jsvisa --- rpc/http_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/rpc/http_test.go b/rpc/http_test.go index d89e8daa599a..20a9aa421246 100644 --- a/rpc/http_test.go +++ b/rpc/http_test.go @@ -19,6 +19,8 @@ package rpc import ( "context" "fmt" + "io" + "io/ioutil" "net/http" "net/http/httptest" "strings" @@ -95,6 +97,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body t.Fatalf("request failed: %v", err) } defer resp.Body.Close() + io.Copy(ioutil.Discard, resp.Body) confirmStatusCode(t, resp.StatusCode, expectedStatusCode) } From 0a576eda025ca603c4f91bb2731b17f41afbb4b8 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:03:14 +0800 Subject: [PATCH 05/16] graphql: close resp.body Signed-off-by: jsvisa --- graphql/graphql_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 46acd1529342..6007261c1ba1 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -21,6 +21,7 @@ import ( "encoding/json" "fmt" "io" + "io/ioutil" "math/big" "net/http" "strings" @@ -155,6 +156,7 @@ func TestGraphQLBlockSerialization(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } + defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("could not read from response body: %v", err) @@ -238,6 +240,7 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } + defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) if err != nil { t.Fatalf("could not read from response body: %v", err) @@ -263,6 +266,8 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } + defer resp.Body.Close() + io.Copy(ioutil.Discard, resp.Body) // make sure the request is not handled successfully assert.Equal(t, http.StatusNotFound, resp.StatusCode) } From 651e69b80dfc09bb7ad3a9b5e005e4c63f47f015 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:07:18 +0800 Subject: [PATCH 06/16] rpc,graphql: ioutil.Discard -> io.Discard Signed-off-by: jsvisa --- graphql/graphql_test.go | 3 +-- rpc/http_test.go | 3 +-- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 6007261c1ba1..fc320f3739cb 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -21,7 +21,6 @@ import ( "encoding/json" "fmt" "io" - "io/ioutil" "math/big" "net/http" "strings" @@ -267,7 +266,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { t.Fatalf("could not post: %v", err) } defer resp.Body.Close() - io.Copy(ioutil.Discard, resp.Body) + io.Copy(io.Discard, resp.Body) // make sure the request is not handled successfully assert.Equal(t, http.StatusNotFound, resp.StatusCode) } diff --git a/rpc/http_test.go b/rpc/http_test.go index 20a9aa421246..ac999a01e09f 100644 --- a/rpc/http_test.go +++ b/rpc/http_test.go @@ -20,7 +20,6 @@ import ( "context" "fmt" "io" - "io/ioutil" "net/http" "net/http/httptest" "strings" @@ -97,7 +96,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body t.Fatalf("request failed: %v", err) } defer resp.Body.Close() - io.Copy(ioutil.Discard, resp.Body) + io.Copy(io.Discard, resp.Body) confirmStatusCode(t, resp.StatusCode, expectedStatusCode) } From b076048763ba296617143cb1d15106c64f2222b8 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:07:37 +0800 Subject: [PATCH 07/16] p2p/simulation: ensure resp.body.close Signed-off-by: jsvisa --- p2p/simulations/adapters/exec.go | 5 ++++- p2p/simulations/mocker_test.go | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index 7bfa8aab6d10..68ad3369d652 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -428,9 +428,12 @@ 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) } + defer resp.Body.Close() + io.Copy(io.Discard, resp.Body) if stackErr != nil { os.Exit(1) } diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 56d81942bbd0..80ada79c174c 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) } + defer 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) } + defer 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) } + defer resp.Body.Close() //now the number of nodes in the network should be zero nodesInfo, err = client.GetNodes() From 5a18dc59942de87dce157d373abde12e36c751fb Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:31:23 +0800 Subject: [PATCH 08/16] node: register resp.body.close Signed-off-by: jsvisa --- node/node_test.go | 1 + 1 file changed, 1 insertion(+) diff --git a/node/node_test.go b/node/node_test.go index 560d487fa823..04810a815bf6 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 } From 0fcf17b53fc6b6ee7f69390b5520f90e321eeb37 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 20:53:15 +0800 Subject: [PATCH 09/16] Update p2p/simulations/adapters/exec.go Co-authored-by: Martin Holst Swende --- p2p/simulations/adapters/exec.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/p2p/simulations/adapters/exec.go b/p2p/simulations/adapters/exec.go index 68ad3369d652..1d812514dee7 100644 --- a/p2p/simulations/adapters/exec.go +++ b/p2p/simulations/adapters/exec.go @@ -432,8 +432,7 @@ func execP2PNode() { if err != nil { log.Crit("Can't post startup info", "url", statusURL, "err", err) } - defer resp.Body.Close() - io.Copy(io.Discard, resp.Body) + resp.Body.Close() if stackErr != nil { os.Exit(1) } From 94b997cee92fccd8fd1bc0dc7e45c94bba672e71 Mon Sep 17 00:00:00 2001 From: jsvisa Date: Fri, 24 Mar 2023 20:54:37 +0800 Subject: [PATCH 10/16] rpc: no need to read body Signed-off-by: jsvisa --- rpc/http_test.go | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/rpc/http_test.go b/rpc/http_test.go index ac999a01e09f..584842a9aa8d 100644 --- a/rpc/http_test.go +++ b/rpc/http_test.go @@ -19,7 +19,6 @@ package rpc import ( "context" "fmt" - "io" "net/http" "net/http/httptest" "strings" @@ -95,8 +94,7 @@ func confirmHTTPRequestYieldsStatusCode(t *testing.T, method, contentType, body if err != nil { t.Fatalf("request failed: %v", err) } - defer resp.Body.Close() - io.Copy(io.Discard, resp.Body) + resp.Body.Close() confirmStatusCode(t, resp.StatusCode, expectedStatusCode) } From 2d12d25ab89bd3e632c6fd1a1e9e27a407fecf24 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:12:26 +0800 Subject: [PATCH 11/16] Update graphql/graphql_test.go Co-authored-by: Martin Holst Swende --- graphql/graphql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index fc320f3739cb..c08d4035b7f1 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -155,8 +155,8 @@ func TestGraphQLBlockSerialization(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } - defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) + resp.Body.Close() if err != nil { t.Fatalf("could not read from response body: %v", err) } From 84c150605eb4cc26e161d31f0a8e2f437b029d10 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:12:48 +0800 Subject: [PATCH 12/16] Update graphql/graphql_test.go Co-authored-by: Martin Holst Swende --- graphql/graphql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index c08d4035b7f1..08685d4de2e9 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -239,8 +239,8 @@ func TestGraphQLBlockSerializationEIP2718(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } - defer resp.Body.Close() bodyBytes, err := io.ReadAll(resp.Body) + resp.Body.Close() if err != nil { t.Fatalf("could not read from response body: %v", err) } From a3bb37b9c505f9157d937bb79cd793084e6c225c Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:13:08 +0800 Subject: [PATCH 13/16] Update graphql/graphql_test.go Co-authored-by: Martin Holst Swende --- graphql/graphql_test.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/graphql/graphql_test.go b/graphql/graphql_test.go index 08685d4de2e9..30b4e7c3547b 100644 --- a/graphql/graphql_test.go +++ b/graphql/graphql_test.go @@ -265,8 +265,7 @@ func TestGraphQLHTTPOnSamePort_GQLRequest_Unsuccessful(t *testing.T) { if err != nil { t.Fatalf("could not post: %v", err) } - defer resp.Body.Close() - io.Copy(io.Discard, resp.Body) + resp.Body.Close() // make sure the request is not handled successfully assert.Equal(t, http.StatusNotFound, resp.StatusCode) } From 548835af5c62acad24b8033a5eba077e57d55b82 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:13:19 +0800 Subject: [PATCH 14/16] Update p2p/simulations/mocker_test.go Co-authored-by: Martin Holst Swende --- p2p/simulations/mocker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 80ada79c174c..538f181501c2 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -124,7 +124,7 @@ func TestMocker(t *testing.T) { if err != nil { t.Fatalf("Could not start mocker: %s", err) } - defer resp.Body.Close() + resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("Invalid Status Code received for starting mocker, expected 200, got %d", resp.StatusCode) } From b4ac27c20248c3102b84689ee1ab3aad9c08c734 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:13:47 +0800 Subject: [PATCH 15/16] Update p2p/simulations/mocker_test.go Co-authored-by: Martin Holst Swende --- p2p/simulations/mocker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 538f181501c2..42ecd7b30cf4 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -146,7 +146,7 @@ func TestMocker(t *testing.T) { if err != nil { t.Fatalf("Could not stop mocker: %s", err) } - defer resp.Body.Close() + resp.Body.Close() if resp.StatusCode != 200 { t.Fatalf("Invalid Status Code received for stopping mocker, expected 200, got %d", resp.StatusCode) } From 34959e3caf7087677c9227d0634ca6fd5d0bcdb7 Mon Sep 17 00:00:00 2001 From: Delweng Date: Fri, 24 Mar 2023 22:13:58 +0800 Subject: [PATCH 16/16] Update p2p/simulations/mocker_test.go Co-authored-by: Martin Holst Swende --- p2p/simulations/mocker_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/p2p/simulations/mocker_test.go b/p2p/simulations/mocker_test.go index 42ecd7b30cf4..0112ee5cfd6e 100644 --- a/p2p/simulations/mocker_test.go +++ b/p2p/simulations/mocker_test.go @@ -156,7 +156,7 @@ func TestMocker(t *testing.T) { if err != nil { t.Fatalf("Could not reset network: %s", err) } - defer resp.Body.Close() + resp.Body.Close() //now the number of nodes in the network should be zero nodesInfo, err = client.GetNodes()