diff --git a/pkg/storage/chunk/client/aws/s3_storage_client.go b/pkg/storage/chunk/client/aws/s3_storage_client.go index 11696f67eddb4..2c3d432b02359 100644 --- a/pkg/storage/chunk/client/aws/s3_storage_client.go +++ b/pkg/storage/chunk/client/aws/s3_storage_client.go @@ -355,7 +355,7 @@ func (a *S3ObjectClient) GetObject(ctx context.Context, objectKey string) (io.Re // Map the key into a bucket bucket := a.bucketFromKey(objectKey) - var lastErr error + lastErr := ctx.Err() retries := backoff.New(ctx, a.cfg.BackoffConfig) for retries.Ongoing() { diff --git a/pkg/storage/chunk/client/aws/s3_storage_client_test.go b/pkg/storage/chunk/client/aws/s3_storage_client_test.go index d966f1a2f9f9c..d2d7bd032bdd3 100644 --- a/pkg/storage/chunk/client/aws/s3_storage_client_test.go +++ b/pkg/storage/chunk/client/aws/s3_storage_client_test.go @@ -130,6 +130,31 @@ func TestRequestMiddleware(t *testing.T) { } } +func TestS3ObjectClient_GetObject_CanceledContext(t *testing.T) { + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + fmt.Fprintln(w, r.Header.Get("echo-me")) + })) + defer ts.Close() + + cfg := S3Config{ + Endpoint: ts.URL, + BucketNames: "buck-o", + S3ForcePathStyle: true, + Insecure: true, + AccessKeyID: "key", + SecretAccessKey: flagext.SecretWithValue("secret"), + } + + client, err := NewS3ObjectClient(cfg, hedging.Config{}) + require.NoError(t, err) + + ctx, cancel := context.WithCancel(context.Background()) + cancel() + + _, _, err = client.GetObject(ctx, "key") + require.Error(t, err, "GetObject should fail when given a canceled context") +} + func Test_Hedging(t *testing.T) { for _, tc := range []struct { name string