Skip to content

Commit 7e7ed58

Browse files
europauleriknordmark
authored andcommitted
fix(vcomlink, zedkube): close response body ASAP
When doing HTTP requests in loops it's a good practice to close the response body explicitly after reading it, instead of deferring it until the surrounding function returns. This helps to free up resources sooner and avoid potential resource leaks, especially in bigger loops. Signed-off-by: Paul Gaiduk <[email protected]> (cherry picked from commit 4d88c7a)
1 parent acafdf8 commit 7e7ed58

File tree

2 files changed

+2
-2
lines changed

2 files changed

+2
-2
lines changed

pkg/pillar/cmd/vcomlink/vcomlink_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,13 +306,13 @@ func TestValidGetEkCert(t *testing.T) {
306306
if err != nil {
307307
t.Fatalf("failed to download issuing CA cert: %v", err)
308308
}
309-
defer resp.Body.Close()
310309

311310
if resp.StatusCode != http.StatusOK {
312311
t.Fatalf("failed to download issuing CA cert, status code: %d", resp.StatusCode)
313312
}
314313

315314
caCertData, err := io.ReadAll(resp.Body)
315+
resp.Body.Close()
316316
if err != nil {
317317
t.Fatalf("failed to read issuing CA cert response body: %v", err)
318318
}

pkg/pillar/cmd/zedkube/clusterstatus.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,14 +412,14 @@ func (z *zedkube) clusterAppIDHandler(w http.ResponseWriter, r *http.Request) {
412412
}
413413
continue
414414
}
415-
defer resp.Body.Close()
416415

417416
if resp.StatusCode != http.StatusOK {
418417
log.Errorf("clusterAppIDHandler: received non-OK status %d from %s", resp.StatusCode, host)
419418
continue
420419
}
421420

422421
remoteAppInfoJSON, err := ioutil.ReadAll(resp.Body)
422+
resp.Body.Close()
423423
if err != nil {
424424
log.Errorf("clusterAppIDHandler: error reading response from %s: %v", host, err)
425425
continue

0 commit comments

Comments
 (0)