Skip to content

Commit 6fced37

Browse files
authored
Merge pull request #137 from jthiltges/pr/mutex
Add mutex around key refresh with get_public_keys_from_web()
2 parents 4f82163 + f87b59c commit 6fced37

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

src/scitokens_internal.cpp

+7-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ struct CurlRaii {
3131

3232
CurlRaii myCurl;
3333

34+
std::mutex key_refresh_mutex;
35+
3436
} // namespace
3537

3638
namespace scitokens {
@@ -792,11 +794,15 @@ Validator::get_public_key_pem(const std::string &issuer, const std::string &kid,
792794

793795
if (get_public_keys_from_db(issuer, now, result->m_keys,
794796
result->m_next_update)) {
795-
if (now > result->m_next_update) {
797+
std::unique_lock<std::mutex> lock(key_refresh_mutex, std::defer_lock);
798+
// If refresh is due *and* the key refresh mutex is free, try to update
799+
if (now > result->m_next_update && lock.try_lock()) {
796800
try {
797801
result->m_ignore_error = true;
798802
result = get_public_keys_from_web(
799803
issuer, internal::SimpleCurlGet::default_timeout);
804+
// Hold refresh mutex in the new result
805+
result->m_refresh_lock = std::move(lock);
800806
} catch (std::runtime_error &) {
801807
result->m_do_store = false;
802808
// ignore the exception: we have a valid set of keys already

src/scitokens_internal.h

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22
#include <memory>
3+
#include <mutex>
34
#include <sstream>
45
#include <unordered_map>
56

@@ -212,6 +213,7 @@ class AsyncStatus {
212213
bool m_has_metadata{false};
213214
bool m_oauth_fallback{false};
214215
AsyncState m_state{DOWNLOAD_METADATA};
216+
std::unique_lock<std::mutex> m_refresh_lock;
215217

216218
int64_t m_next_update{-1};
217219
int64_t m_expires{-1};

0 commit comments

Comments
 (0)