Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] - Fix race condition #2656

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft

Conversation

ahrav
Copy link
Collaborator

@ahrav ahrav commented Apr 2, 2024

Description:

The function uses a global mutex rateLimitMu to synchronize access to the rateLimitResumeTime variable. However, when reading the rateLimitResumeTime variable. The code first reads the value using rateLimitMu.RLock() and then later locks the mutex using rateLimitMu.Lock() to update the value. If another goroutine updates the rateLimitResumeTime between the read and write operations.

Example:
Thread 1:

  • Acquires rateLimitMu.RLock().
  • Reads the value of rateLimitResumeTime.
  • Releases rateLimitMu.RUnlock().
    Thread 2:
  • Acquires rateLimitMu.Lock().
  • Modifies rateLimitResumeTime.
  • Releases rateLimitMu.Unlock().
    Thread 1 (cont.):
  • Proceeds based on the old value of rateLimitResumeTime that it read earlier.

Checklist:

  • Tests passing (make test-community)?
  • Lint passing (make lint this requires golangci-lint)?

@ahrav ahrav marked this pull request as ready for review April 2, 2024 19:38
@ahrav ahrav requested a review from a team as a code owner April 2, 2024 19:38
@rgmz
Copy link
Contributor

rgmz commented Apr 2, 2024

Interesting, I didn't realize that such a race was possible. The underlying go-github repository implements its own rate limit handling, so it seemed highly unlikely that rateLimitResumeTime would be updated while an existing rate limit was still valid.

@rosecodym
Copy link
Collaborator

This is changing code introduced by #2379, right?

@rgmz
Copy link
Contributor

rgmz commented Apr 3, 2024

I think it's entirely from #2041.

@ahrav ahrav marked this pull request as draft April 4, 2024 14:30
@ahrav
Copy link
Collaborator Author

ahrav commented Apr 4, 2024

This solution is actually not ideal. After some discussion w/ @mcastorina the fact we are holding the lock while sleeping is raising some alarm 🔔s. Additionally this approach potentially results in a stale error state once a waiting goroutine resumes processing after the retry after period. Converting this to a Draft for now.

@rgmz
Copy link
Contributor

rgmz commented Apr 4, 2024

After some discussion w/ @mcastorina the fact we are holding the lock while sleeping is raising some alarm 🔔s

Yeah, that's why I used the (admittedly awkward) explicit unlocks instead of defer.

#2041 (comment)

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

Successfully merging this pull request may close these issues.

4 participants