Skip to content

Commit 6d31d40

Browse files
authored
fix: close response body immediately in retry loop (#567)
ObtainClientCredentialsToken: replace defer resp.Body.Close() inside the retry loop with immediate close after reading the response. The deferred closures accumulated on each iteration and bodies were not released until the function returned.
1 parent 1927d1e commit 6d31d40

File tree

1 file changed

+2
-1
lines changed

1 file changed

+2
-1
lines changed

e2e/helpers/auth.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -566,21 +566,22 @@ func (p *OIDCTokenProvider) ObtainClientCredentialsToken(t *testing.T, ctx conte
566566

567567
resp, err := httpClient.Do(req)
568568
if err == nil {
569-
defer func() { _ = resp.Body.Close() }()
570569
if resp.StatusCode == http.StatusOK {
571570
var tokenResp struct {
572571
AccessToken string `json:"access_token"`
573572
TokenType string `json:"token_type"`
574573
ExpiresIn int `json:"expires_in"`
575574
}
576575
err = json.NewDecoder(resp.Body).Decode(&tokenResp)
576+
_ = resp.Body.Close()
577577
require.NoError(t, err, "Failed to decode client_credentials token response")
578578
require.NotEmpty(t, tokenResp.AccessToken, "No access_token in client_credentials response")
579579
if attempt > 1 {
580580
t.Logf("client_credentials token acquired on attempt %d/%d", attempt, maxAttempts)
581581
}
582582
return tokenResp.AccessToken
583583
}
584+
_ = resp.Body.Close()
584585
lastErr = fmt.Errorf("client_credentials token request failed with status %d", resp.StatusCode)
585586
} else {
586587
lastErr = fmt.Errorf("client_credentials token request failed: %w", err)

0 commit comments

Comments
 (0)