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

RUBY-3604 Fix multithread auth race condition #2912

Merged
merged 4 commits into from
Jan 21, 2025

Conversation

jteich
Copy link
Contributor

@jteich jteich commented Dec 23, 2024

Fixes: https://jira.mongodb.org/browse/RUBY-3604

This PR fixes a race-condition that we occasionally see when multiple threads are simultaneously authenticating with the database.

Details of the failure:
While the cache is being written to, the value will go through a phase, such that if it is retrieved a particular moment, it will return false (value is never actually set to false by the code, somehow the underlying Ruby code behaves this way). The cache code does check if the value is nil, but will consider the value false to be a cache hit. As a result, The methods client_key and server_key in scram_conversion_base.rb may incorrectly return false instead of the value from the block. This will result in an error "no implicit conversion of false into String" when attempting to use the "false" value for authentication.

In our testing, we found that protecting the cache with a mutex resolves this problem.

Of note is that the two other caches in this library are already made theadsafe. These are Mongo::Auth::Aws::CredentialsCache (auth/aws/credentials_cache.rb) [not to be confused with Mongo::Auth::CredentialCache] which is protected with a Mutex and Mongo::QueryCache (query_cache.rb) which is protected with a ThreadLocal variable.

@comandeo-mongo comandeo-mongo requested a review from jamis January 21, 2025 10:25
@comandeo-mongo comandeo-mongo merged commit b0ccd0e into mongodb:master Jan 21, 2025
250 of 258 checks passed
@comandeo-mongo
Copy link
Contributor

@jteich Thank you for your contribution!

comandeo-mongo added a commit to comandeo-mongo/mongo-ruby-driver that referenced this pull request Jan 22, 2025
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.

3 participants