@@ -351,7 +351,7 @@ private void doNetworkCall(boolean forceRefresh) {
351
351
}
352
352
353
353
// Store the prefs to persist the result of the previous step.
354
- persistedInstallation . insertOrUpdatePersistedInstallationEntry (prefs );
354
+ insertOrUpdatePrefs (prefs );
355
355
356
356
// Let the caller know about the result.
357
357
if (prefs .isErrored ()) {
@@ -365,6 +365,29 @@ private void doNetworkCall(boolean forceRefresh) {
365
365
}
366
366
}
367
367
368
+ /**
369
+ * Inserting or Updating the prefs. This operation is made cross-process and cross-thread safe by
370
+ * wrapping all the processing first in a java synchronization block and wrapping that in a
371
+ * cross-process lock created using FileLocks.
372
+ */
373
+ private void insertOrUpdatePrefs (PersistedInstallationEntry prefs ) {
374
+ synchronized (lockGenerateFid ) {
375
+ CrossProcessLock lock =
376
+ CrossProcessLock .acquire (firebaseApp .getApplicationContext (), LOCKFILE_NAME_GENERATE_FID );
377
+ try {
378
+ // Store the prefs to persist the result of the previous step.
379
+ persistedInstallation .insertOrUpdatePersistedInstallationEntry (prefs );
380
+ } finally {
381
+ // It is possible that the lock acquisition failed, resulting in lock being null.
382
+ // We handle this case by going on with our business even if the acquisition failed
383
+ // but we need to be sure to only release if we got a lock.
384
+ if (lock != null ) {
385
+ lock .releaseAndClose ();
386
+ }
387
+ }
388
+ }
389
+ }
390
+
368
391
/**
369
392
* Loads the prefs, generating a new ID if necessary. This operation is made cross-process and
370
393
* cross-thread safe by wrapping all the processing first in a java synchronization block and
@@ -513,7 +536,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
513
536
}
514
537
}
515
538
516
- persistedInstallation . insertOrUpdatePersistedInstallationEntry (entry .withNoGeneratedFid ());
539
+ insertOrUpdatePrefs (entry .withNoGeneratedFid ());
517
540
return null ;
518
541
}
519
542
}
0 commit comments