Skip to content

Commit 103e020

Browse files
rfrattotrevorwhitney
authored andcommitted
fix(storage/chunk/client/aws): have GetObject check for canceled context (#14420)
1 parent 2135959 commit 103e020

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

pkg/storage/chunk/client/aws/s3_storage_client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ func (a *S3ObjectClient) GetObject(ctx context.Context, objectKey string) (io.Re
393393
// Map the key into a bucket
394394
bucket := a.bucketFromKey(objectKey)
395395

396-
var lastErr error
396+
lastErr := ctx.Err()
397397

398398
retries := backoff.New(ctx, a.cfg.BackoffConfig)
399399
for retries.Ongoing() {

pkg/storage/chunk/client/aws/s3_storage_client_test.go

+25
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,31 @@ func TestRequestMiddleware(t *testing.T) {
234234
}
235235
}
236236

237+
func TestS3ObjectClient_GetObject_CanceledContext(t *testing.T) {
238+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
239+
fmt.Fprintln(w, r.Header.Get("echo-me"))
240+
}))
241+
defer ts.Close()
242+
243+
cfg := S3Config{
244+
Endpoint: ts.URL,
245+
BucketNames: "buck-o",
246+
S3ForcePathStyle: true,
247+
Insecure: true,
248+
AccessKeyID: "key",
249+
SecretAccessKey: flagext.SecretWithValue("secret"),
250+
}
251+
252+
client, err := NewS3ObjectClient(cfg, hedging.Config{})
253+
require.NoError(t, err)
254+
255+
ctx, cancel := context.WithCancel(context.Background())
256+
cancel()
257+
258+
_, _, err = client.GetObject(ctx, "key")
259+
require.Error(t, err, "GetObject should fail when given a canceled context")
260+
}
261+
237262
func Test_Hedging(t *testing.T) {
238263
for _, tc := range []struct {
239264
name string

0 commit comments

Comments
 (0)