Repository navigation
fix: load the native library only once when Logging is used first - #593
Merged
Merged
Conversation
NativeLoader.loadLibrary marked a library as loaded only after loading it. Loading webrtc-java runs its JNI_OnLoad, which looks up Java classes and with that initializes them. When the first class used was not a native object, as with Logging or VideoBufferConverter, this initialized NativeObject during the load, whose static initializer called loadLibrary again on the same thread. The lock of the synchronized method is reentrant, the name was not marked yet, and a second copy was extracted and loaded into the process. On macOS the Objective-C runtime warned of every class implemented in both copies. The native methods and state were split between the two copies: with leak detection on and Logging used first, objects went unwatched in about one run of three. The library is now marked as being loaded before it is loaded. A nested call returns at once, and a failed load leaves the library unmarked, so that a later call tries again. NativeObject turns the leak detector on through the new NativeLoader.whenLoaded, which waits for the outer load to finish, since the JVM finds no native method of a library before its JNI_OnLoad has returned.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
When
Logging(orVideoBufferConverter) was the first webrtc class an application used, the native library was extracted and loaded twice into the same JVM. On macOS this shows as 42objc[...]: Class ... is implemented in both ...warnings naming twowebrtc-java-natives-*directories.Cause
NativeLoader.loadLibraryadded a library to its loaded set only after loading it. Loadingwebrtc-javaruns itsJNI_OnLoad, which looks up Java classes and so initializes them. That initializedNativeObjectduring the load, and its static initializer calledloadLibrary("webrtc-java")again on the same thread. The method's lock is reentrant and the name was not in the set yet, so a second copy was loaded. A jdb stack of the nested call showsNativeObject.<clinit>running insideNativeLibraries.loadof the outerSystem.loadfromLogging.<clinit>. WithPeerConnectionFactoryfirst,NativeObjectis itself the outer loader, which is why that order was clean.This was not only noise: native methods and state were split between the two copies. With
-Dwebrtc.java.leak.detection=trueandLoggingused first, the existingLeakDetectionUsescenario failed in 4 of 12 runs ("an object to watch is not watched").Change
NativeLoader.loadOncemarks a library as being loaded before loading it. A nested call on the same thread returns at once. A failed load leaves the library unmarked, so a later call tries again, and the original exception still reaches the caller.JNI_OnLoadhas returned.NativeObject.<clinit>calledLeakDetector.initialize()(the nativeenableNative()) right after loading, which would then fail the whole load withUnsatisfiedLinkError. The new package-privateNativeLoader.whenLoaded(name, action)runs an action once the outer load has finished, andNativeObjectuses it. None of the other 17 static initializers that load the library call into it afterwards.