Skip to content

Commit

Permalink
Fix race condition in SoLoader#init. (#99)
Browse files Browse the repository at this point in the history
Summary:
If two threads call `SoLoader#init` at the same time, there is a chance
they both try to initialize `sSoSources` one after the other, which
eventually leads to an `OverlappingFileLockException`.

To fix this issue, add a double check after obtaining the write lock to
only initialize if `sSoSources` is still null.

Fixes #93.

Pull Request resolved: #99

Differential Revision: D35891724

Pulled By: passy

fbshipit-source-id: fb1bcfaa9df7102c1083ec0c0deb336cef75c0dd
  • Loading branch information
Matthew Rheaume authored and facebook-github-bot committed Apr 27, 2022
1 parent efaf467 commit 05322e3
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion java/com/facebook/soloader/SoLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public class SoLoader {
*/
@GuardedBy("sSoSourcesLock")
@Nullable
private static SoSource[] sSoSources = null;
private static volatile SoSource[] sSoSources = null;

@GuardedBy("sSoSourcesLock")
private static final AtomicInteger sSoSourcesVersion = new AtomicInteger(0);
Expand Down Expand Up @@ -278,6 +278,13 @@ private static void initSoSources(Context context, int flags, String[] denyList)
}

sSoSourcesLock.writeLock().lock();

// Double check that sSoSources wasn't initialized while waiting for the lock.
if (sSoSources != null) {
sSoSourcesLock.writeLock().unlock();
return;
}

try {
sFlags = flags;

Expand Down

0 comments on commit 05322e3

Please sign in to comment.