Skip to content

fix: load the native library only once when Logging is used first - #593

Merged
devopvoid merged 1 commit into
mainfrom
fix/native-library-loaded-twice
Nov 12, 2026
Merged

devopvoid merged 1 commit into
mainfrom
fix/native-library-loaded-twice

Conversation

@devopvoid

@devopvoid devopvoid commented Nov 12, 2026 •

Copy link
Copy Markdown
Owner

When Logging (or VideoBufferConverter) 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 42 objc[...]: Class ... is implemented in both ... warnings naming two webrtc-java-natives-* directories.

Cause

NativeLoader.loadLibrary added a library to its loaded set only after loading it. Loading webrtc-java runs its JNI_OnLoad, which looks up Java classes and so initializes them. That initialized NativeObject during the load, and its static initializer called loadLibrary("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 shows NativeObject.<clinit> running inside NativeLibraries.load of the outer System.load from Logging.<clinit>. With PeerConnectionFactory first, NativeObject is 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=true and Logging used first, the existing LeakDetectionUse scenario failed in 4 of 12 runs ("an object to watch is not watched").

Change

  • NativeLoader.loadOnce marks 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.
  • The nested call now returns before the library is loaded, and the JVM finds none of its native methods until JNI_OnLoad has returned. NativeObject.<clinit> called LeakDetector.initialize() (the native enableNative()) right after loading, which would then fail the whole load with UnsatisfiedLinkError. The new package-private NativeLoader.whenLoaded(name, action) runs an action once the outer load has finished, and NativeObject uses it. None of the other 17 static initializers that load the library call into it afterwards.

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.
@devopvoid
devopvoid merged commit 9411983 into main Nov 12, 2026
24 checks passed
@devopvoid
devopvoid deleted the fix/native-library-loaded-twice branch November 12, 2026 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant