Skip to content

Commit 081aade

Browse files
authored
Fix OverlappingFileLockException. (#1536)
* Dropping guava and appcompat dependency from FIS SDK. * Fix OverlappingFileLockException. Access the data shared by multiple threads only after acquiring cross-process and multi-thread safe locks.
1 parent 9eb88b1 commit 081aade

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
version=16.2.2
2-
latestReleasedVersion=16.2.1
1+
version=16.3.0
2+
latestReleasedVersion=16.2.2

firebase-installations/src/main/java/com/google/firebase/installations/FirebaseInstallations.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ private void doNetworkCall(boolean forceRefresh) {
351351
}
352352

353353
// Store the prefs to persist the result of the previous step.
354-
persistedInstallation.insertOrUpdatePersistedInstallationEntry(prefs);
354+
insertOrUpdatePrefs(prefs);
355355

356356
// Let the caller know about the result.
357357
if (prefs.isErrored()) {
@@ -365,6 +365,29 @@ private void doNetworkCall(boolean forceRefresh) {
365365
}
366366
}
367367

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+
368391
/**
369392
* Loads the prefs, generating a new ID if necessary. This operation is made cross-process and
370393
* cross-thread safe by wrapping all the processing first in a java synchronization block and
@@ -513,7 +536,7 @@ private Void deleteFirebaseInstallationId() throws FirebaseInstallationsExceptio
513536
}
514537
}
515538

516-
persistedInstallation.insertOrUpdatePersistedInstallationEntry(entry.withNoGeneratedFid());
539+
insertOrUpdatePrefs(entry.withNoGeneratedFid());
517540
return null;
518541
}
519542
}

0 commit comments

Comments
 (0)