Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

importccl: Correctly handle cancelled context when importing #43789

Merged
merged 1 commit into from
Jan 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion pkg/storage/cloud/external_storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -559,6 +559,10 @@ func checkHTTPContentRangeHeader(h string, pos int64) error {
func (r *resumingHTTPReader) sendRequest(
reqHeaders map[string]string,
) (resp *http.Response, err error) {
// Initialize err to the context.Canceled: if our context is canceled, we will
// never enter the loop below; in this case we want to return "nil, canceled"
err = context.Canceled

for attempt, retries := 0,
retry.StartWithCtx(r.ctx, httpRetryOptions); retries.Next(); attempt++ {
resp, err = r.client.req(r.ctx, "GET", r.url, nil, reqHeaders)
Expand All @@ -570,7 +574,7 @@ func (r *resumingHTTPReader) sendRequest(
log.Errorf(r.ctx, "HTTP:Req error: err=%s (attempt %d)", err, attempt)

if _, ok := err.(*retryableHTTPError); !ok {
break
return
}
}

Expand Down
19 changes: 19 additions & 0 deletions pkg/storage/cloud/external_storage_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,22 @@ func TestHttpGet(t *testing.T) {
})
}
}

func TestHttpGetWithCancelledContext(t *testing.T) {
defer leaktest.AfterTest(t)()

s := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {}))
defer s.Close()

store, err := makeHTTPStorage(s.URL, testSettings)
require.NoError(t, err)
defer func() {
require.NoError(t, store.Close())
}()

ctx, cancel := context.WithCancel(context.Background())
cancel()

_, err = store.ReadFile(ctx, "/something")
require.Error(t, context.Canceled, err)
}