Skip to content
This repository was archived by the owner on Feb 12, 2025. It is now read-only.

Commit 40e67ab

Browse files
authored
Check for nil response before dereferencing it. (#217)
If we didn't receive a response in the retry loop don't dereference it and just count it against the retry cap.
1 parent 4f2543e commit 40e67ab

File tree

3 files changed

+25
-1
lines changed

3 files changed

+25
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## v9.5.2
4+
5+
### Bug Fixes
6+
7+
- Check for nil *http.Response before dereferencing it.
8+
39
## v9.5.1
410

511
### Bug Fixes

autorest/sender.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ func DoRetryForStatusCodes(attempts int, backoff time.Duration, codes ...int) Se
232232
}
233233
// don't count a 429 against the number of attempts
234234
// so that we continue to retry until it succeeds
235-
if resp.StatusCode != http.StatusTooManyRequests {
235+
if resp == nil || resp.StatusCode != http.StatusTooManyRequests {
236236
attempt++
237237
}
238238
}

autorest/sender_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,3 +809,21 @@ func TestDelayWithRetryAfterWithSuccess(t *testing.T) {
809809
r.Status, client.Attempts()-1)
810810
}
811811
}
812+
813+
func TestDoRetryForStatusCodes_NilResponse(t *testing.T) {
814+
client := mocks.NewSender()
815+
client.AppendResponse(nil)
816+
client.SetError(fmt.Errorf("faux error"))
817+
818+
r, err := SendWithSender(client, mocks.NewRequest(),
819+
DoRetryForStatusCodes(3, time.Duration(1*time.Second), StatusCodesForRetry...),
820+
)
821+
822+
Respond(r,
823+
ByDiscardingBody(),
824+
ByClosing())
825+
826+
if err != nil || client.Attempts() != 2 {
827+
t.Fatalf("autorest: Sender#TestDoRetryForStatusCodes_NilResponse -- Got: non-nil error or wrong number of attempts - %v", err)
828+
}
829+
}

0 commit comments

Comments
 (0)