Skip to content

Commit 2cf12ac

Browse files
authored
chore: close response body to prevent resource leaks (#9193)
1 parent ecbc356 commit 2cf12ac

File tree

9 files changed

+40
-6
lines changed

9 files changed

+40
-6
lines changed

.golangci.yml

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ linters:
3838
- unconvert
3939
- unused
4040
- intrange
41+
- bodyclose

contrib/jepsen/main.go

+7-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ import (
4343
"sync"
4444
"time"
4545

46+
"github.com/golang/glog"
4647
"github.com/spf13/pflag"
4748

4849
"github.com/dgraph-io/dgraph/v24/contrib/jepsen/browser"
@@ -216,7 +217,12 @@ func jepsenServe() error {
216217
// Check if the page is already up
217218
checkServing := func() error {
218219
url := jepsenURL()
219-
_, err := http.Get(url) // nolint:gosec
220+
resp, err := http.Get(url) // nolint:gosec
221+
defer func() {
222+
if err = resp.Body.Close(); err != nil {
223+
glog.Errorf("Error while closing response body: %v", err)
224+
}
225+
}()
220226
return err
221227
}
222228
if err := checkServing(); err == nil {

graphql/e2e/common/common.go

+6-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,6 @@ type GqlSchema struct {
252252
}
253253

254254
func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, error) {
255-
256255
request, err := http.NewRequest("GET", "http://"+authority+"/probe/graphql", nil)
257256
if err != nil {
258257
return nil, err
@@ -263,6 +262,11 @@ func probeGraphQL(authority string, header http.Header) (*ProbeGraphQLResp, erro
263262
if err != nil {
264263
return nil, err
265264
}
265+
defer func() {
266+
if err = resp.Body.Close(); err != nil {
267+
glog.Errorf("Error while closing response body: %v", err)
268+
}
269+
}()
266270

267271
probeResp := ProbeGraphQLResp{}
268272
if resp.StatusCode == http.StatusOK {
@@ -592,6 +596,7 @@ func safelyDropAll(t *testing.T, withGroot bool) {
592596
func updateGQLSchemaUsingAdminSchemaEndpt(t *testing.T, authority, schema string) string {
593597
resp, err := http.Post("http://"+authority+"/admin/schema", "", strings.NewReader(schema))
594598
require.NoError(t, err)
599+
defer func() { require.NoError(t, resp.Body.Close()) }()
595600

596601
b, err := io.ReadAll(resp.Body)
597602
require.NoError(t, err)

graphql/e2e/common/query.go

+2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ func touchedUidsHeader(t *testing.T) {
8888
client := http.Client{Timeout: 10 * time.Second}
8989
resp, err := client.Do(req)
9090
require.NoError(t, err)
91+
defer func() { require.NoError(t, resp.Body.Close()) }()
9192

9293
// confirm that the header value is a non-negative integer
9394
touchedUidsInHeader, err := strconv.ParseUint(resp.Header.Get("Graphql-TouchedUids"), 10, 64)
@@ -116,6 +117,7 @@ func cacheControlHeader(t *testing.T) {
116117
client := http.Client{Timeout: 10 * time.Second}
117118
resp, err := client.Do(req)
118119
require.NoError(t, err)
120+
defer func() { require.NoError(t, resp.Body.Close()) }()
119121

120122
// confirm that the header value is a non-negative integer
121123
require.Equal(t, "public,max-age=5", resp.Header.Get("Cache-Control"))

graphql/e2e/common/subscription.go

+7-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"math/rand"
2424
"net/http"
2525

26+
"github.com/golang/glog"
2627
"github.com/gorilla/websocket"
2728

2829
"github.com/dgraph-io/dgraph/v24/graphql/schema"
@@ -68,11 +69,15 @@ func NewGraphQLSubscription(url string, req *schema.Request, subscriptionPayload
6869

6970
dialer := websocket.DefaultDialer
7071
dialer.EnableCompression = true
71-
conn, _, err := dialer.Dial(url, header)
72+
conn, resp, err := dialer.Dial(url, header)
7273
if err != nil {
7374
return nil, err
7475
}
75-
76+
defer func() {
77+
if err = resp.Body.Close(); err != nil {
78+
glog.Errorf("Error while closing response body: %v", err)
79+
}
80+
}()
7681
// Initialize subscription.
7782
init := operationMessage{
7883
Type: initMsg,

graphql/resolve/webhook.go

+7
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,13 @@ func sendWebhookEvent(ctx context.Context, m schema.Mutation, commitTs uint64, r
124124
if err != nil {
125125
glog.V(3).Info(errors.Wrap(err, "unable to send webhook event"))
126126
}
127+
128+
defer func() {
129+
if err = resp.Body.Close(); err != nil {
130+
glog.Errorf("Error while closing response body: %v", err)
131+
}
132+
}()
133+
127134
if resp != nil && (resp.StatusCode < 200 || resp.StatusCode >= 300) {
128135
glog.V(3).Info(errors.Errorf("got unsuccessful status from webhook: %s", resp.Status))
129136
}

systest/backup/common/utils.go

+4-2
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ func AddItem(t *testing.T, minSuffixVal int, maxSuffixVal int, jwtToken string,
154154
client := &http.Client{}
155155
resp, err := client.Do(req)
156156
require.NoError(t, err)
157-
158157
var data interface{}
159158
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
159+
require.NoError(t, resp.Body.Close())
160160
}
161161
}
162162

@@ -189,8 +189,8 @@ func CheckItemExists(t *testing.T, desriedSuffix int, jwtToken string, whichAlph
189189
}
190190
client := &http.Client{}
191191
resp, err := client.Do(req)
192-
193192
require.NoError(t, err)
193+
defer func() { require.NoError(t, resp.Body.Close()) }()
194194

195195
var data interface{}
196196
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
@@ -229,6 +229,7 @@ func TakeBackup(t *testing.T, jwtToken string, backupDst string, whichAlpha stri
229229
client := &http.Client{}
230230
resp, err := client.Do(req)
231231
require.NoError(t, err)
232+
defer func() { require.NoError(t, resp.Body.Close()) }()
232233

233234
var data interface{}
234235
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))
@@ -265,6 +266,7 @@ func RunRestore(t *testing.T, jwtToken string, restoreLocation string, whichAlph
265266
client := &http.Client{}
266267
resp, err := client.Do(req)
267268
require.NoError(t, err)
269+
defer func() { require.NoError(t, resp.Body.Close()) }()
268270

269271
var data interface{}
270272
require.NoError(t, json.NewDecoder(resp.Body).Decode(&data))

systest/bulk_live/common/bulk_live_cases.go

+5
Original file line numberDiff line numberDiff line change
@@ -798,6 +798,11 @@ func matchExportCount(opts matchExport) error {
798798
if err != nil {
799799
return err
800800
}
801+
defer func() {
802+
if err = resp.Body.Close(); err != nil {
803+
glog.Errorf("Error while closing response body: %v", err)
804+
}
805+
}()
801806

802807
b, err = io.ReadAll(resp.Body)
803808
if err != nil {

testutil/backup.go

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ func WaitForRestore(t *testing.T, dg *dgo.Dgraph, HttpSocket string) {
7070
resp, err := http.Get("http://" + HttpSocket + "/health")
7171
require.NoError(t, err)
7272
buf, err := io.ReadAll(resp.Body)
73+
require.NoError(t, resp.Body.Close())
7374
require.NoError(t, err)
7475
sbuf := string(buf)
7576
if !strings.Contains(sbuf, "opRestore") {

0 commit comments

Comments
 (0)