Skip to content

Commit 54fab10

Browse files
authored
fix(storage): retry net.OpError on connection reset (#10154)
We are seeing these errors surfaced via net.OpError as well as url.Error. Update the ShouldRetry function accordingly. Also, use net.ErrClosed sentinel over string matching. Fixes #9478
1 parent 7c52978 commit 54fab10

File tree

2 files changed

+11
-9
lines changed

2 files changed

+11
-9
lines changed

‎storage/invoke.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,18 +105,16 @@ func ShouldRetry(err error) bool {
105105
if errors.Is(err, io.ErrUnexpectedEOF) {
106106
return true
107107
}
108+
if errors.Is(err, net.ErrClosed) {
109+
return true
110+
}
108111

109112
switch e := err.(type) {
110-
case *net.OpError:
111-
if strings.Contains(e.Error(), "use of closed network connection") {
112-
// TODO: check against net.ErrClosed (go 1.16+) instead of string
113-
return true
114-
}
115113
case *googleapi.Error:
116114
// Retry on 408, 429, and 5xx, according to
117115
// https://cloud.google.com/storage/docs/exponential-backoff.
118116
return e.Code == 408 || e.Code == 429 || (e.Code >= 500 && e.Code < 600)
119-
case *url.Error:
117+
case *net.OpError, *url.Error:
120118
// Retry socket-level errors ECONNREFUSED and ECONNRESET (from syscall).
121119
// Unfortunately the error type is unexported, so we resort to string
122120
// matching.

‎storage/invoke_test.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,11 @@ func TestShouldRetry(t *testing.T) {
346346
inputErr: &url.Error{Op: "blah", URL: "blah", Err: errors.New("connection refused")},
347347
shouldRetry: true,
348348
},
349+
{
350+
desc: "net.OpError{Err: errors.New(\"connection reset by peer\")}",
351+
inputErr: &net.OpError{Op: "blah", Net: "tcp", Err: errors.New("connection reset by peer")},
352+
shouldRetry: true,
353+
},
349354
{
350355
desc: "io.ErrUnexpectedEOF",
351356
inputErr: io.ErrUnexpectedEOF,
@@ -382,9 +387,8 @@ func TestShouldRetry(t *testing.T) {
382387
shouldRetry: false,
383388
},
384389
{
385-
desc: "wrapped ErrClosed text",
386-
// TODO: check directly against wrapped net.ErrClosed (go 1.16+)
387-
inputErr: &net.OpError{Op: "write", Err: errors.New("use of closed network connection")},
390+
desc: "wrapped net.ErrClosed",
391+
inputErr: &net.OpError{Err: net.ErrClosed},
388392
shouldRetry: true,
389393
},
390394
} {

0 commit comments

Comments
 (0)