Skip to content

Commit

Permalink
Hopefully fix so loading crashes
Browse files Browse the repository at this point in the history
Summary:
Changelog:

[Android][Internal] Fix potential initializer interruption threading crashes.

Reviewed By: mdvacca

Differential Revision: D20615755

fbshipit-source-id: 58b706deeb6df1998caff5bf2ae9ec60114313fe
  • Loading branch information
sahrens authored and facebook-github-bot committed Mar 24, 2020
1 parent d241de9 commit 33f2024
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,20 @@
* pass it from CatalystInstance, through Java, to TurboModuleManager::initHybrid.
*/
public class CallInvokerHolderImpl implements CallInvokerHolder {
static {
SoLoader.loadLibrary("turbomodulejsijni");
}
private static volatile boolean sIsSoLibraryLoaded;

private final HybridData mHybridData;

private CallInvokerHolderImpl(HybridData hd) {
maybeLoadSoLibrary();
mHybridData = hd;
}

// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context.
private static synchronized void maybeLoadSoLibrary() {
if (!sIsSoLibraryLoaded) {
SoLoader.loadLibrary("turbomodulejsijni");
sIsSoLibraryLoaded = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,9 @@
* a Java module, that the C++ counterpart calls.
*/
public class TurboModuleManager implements JSIModule, TurboModuleRegistry {
static {
SoLoader.loadLibrary("turbomodulejsijni");
}

private final TurboModuleManagerDelegate mTurbomoduleManagerDelegate;
private static volatile boolean sIsSoLibraryLoaded;

private final Map<String, TurboModule> mTurboModules = new HashMap<>();

Expand All @@ -43,6 +41,7 @@ public TurboModuleManager(
TurboModuleManagerDelegate tmmDelegate,
CallInvokerHolder jsCallInvokerHolder,
CallInvokerHolder nativeCallInvokerHolder) {
maybeLoadSoLibrary();
mHybridData =
initHybrid(
jsContext.get(),
Expand Down Expand Up @@ -137,4 +136,12 @@ public void onCatalystInstanceDestroy() {
// Delete the native part of this hybrid class.
mHybridData.resetNative();
}

// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context.
private static synchronized void maybeLoadSoLibrary() {
if (!sIsSoLibraryLoaded) {
SoLoader.loadLibrary("turbomodulejsijni");
sIsSoLibraryLoaded = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@
import java.util.List;

public abstract class TurboModuleManagerDelegate {
static {
SoLoader.loadLibrary("turbomodulejsijni");
}

private final HybridData mHybridData;

private static volatile boolean sIsSoLibraryLoaded;

protected abstract HybridData initHybrid();

protected TurboModuleManagerDelegate() {
maybeLoadOtherSoLibraries();
maybeLoadSoLibrary();
mHybridData = initHybrid();
}

Expand All @@ -45,4 +45,14 @@ protected TurboModuleManagerDelegate() {
public List<String> getEagerInitModuleNames() {
return new ArrayList<>();
}

// Prevents issues with initializer interruptions. See T38996825 and D13793825 for more context.
private static synchronized void maybeLoadSoLibrary() {
if (!sIsSoLibraryLoaded) {
SoLoader.loadLibrary("turbomodulejsijni");
sIsSoLibraryLoaded = true;
}
}

protected synchronized void maybeLoadOtherSoLibraries() {}
}

0 comments on commit 33f2024

Please sign in to comment.