Skip to content

Commit d040f29

Browse files
chkuang-ga-maurice
authored andcommitted
Make sure both find and register are guarded for race condition
PiperOrigin-RevId: 253870928
1 parent 67f78c4 commit d040f29

File tree

4 files changed

+6
-0
lines changed

4 files changed

+6
-0
lines changed

instance_id/src/android/instance_id.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,7 @@ Future<void> InstanceId::DeleteToken(const char* entity, const char* scope) {
309309
InstanceId* InstanceId::GetInstanceId(App* app, InitResult* init_result_out) {
310310
FIREBASE_ASSERT_MESSAGE_RETURN(nullptr, app, "App must be specified.");
311311
FIREBASE_UTIL_RETURN_NULL_IF_GOOGLE_PLAY_UNAVAILABLE(*app, init_result_out);
312+
MutexLock lock(InstanceIdInternal::mutex());
312313
if (init_result_out) *init_result_out = kInitResultSuccess;
313314
auto instance_id = InstanceIdInternal::FindInstanceIdByApp(app);
314315
if (instance_id) return instance_id;

instance_id/src/desktop/instance_id.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Future<void> InstanceId::DeleteToken(const char* entity, const char* scope) {
159159
}
160160

161161
InstanceId* InstanceId::GetInstanceId(App* app, InitResult* init_result_out) {
162+
MutexLock lock(InstanceIdInternal::mutex());
162163
if (init_result_out) *init_result_out = kInitResultSuccess;
163164
auto instance_id = InstanceIdInternal::FindInstanceIdByApp(app);
164165
if (instance_id) return instance_id;

instance_id/src/instance_id_internal_base.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,9 @@ class InstanceIdInternalBase {
5959
// Find an InstanceId instance associated with an app.
6060
static InstanceId *FindInstanceIdByApp(App *app);
6161

62+
// Return the mutex to make sure both find and register are guarded.
63+
static Mutex& mutex() { return instance_id_by_app_mutex_; }
64+
6265
private:
6366
/// Handle calls from Futures that the API returns.
6467
ReferenceCountedFutureImpl future_api_;

instance_id/src/ios/instance_id.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,7 @@ static void HookAppDelegateMethods(Class clazz) {
229229

230230
InstanceId* InstanceId::GetInstanceId(App* app, InitResult* init_result_out) {
231231
FIREBASE_ASSERT_MESSAGE_RETURN(nullptr, app, "App must be specified.");
232+
MutexLock lock(InstanceIdInternal::mutex());
232233
if (app != App::GetInstance()) {
233234
LogError("InstanceId can only be created for the default app on iOS. "
234235
"App %s is not the default app.", app->name());

0 commit comments

Comments
 (0)