@@ -48,6 +48,7 @@ typedef void (SWIGSTDCALL *CompleteBuiltInGetToken)(int key, AppCheckToken* toke
4848static GetTokenFromCSharp g_get_token_from_csharp = nullptr ;
4949static int g_pending_token_keys = 0 ;
5050static std::map<int , std::function<void (AppCheckToken, int , const std::string&)>> g_pending_get_tokens;
51+ static ::firebase::Mutex g_pending_get_tokens_mutex;
5152
5253// Should be set to the C# function FirebaseAppCheck.TokenChangedMethod
5354static TokenChanged g_token_changed = nullptr ;
@@ -59,8 +60,19 @@ static CompleteBuiltInGetToken g_complete_built_in_get_token = nullptr;
5960void FinishGetTokenCallback (int key, const char * token, int64_t expire_ms,
6061 int error_code, const char * error_message) {
6162 // Get the function from the map, and erase it
62- auto callback = g_pending_get_tokens[key];
63- g_pending_get_tokens.erase (key);
63+ std::function<void (AppCheckToken, int , const std::string&)> callback;
64+ {
65+ MutexLock lock (g_pending_get_tokens_mutex);
66+ auto it = g_pending_get_tokens.find (key);
67+ if (it != g_pending_get_tokens.end ()) {
68+ callback = it->second ;
69+ g_pending_get_tokens.erase (it);
70+ } else {
71+ // The callback was missing. This is likely caused by trying to finish the same
72+ // callback multiple times, so ignore it.
73+ return ;
74+ }
75+ }
6476
6577 AppCheckToken app_check_token;
6678 app_check_token.token = token;
@@ -98,8 +110,12 @@ class SwigAppCheckProvider : public AppCheckProvider {
98110 completion_callback) override {
99111 if (g_get_token_from_csharp) {
100112 // Save the callback in the map, and generate a key
101- int key = g_pending_token_keys++;
102- g_pending_get_tokens[key] = completion_callback;
113+ int key;
114+ {
115+ MutexLock lock (g_pending_get_tokens_mutex);
116+ key = g_pending_token_keys++;
117+ g_pending_get_tokens[key] = completion_callback;
118+ }
103119 // Queue a call to the C# function that will generate the token.
104120 firebase::callback::AddCallback (
105121 new firebase::callback::CallbackValue1String1<int >(
0 commit comments