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

Use correct atomic operation while generating client id #9976

Open
wants to merge 1 commit into
base: unstable
Choose a base branch
from

Conversation

tezc
Copy link
Collaborator

@tezc tezc commented Dec 21, 2021

To generate a client id, atomicGetIncr() is called. atomicGetIncr() uses memory_order_relaxed which does not guarantee visibility. If createClient() is called from different threads, it might cause generating same id for different clients.

This PR adds incr/decr atomic operations with visibility guarantees and uses atomicGetIncrWithSync() for client id generation.

@oranagra
Copy link
Member

@tezc please remind me where we create client outside the main thread without the GIL being locked?
is it just RM_GetThreadSafeContext?
i suppose these all must be createClient(NULL), since passing a non NULL arg will lead to linkClient being called, which is not protected by any synchronization code.

@ShooterIT FYI.

@oranagra oranagra added this to Needs triage in Triage via automation Dec 21, 2021
@oranagra oranagra moved this from Needs triage to In review in Triage Dec 21, 2021
@tezc
Copy link
Collaborator Author

tezc commented Dec 21, 2021

@oranagra Yes, looks like these two right now : RM_GetThreadSafeContext, RM_GetDetachedThreadSafeContext.

@tezc
Copy link
Collaborator Author

tezc commented Dec 21, 2021

Btw, I didn't run into a problem. Feels like quite unlikely to cause an issue(Also looks quite hard to identify the problem and reproduce it). Just discussed a bit with @yossigo, as it's safer to use full memory barrier, I created the PR.

@ShooterIT
Copy link
Collaborator

If createClient() is called from different threads, it might cause generating same id for different clients.

I don't think this problem could happen, atomic operation could guarantee atomic. There is a problem that others threads could read the latest update, but in redis, there is no this problem since executing commands always is in the main thread.

FYI, https://en.cppreference.com/w/c/atomic/atomic_fetch_add

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: In review
Triage
In review
Development

Successfully merging this pull request may close these issues.

None yet

4 participants