Skip to content

Conversation

kevinAlbs
Copy link
Collaborator

@kevinAlbs kevinAlbs commented Sep 19, 2025

Patch: https://spruce.mongodb.com/version/68cd50d489be4200072d0dd3

Summary

Add internal OIDC cache: mongoc_oidc_cache_t

Background & Motivation

mongoc_oidc_cache_t is an internal component tested in isolation. Subsequent PRs will use mongoc_oidc_cache_t to resolve CDRIVER-4689.

mongoc_oidc_cache_t implements behaviors described in Credential Caching. mongoc_oidc_cache_t implements the "Client Cache" (not the "Connection Cache").

mongoc_oidc_cache_set_usleep_fn is added to permit a custom sleep function (related to CDRIVER-4736).

@kevinAlbs kevinAlbs marked this pull request as ready for review September 19, 2025 13:33
@kevinAlbs kevinAlbs requested a review from a team as a code owner September 19, 2025 13:33
Copy link
Collaborator Author

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

Latest changes tested with this patch: https://spruce.mongodb.com/version/68d5332f0f310c0007df4bf8

Filed CDRIVER-6101 to track assumed unrelated failure task: "Unexpected error running killAllSessions".

mongoc_oidc_cache_set_cached_token(mongoc_oidc_cache_t *cache, const char *token);

// mongoc_oidc_cache_invalidate_cached_token invalidates if the cached token matches `token`. Thread safe.
// mongoc_oidc_cache_invalidate_token invalidates if the cached token (if any) matches `token`. Thread safe.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// mongoc_oidc_cache_invalidate_token invalidates if the cached token (if any) matches `token`. Thread safe.
// mongoc_oidc_cache_invalidate_token invalidates the cached token if it matches `token`. Thread safe.

Wording tweak suggestion (a bit too many "if"s).

Comment on lines 122 to 127
bson_shared_mutex_lock(&cache->lock);

bson_free(cache->token);
cache->token = NULL;
cache->token = bson_strdup(token);
bson_shared_mutex_unlock(&cache->lock);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
bson_shared_mutex_lock(&cache->lock);
bson_free(cache->token);
cache->token = NULL;
cache->token = bson_strdup(token);
bson_shared_mutex_unlock(&cache->lock);
bson_shared_mutex_lock(&cache->lock);
char *const old_token = cache->token;
cache->token = bson_strdup(token);
bson_shared_mutex_unlock(&cache->lock);
bson_free(old_token);

Minor optimization: move deallocation out of critical zone + remove redundant assignment.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated here and in mongoc_oidc_cache_invalidate_token.

Comment on lines 163 to 169
mongoc_oidc_callback_params_t *params = mongoc_oidc_callback_params_new();
mongoc_oidc_callback_params_set_user_data(params, mongoc_oidc_callback_get_user_data(cache->callback));
// From spec: "If CSOT is not applied, then the driver MUST use 1 minute as the timeout."
// The timeout parameter (when set) is meant to be directly compared against bson_get_monotonic_time(). It is a
// time point, not a duration.
mongoc_oidc_callback_params_set_timeout(
params, mlib_microseconds_count(mlib_time_add(mlib_now(), mlib_duration(1, min)).time_since_monotonic_start));
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor optimization: move callback param object initialization up and out of the critical section.


cache->last_called = mlib_now();
cache->ever_called = true;
mongoc_oidc_callback_params_destroy(params);
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor optimization: move callback param object deallocation down and out of the critical section.

cache->token = bson_strdup(token); // Cache a copy.

unlock_and_return:
bson_shared_mutex_unlock(&cache->lock);
Copy link
Contributor

Choose a reason for hiding this comment

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

Minor suggestion: consider scoping critical sections with {}:

{
   // Obtain write-lock:
   bson_shared_mutex_lock(&cache->lock);

   ...

unlock_and_return:
   bson_shared_mutex_unlock(&cache->lock);
}

Copy link
Collaborator Author

@kevinAlbs kevinAlbs left a comment

Choose a reason for hiding this comment

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

Latest changes include 51adf05 to switch from (1, min) to (60, sec) to work around CDRIVER-6102.

Comment on lines 122 to 127
bson_shared_mutex_lock(&cache->lock);

bson_free(cache->token);
cache->token = NULL;
cache->token = bson_strdup(token);
bson_shared_mutex_unlock(&cache->lock);
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Updated here and in mongoc_oidc_cache_invalidate_token.

@kevinAlbs kevinAlbs merged commit 016415b into mongodb:master Oct 1, 2025
43 of 46 checks passed
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