Skip to content

Commit f3d69ff

Browse files
fix(storage/chunk/client/aws): have GetObject check for canceled context (backport release-3.1.x) (#14421)
Co-authored-by: Robert Fratto <robertfratto@gmail.com>
1 parent 05b6a65 commit f3d69ff

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func (a *S3ObjectClient) GetObject(ctx context.Context, objectKey string) (io.Re
355355
// Map the key into a bucket
356356
bucket := a.bucketFromKey(objectKey)
357357

358-
var lastErr error
358+
lastErr := ctx.Err()
359359

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

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

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,31 @@ func TestRequestMiddleware(t *testing.T) {
9191
}
9292
}
9393

94+
func TestS3ObjectClient_GetObject_CanceledContext(t *testing.T) {
95+
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
96+
fmt.Fprintln(w, r.Header.Get("echo-me"))
97+
}))
98+
defer ts.Close()
99+
100+
cfg := S3Config{
101+
Endpoint: ts.URL,
102+
BucketNames: "buck-o",
103+
S3ForcePathStyle: true,
104+
Insecure: true,
105+
AccessKeyID: "key",
106+
SecretAccessKey: flagext.SecretWithValue("secret"),
107+
}
108+
109+
client, err := NewS3ObjectClient(cfg, hedging.Config{})
110+
require.NoError(t, err)
111+
112+
ctx, cancel := context.WithCancel(context.Background())
113+
cancel()
114+
115+
_, _, err = client.GetObject(ctx, "key")
116+
require.Error(t, err, "GetObject should fail when given a canceled context")
117+
}
118+
94119
func Test_Hedging(t *testing.T) {
95120
for _, tc := range []struct {
96121
name string

0 commit comments

Comments
 (0)