From 4670d86fb9ce9069941221a9ff3a93a5a2d12dcc Mon Sep 17 00:00:00 2001 From: Matthew Rheaume Date: Thu, 21 Apr 2022 17:19:33 -0700 Subject: [PATCH] Fix race condition in `SoLoader#init`. 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. --- java/com/facebook/soloader/SoLoader.java | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/java/com/facebook/soloader/SoLoader.java b/java/com/facebook/soloader/SoLoader.java index 11eb942..1273843 100644 --- a/java/com/facebook/soloader/SoLoader.java +++ b/java/com/facebook/soloader/SoLoader.java @@ -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); @@ -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;