diff --git a/.golangci.yml b/.golangci.yml index 93c669b15cc..a387ca1cd43 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -38,3 +38,4 @@ linters: - unconvert - unused - intrange + - bodyclose diff --git a/contrib/jepsen/main.go b/contrib/jepsen/main.go index 1f1216b34e9..e14755b45ec 100644 --- a/contrib/jepsen/main.go +++ b/contrib/jepsen/main.go @@ -32,6 +32,7 @@ import ( "context" "errors" "fmt" + "github.com/golang/glog" "io" "log" "net/http" @@ -216,7 +217,13 @@ func jepsenServe() error { // Check if the page is already up checkServing := func() error { url := jepsenURL() - _, err := http.Get(url) // nolint:gosec + resp, err := http.Get(url) // nolint:gosec + defer func() { + err = resp.Body.Close() + if err != nil { + glog.Errorf("Error while closing response body: %s", err) + } + }() return err } if err := checkServing(); err == nil { diff --git a/graphql/e2e/common/common.go b/graphql/e2e/common/common.go index 51f0fa331b3..0b83f9cc882 100644 --- a/graphql/e2e/common/common.go +++ b/graphql/e2e/common/common.go @@ -252,7 +252,6 @@ type GqlSchema struct { } func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, error) { - request, err := http.NewRequest("GET", "http://"+authority+"/probe/graphql", nil) if err != nil { return nil, err @@ -263,6 +262,12 @@ func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, erro if err != nil { return nil, err } + defer func() { + err = resp.Body.Close() + if err != nil { + glog.Errorf("Error while closing response body: %s", err) + } + }() probeResp := ProbeGraphQLResp{} if resp.StatusCode == http.StatusOK { @@ -592,6 +597,7 @@ func safelyDropAll(t *testing.T, withGroot bool) { func updateGQLSchemaUsingAdminSchemaEndpt(t *testing.T, authority, schema string) string { resp, err := http.Post("http://"+authority+"/admin/schema", "", strings.NewReader(schema)) require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() b, err := io.ReadAll(resp.Body) require.NoError(t, err) diff --git a/graphql/e2e/common/query.go b/graphql/e2e/common/query.go index 4dd8094e800..3e494fb100f 100644 --- a/graphql/e2e/common/query.go +++ b/graphql/e2e/common/query.go @@ -88,6 +88,7 @@ func touchedUidsHeader(t *testing.T) { client := http.Client{Timeout: 10 * time.Second} resp, err := client.Do(req) require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() // confirm that the header value is a non-negative integer touchedUidsInHeader, err := strconv.ParseUint(resp.Header.Get("Graphql-TouchedUids"), 10, 64) @@ -116,6 +117,7 @@ func cacheControlHeader(t *testing.T) { client := http.Client{Timeout: 10 * time.Second} resp, err := client.Do(req) require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() // confirm that the header value is a non-negative integer require.Equal(t, "public,max-age=5", resp.Header.Get("Cache-Control")) diff --git a/graphql/e2e/common/subscription.go b/graphql/e2e/common/subscription.go index a2f0a4cfd0d..b7883e45c25 100644 --- a/graphql/e2e/common/subscription.go +++ b/graphql/e2e/common/subscription.go @@ -20,6 +20,7 @@ import ( "encoding/json" "errors" "fmt" + "github.com/golang/glog" "math/rand" "net/http" @@ -68,11 +69,16 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload dialer := websocket.DefaultDialer dialer.EnableCompression = true - conn, _, err := dialer.Dial(url, header) + conn, resp, err := dialer.Dial(url, header) if err != nil { return nil, err } - + defer func() { + err = resp.Body.Close() + if err != nil { + glog.Errorf("Error while closing response body: %s", err) + } + }() // Initialize subscription. init := operationMessage{ Type: initMsg, diff --git a/graphql/resolve/webhook.go b/graphql/resolve/webhook.go index 2f74a194a7f..f8aeb121acd 100644 --- a/graphql/resolve/webhook.go +++ b/graphql/resolve/webhook.go @@ -124,6 +124,14 @@ func sendWebhookEvent(ctx context.Context, m schema.Mutation, commitTs uint64, r if err != nil { glog.V(3).Info(errors.Wrap(err, "unable to send webhook event")) } + + defer func() { + err = resp.Body.Close() + if err != nil { + glog.Errorf("Error while closing response body: %s", err) + } + }() + if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) { glog.V(3).Info(errors.Errorf("got unsuccessful status from webhook: %s", resp.Status)) } diff --git a/systest/backup/common/utils.go b/systest/backup/common/utils.go index 69fb6186823..1144d203f16 100644 --- a/systest/backup/common/utils.go +++ b/systest/backup/common/utils.go @@ -154,9 +154,9 @@ func AddItem(t *testing.T, minSuffixVal int, maxSuffixVal int, jwtToken string, client := &http.Client{} resp, err := client.Do(req) require.NoError(t, err) - var data interface{} require.NoError(t, json.NewDecoder(resp.Body).Decode(&data)) + require.NoError(t, resp.Body.Close()) } } @@ -189,8 +189,8 @@ func CheckItemExists(t *testing.T, desriedSuffix int, jwtToken string, whichAlph } client := &http.Client{} resp, err := client.Do(req) - require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() var data interface{} require.NoError(t, json.NewDecoder(resp.Body).Decode(&data)) @@ -229,6 +229,7 @@ func TakeBackup(t *testing.T, jwtToken string, backupDst string, whichAlpha stri client := &http.Client{} resp, err := client.Do(req) require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() var data interface{} require.NoError(t, json.NewDecoder(resp.Body).Decode(&data)) @@ -265,6 +266,7 @@ func RunRestore(t *testing.T, jwtToken string, restoreLocation string, whichAlph client := &http.Client{} resp, err := client.Do(req) require.NoError(t, err) + defer func() { require.NoError(t, resp.Body.Close()) }() var data interface{} require.NoError(t, json.NewDecoder(resp.Body).Decode(&data)) diff --git a/systest/bulk_live/common/bulk_live_cases.go b/systest/bulk_live/common/bulk_live_cases.go index dc80eae99cb..580e33fd589 100644 --- a/systest/bulk_live/common/bulk_live_cases.go +++ b/systest/bulk_live/common/bulk_live_cases.go @@ -798,6 +798,12 @@ func matchExportCount(opts matchExport) error { if err != nil { return err } + defer func() { + err = resp.Body.Close() + if err != nil { + glog.Errorf("Error while closing response body: %s", err) + } + }() b, err = io.ReadAll(resp.Body) if err != nil { diff --git a/testutil/backup.go b/testutil/backup.go index 47f5d612390..47bfc5d4a88 100644 --- a/testutil/backup.go +++ b/testutil/backup.go @@ -70,6 +70,7 @@ func WaitForRestore(t *testing.T, dg *dgo.Dgraph, HttpSocket string) { resp, err := http.Get("http://" + HttpSocket + "/health") require.NoError(t, err) buf, err := io.ReadAll(resp.Body) + require.NoError(t, resp.Body.Close()) require.NoError(t, err) sbuf := string(buf) if !strings.Contains(sbuf, "opRestore") {