@@ -48,6 +48,7 @@ typedef void (SWIGSTDCALL *CompleteBuiltInGetToken)(int key, AppCheckToken* toke
48
48
static GetTokenFromCSharp g_get_token_from_csharp = nullptr ;
49
49
static int g_pending_token_keys = 0 ;
50
50
static std::map<int , std::function<void (AppCheckToken, int , const std::string&)>> g_pending_get_tokens;
51
+ static ::firebase::Mutex g_pending_get_tokens_mutex;
51
52
52
53
// Should be set to the C# function FirebaseAppCheck.TokenChangedMethod
53
54
static TokenChanged g_token_changed = nullptr ;
@@ -59,8 +60,19 @@ static CompleteBuiltInGetToken g_complete_built_in_get_token = nullptr;
59
60
void FinishGetTokenCallback (int key, const char * token, int64_t expire_ms,
60
61
int error_code, const char * error_message) {
61
62
// 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
+ }
64
76
65
77
AppCheckToken app_check_token;
66
78
app_check_token.token = token;
@@ -98,8 +110,12 @@ class SwigAppCheckProvider : public AppCheckProvider {
98
110
completion_callback) override {
99
111
if (g_get_token_from_csharp) {
100
112
// 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
+ }
103
119
// Queue a call to the C# function that will generate the token.
104
120
firebase::callback::AddCallback (
105
121
new firebase::callback::CallbackValue1String1<int >(
0 commit comments