Skip to content

fix: deduplicate concurrent ReserveSample calls with singleflight#5373

Open
gacevicljubisa wants to merge 1 commit intomasterfrom
fix/singleflight-reserve-sample
Open

fix: deduplicate concurrent ReserveSample calls with singleflight#5373
gacevicljubisa wants to merge 1 commit intomasterfrom
fix/singleflight-reserve-sample

Conversation

@gacevicljubisa
Copy link
Member

@gacevicljubisa gacevicljubisa commented Feb 18, 2026

Checklist

  • I have read the coding guide.
  • My change requires a documentation update, and I have done it.
  • I have added tests to cover my changes.
  • I have filled out the description and linked the related issues.

Description

fixes #5166

The problem: makeSample (storage incentives agent) and SampleWithProofs (/rchash API endpoint) both can independently call ReserveSample, meaning concurrent calls with the same parameters would trigger duplicate expensive sampling operations.

The fix: Introduces a singleflight.Group on the Agent struct to deduplicate concurrent calls. Both callers now go through a shared reserveSampleAndHash method keyed by anchor + depth. If multiple goroutines call it simultaneously with the same key, only one actual ReserveSample + sampleHash executes — the rest wait and share the result.

Open API Spec Version Changes (if applicable)

Motivation and Context (Optional)

Related Issue (Optional)

Screenshots (if appropriate):

@gacevicljubisa gacevicljubisa marked this pull request as ready for review February 19, 2026 09:56
@acud
Copy link
Contributor

acud commented Feb 19, 2026

@gacevicljubisa can you explain how are the concurrent calls possible?

@gacevicljubisa
Copy link
Member Author

gacevicljubisa commented Feb 19, 2026

@gacevicljubisa can you explain how are the concurrent calls possible?

@acud I have updated the description with explanation.

Copy link
Contributor

@akrem-chabchoub akrem-chabchoub left a comment

Choose a reason for hiding this comment

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

Maybe some tests to cover these changes ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Running only one sampling process

4 participants