Skip to content

fix panic in stream push when context deadline expires during request marshalling#7541

Open
SungJin1212 wants to merge 3 commits into
cortexproject:masterfrom
SungJin1212:fix-push-stream-out-of-range-panic
Open

fix panic in stream push when context deadline expires during request marshalling#7541
SungJin1212 wants to merge 3 commits into
cortexproject:masterfrom
SungJin1212:fix-push-stream-out-of-range-panic

Conversation

@SungJin1212
Copy link
Copy Markdown
Member

@SungJin1212 SungJin1212 commented May 22, 2026

Fix Distributor slice bounds out of range panic with gRPC push stream

Which issue(s) this PR fixes:
Fixes #7540

Checklist

  • Tests updated
  • Documentation added
  • CHANGELOG.md updated - the order of entries should be [CHANGE], [FEATURE], [ENHANCEMENT], [BUGFIX]
  • docs/configuration/v1-guarantees.md updated if this PR introduces experimental flags

@dosubot dosubot Bot added component/distributor go Pull requests that update Go code type/bug labels May 22, 2026
@SungJin1212 SungJin1212 changed the title fix panic in stream push when context deadline expires during request… fix panic in stream push when context deadline expires during request marshalling May 22, 2026
@SungJin1212
Copy link
Copy Markdown
Member Author

SungJin1212 commented May 22, 2026

@alexqyle
Could you take a look?

… marshalling

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
@SungJin1212 SungJin1212 force-pushed the fix-push-stream-out-of-range-panic branch from 93b0a3e to c93d079 Compare May 25, 2026 23:21
Copy link
Copy Markdown
Member

@friedrichg friedrichg left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wdyt?

Comment thread pkg/ingester/client/client.go Outdated
}
c.streamPushChan <- job
<-reqCtx.Done()
<-job.sendDone
Copy link
Copy Markdown
Member

@friedrichg friedrichg May 26, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<-job.sendDone
select {
case <-job.sendDone:
return job.resp, job.err
case <-ctx.Done():
<-job.sendDone
return nil, ctx.Err()
}

Comment thread pkg/ingester/client/client_test.go Outdated
defer pushCancel()

// PushStreamConnection blocks until Send()+Recv() complete.
client.PushStreamConnection(pushCtx, writeReq) //nolint:errcheck
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
client.PushStreamConnection(pushCtx, writeReq) //nolint:errcheck
_, pushErr := client.PushStreamConnection(pushCtx, writeReq)
require.ErrorIs(t, pushErr, context.DeadlineExceeded,
"caller should observe its own context deadline")

Comment thread pkg/ingester/client/client.go Outdated
Comment on lines +236 to +258
err = stream.Send(job.req)
if err == io.EOF {
job.resp = &cortexpb.WriteResponse{}
job.cancel()
close(job.sendDone)
return
}
if err != nil {
job.err = err
job.cancel()
close(job.sendDone)
continue
}
resp, err := stream.Recv()
if err == io.EOF {
job.resp = &cortexpb.WriteResponse{}
job.cancel()
close(job.sendDone)
return
}
job.resp = resp
job.err = err
if err == nil && job.resp.Code != http.StatusOK {
job.err = httpgrpc.Errorf(int(job.resp.Code), "%s", job.resp.Message)
}
job.cancel()
close(job.sendDone)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

close(job.sendDone) is repeated four times across all exit paths. With the new model, missing one of these through a panic, or someone adding a new return path in a future refactor would deadlock the caller forever on <-job.sendDone

Maybe it makes sense to do the close in a defer command

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I extracted the processing logic as a dedicated func and added a defer

Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Signed-off-by: SungJin1212 <tjdwls1201@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

component/distributor go Pull requests that update Go code size/L type/bug

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bug: Distributor panic with gRPC push stream

2 participants