Skip to content

Commit

Permalink
There is a period of time between checking if NativeLoader has been i…
Browse files Browse the repository at this point in the history
…nitialized, and when we try to perform .init() which means a race condition can occur (as mentioned in facebook/SoLoader#60).

This change uses synchronization on the NativeLoader class, and a secondary check to verify that the initialization needs to take place just before we perform it, in order to reduce the window for a race condition to affect client code.

When combined with facebook/SoLoader#69 this will remove the race condition from the code because the initialisation inside SoLoader and inside Fresco will both be synchronized on the NativeLoad.class object.
  • Loading branch information
alsutton committed Jan 20, 2021
1 parent 21e43cf commit 7dcfe68
Showing 1 changed file with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,16 @@ public static void initialize(
clazz.getMethod("init", Context.class).invoke(null, context);
} catch (ClassNotFoundException e) {
// Failed to initialize SoLoader
NativeLoader.init(new SystemDelegate());
initializeNativeLoaderWithSystemDelegate();
} catch (IllegalAccessException e) {
// Failed to initialize SoLoader
NativeLoader.init(new SystemDelegate());
initializeNativeLoaderWithSystemDelegate();
} catch (InvocationTargetException e) {
// Failed to initialize SoLoader
NativeLoader.init(new SystemDelegate());
initializeNativeLoaderWithSystemDelegate();
} catch (NoSuchMethodException e) {
// Failed to initialize SoLoader
NativeLoader.init(new SystemDelegate());
initializeNativeLoaderWithSystemDelegate();
} finally {
if (FrescoSystrace.isTracing()) {
FrescoSystrace.endSection();
Expand All @@ -113,6 +113,14 @@ public static void initialize(
}
}

private static void initializeNativeLoaderWithSystemDelegate() {
synchronized(NativeLoader.class) {
if(!NativeLoader.isInitialized()) {
NativeLoader.init(new SystemDelegate());
}
}
}

/** Initializes Drawee with the specified config. */
private static void initializeDrawee(Context context, @Nullable DraweeConfig draweeConfig) {
if (FrescoSystrace.isTracing()) {
Expand Down

0 comments on commit 7dcfe68

Please sign in to comment.