Skip to content

Commit

Permalink
Fix race condition with App Check callback (#984)
Browse files Browse the repository at this point in the history
* Fix race condition with App Check callback

* Update app_check.i

* Update readme.md
  • Loading branch information
a-maurice authored Apr 15, 2024
1 parent 38b67d2 commit 956b072
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 4 deletions.
24 changes: 20 additions & 4 deletions app_check/src/swig/app_check.i
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ typedef void (SWIGSTDCALL *CompleteBuiltInGetToken)(int key, AppCheckToken* toke
static GetTokenFromCSharp g_get_token_from_csharp = nullptr;
static int g_pending_token_keys = 0;
static std::map<int, std::function<void(AppCheckToken, int, const std::string&)>> g_pending_get_tokens;
static ::firebase::Mutex g_pending_get_tokens_mutex;

// Should be set to the C# function FirebaseAppCheck.TokenChangedMethod
static TokenChanged g_token_changed = nullptr;
Expand All @@ -59,8 +60,19 @@ static CompleteBuiltInGetToken g_complete_built_in_get_token = nullptr;
void FinishGetTokenCallback(int key, const char* token, int64_t expire_ms,
int error_code, const char* error_message) {
// Get the function from the map, and erase it
auto callback = g_pending_get_tokens[key];
g_pending_get_tokens.erase(key);
std::function<void(AppCheckToken, int, const std::string&)> callback;
{
MutexLock lock(g_pending_get_tokens_mutex);
auto it = g_pending_get_tokens.find(key);
if (it != g_pending_get_tokens.end()) {
callback = it->second;
g_pending_get_tokens.erase(it);
} else {
// The callback was missing. This is likely caused by trying to finish the same
// callback multiple times, so ignore it.
return;
}
}

AppCheckToken app_check_token;
app_check_token.token = token;
Expand Down Expand Up @@ -98,8 +110,12 @@ class SwigAppCheckProvider : public AppCheckProvider {
completion_callback) override {
if (g_get_token_from_csharp) {
// Save the callback in the map, and generate a key
int key = g_pending_token_keys++;
g_pending_get_tokens[key] = completion_callback;
int key;
{
MutexLock lock(g_pending_get_tokens_mutex);
key = g_pending_token_keys++;
g_pending_get_tokens[key] = completion_callback;
}
// Queue a call to the C# function that will generate the token.
firebase::callback::AddCallback(
new firebase::callback::CallbackValue1String1<int>(
Expand Down
5 changes: 5 additions & 0 deletions docs/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,11 @@ Support

Release Notes
-------------
### Upcoming
- Changes
- App Check: Fix potential crash when fetching a token.
([#877](https://github.com/firebase/firebase-unity-sdk/issues/877))

### 11.8.1
- Changes
- Firestore (iOS): Fix undefined absl symbols error.
Expand Down

0 comments on commit 956b072

Please sign in to comment.