Skip to content

Commit

Permalink
chore: close response body to prevent resource leaks (#9193)
Browse files Browse the repository at this point in the history
  • Loading branch information
xqqp authored Oct 14, 2024
1 parent ecbc356 commit 2cf12ac
Show file tree
Hide file tree
Showing 9 changed files with 40 additions and 6 deletions.
1 change: 1 addition & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ linters:
- unconvert
- unused
- intrange
- bodyclose
8 changes: 7 additions & 1 deletion contrib/jepsen/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ import (
"sync"
"time"

"github.com/golang/glog"
"github.com/spf13/pflag"

"github.com/dgraph-io/dgraph/v24/contrib/jepsen/browser"
Expand Down Expand Up @@ -216,7 +217,12 @@ 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() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()
return err
}
if err := checkServing(); err == nil {
Expand Down
7 changes: 6 additions & 1 deletion graphql/e2e/common/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -263,6 +262,11 @@ func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, erro
if err != nil {
return nil, err
}
defer func() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()

probeResp := ProbeGraphQLResp{}
if resp.StatusCode == http.StatusOK {
Expand Down Expand Up @@ -592,6 +596,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)
Expand Down
2 changes: 2 additions & 0 deletions graphql/e2e/common/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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"))
Expand Down
9 changes: 7 additions & 2 deletions graphql/e2e/common/subscription.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"math/rand"
"net/http"

"github.com/golang/glog"
"github.com/gorilla/websocket"

"github.com/dgraph-io/dgraph/v24/graphql/schema"
Expand Down Expand Up @@ -68,11 +69,15 @@ 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() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()
// Initialize subscription.
init := operationMessage{
Type: initMsg,
Expand Down
7 changes: 7 additions & 0 deletions graphql/resolve/webhook.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,13 @@ 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() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()

if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
glog.V(3).Info(errors.Errorf("got unsuccessful status from webhook: %s", resp.Status))
}
Expand Down
6 changes: 4 additions & 2 deletions systest/backup/common/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}
}

Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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))
Expand Down
5 changes: 5 additions & 0 deletions systest/bulk_live/common/bulk_live_cases.go
Original file line number Diff line number Diff line change
Expand Up @@ -798,6 +798,11 @@ func matchExportCount(opts matchExport) error {
if err != nil {
return err
}
defer func() {
if err = resp.Body.Close(); err != nil {
glog.Errorf("Error while closing response body: %v", err)
}
}()

b, err = io.ReadAll(resp.Body)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions testutil/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -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") {
Expand Down

0 comments on commit 2cf12ac

Please sign in to comment.