From 05459356b52b8107791f738f53fab50f4652dad1 Mon Sep 17 00:00:00 2001 From: Alex Andres Date: Thu, 12 Nov 2026 18:33:16 +0100 Subject: [PATCH] fix: load the native library only once when Logging is used first 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. --- .../onvoid/webrtc/internal/NativeLoader.java | 71 +++++++++++++++++-- .../onvoid/webrtc/internal/NativeObject.java | 4 +- .../webrtc/internal/LeakDetectionUse.java | 12 ++++ .../webrtc/internal/LeakDetectorTests.java | 8 +++ .../webrtc/internal/NativeLoaderTests.java | 69 +++++++++++++++++- .../internal/StandaloneLoadingTests.java | 6 ++ .../onvoid/webrtc/internal/StandaloneUse.java | 7 ++ 7 files changed, 170 insertions(+), 7 deletions(-) diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeLoader.java b/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeLoader.java index 5cb7edbe0..32ea065b7 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeLoader.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeLoader.java @@ -31,9 +31,11 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; +import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Locale; +import java.util.Map; import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.regex.Pattern; @@ -105,6 +107,12 @@ public class NativeLoader { private static final Set LOADED_LIB_SET = new HashSet<>(); + /** The libraries the thread that holds the lock of this class is loading. */ + private static final Set LOADING_LIB_SET = new HashSet<>(); + + /** What is to run once a library is loaded, by the name of the library. */ + private static final Map> WHEN_LOADED = new HashMap<>(); + /** Whether this process has swept up after earlier ones yet. */ private static boolean swept; @@ -160,15 +168,70 @@ public static void loadLibrary(final String libName) throws Exception { */ public static synchronized void loadLibrary(final String libName, final String... dependencies) throws Exception { - if (LOADED_LIB_SET.contains(libName)) { + loadOnce(libName, () -> load(libName + "-" + platform(), + Arrays.asList(dependencies), Locations.fromSystem(), + NativeLoader::classPathResource, Linker.SYSTEM)); + } + + /** + * Runs the given loader, unless the library is loaded already or is being + * loaded by this thread, and then what is to run once it is loaded. + *

+ * Loading a library runs its {@code JNI_OnLoad}, which looks up Java + * classes and with that initializes them. A class that loads the same + * library in its static initializer then calls back in on this thread, + * which the lock of this class lets through. That nested call must not + * load the library again, which would put a second copy of it into the + * process: it returns at once, before the library is loaded, and the outer + * call finishes loading it. Code that needs the library at that point + * goes through {@link #whenLoaded(String, Runnable)}. A loader that fails + * leaves the library not loaded, so that a later call tries anew. + * + * @param libName The name of the library. + * @param loader Loads the library. + */ + static synchronized void loadOnce(String libName, Runnable loader) { + if (LOADED_LIB_SET.contains(libName) || !LOADING_LIB_SET.add(libName)) { return; } - load(libName + "-" + platform(), - Arrays.asList(dependencies), Locations.fromSystem(), - NativeLoader::classPathResource, Linker.SYSTEM); + try { + loader.run(); + } + finally { + LOADING_LIB_SET.remove(libName); + } LOADED_LIB_SET.add(libName); + + List actions = WHEN_LOADED.remove(libName); + + if (actions != null) { + for (Runnable action : actions) { + action.run(); + } + } + } + + /** + * Runs the given action now if the library is loaded, or else once it is. + * A static initializer that loads a library and then calls one of its + * native methods makes that call through this. When the loading of the + * library is what runs the initializer, the library is not loaded yet, see + * {@link #loadOnce(String, Runnable)}, and the JVM finds none of its + * native methods before its {@code JNI_OnLoad} has returned. + * + * @param libName The name of the library, as given to {@link + * #loadLibrary(String, String...)}. + * @param action What needs the library. + */ + static synchronized void whenLoaded(String libName, Runnable action) { + if (LOADED_LIB_SET.contains(libName)) { + action.run(); + } + else { + WHEN_LOADED.computeIfAbsent(libName, name -> new ArrayList<>()).add(action); + } } /** diff --git a/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeObject.java b/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeObject.java index 918ef8913..b4562595a 100644 --- a/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeObject.java +++ b/webrtc/src/main/java/dev/onvoid/webrtc/internal/NativeObject.java @@ -42,7 +42,9 @@ public abstract class NativeObject { throw new RuntimeException("Load library 'webrtc-java' failed", e); } - LeakDetector.initialize(); + // When loading the library is what initializes this class, the call + // above returns before the library is loaded. + NativeLoader.whenLoaded("webrtc-java", LeakDetector::initialize); } /** diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectionUse.java b/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectionUse.java index 50fd50cd5..6eb0dd57c 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectionUse.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectionUse.java @@ -20,6 +20,7 @@ import dev.onvoid.webrtc.RTCConfiguration; import dev.onvoid.webrtc.RTCPeerConnection; import dev.onvoid.webrtc.RTCRtpSender; +import dev.onvoid.webrtc.logging.Logging; import dev.onvoid.webrtc.media.MediaStreamTrack; import dev.onvoid.webrtc.media.audio.AudioConverter; import dev.onvoid.webrtc.media.audio.AudioDeviceModule; @@ -44,7 +45,18 @@ */ public final class LeakDetectionUse { + /** + * The system property that has {@link Logging} load the library, which + * then initializes {@link NativeObject} while it is being loaded. + */ + static final String LOGGING_FIRST_PROPERTY = "test.leak.logging.first"; + + public static void main(String[] args) { + if (Boolean.getBoolean(LOGGING_FIRST_PROPERTY)) { + Logging.logTimestamps(false); + } + List reports = Collections.synchronizedList(new ArrayList<>()); LeakDetector.reporter = reports::add; diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectorTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectorTests.java index bdec801ca..7b6ab6ba9 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectorTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/internal/LeakDetectorTests.java @@ -86,6 +86,14 @@ void forgottenObjectsReported() throws Exception { assertEquals(0, result.exitValue, result.output); } + @Test + void reportedWithLoggingFirst() throws Exception { + Result result = run("-D" + LeakDetector.PROPERTY + "=true", + "-D" + LeakDetectionUse.LOGGING_FIRST_PROPERTY + "=true"); + + assertEquals(0, result.exitValue, result.output); + } + @Test void traceNamesWhereCreated() throws Exception { Result result = run("-D" + LeakDetector.PROPERTY + "=true", "-D" + LeakDetector.TRACE_PROPERTY + "=true"); diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/internal/NativeLoaderTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/internal/NativeLoaderTests.java index b6bda84e9..6d61c62cd 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/internal/NativeLoaderTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/internal/NativeLoaderTests.java @@ -18,6 +18,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assumptions.assumeFalse; @@ -37,6 +38,7 @@ import java.util.Arrays; import java.util.Collections; import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; import java.util.stream.Stream; import dev.onvoid.webrtc.TestBase; @@ -45,8 +47,8 @@ import org.junit.jupiter.api.io.TempDir; /** - * Covers where native libraries are looked for and the cleanup of extracted - * ones. What is swept is an isolated directory set up here, never the real + * Covers that a native library is loaded once, where native libraries are + * looked for and the cleanup of extracted ones. What is swept is an isolated directory set up here, never the real * temporary directory, and the time a sweep runs at is given rather than read * from the clock. The lookup is driven with a linker that only records what it * is handed; {@link LibraryLocationTests} loads the real library. @@ -63,6 +65,8 @@ class NativeLoaderTests extends TestBase { private static final String LIB_FILE = System.mapLibraryName(LIB); + private static final AtomicInteger LIB_COUNTER = new AtomicInteger(); + @TempDir Path root; @@ -137,6 +141,59 @@ void holdsOwnDirectory() throws Exception { assertTrue(NativeLoader.isInUse(directory)); } + @Test + void nestedLoadLoadsOnce() { + String lib = freshLib(); + AtomicInteger loads = new AtomicInteger(); + + NativeLoader.loadOnce(lib, () -> { + loads.incrementAndGet(); + + // As when JNI_OnLoad initializes a class that loads the library too. + NativeLoader.loadOnce(lib, loads::incrementAndGet); + }); + NativeLoader.loadOnce(lib, loads::incrementAndGet); + + assertEquals(1, loads.get()); + } + + @Test + void whenLoadedAwaitsLoad() { + String lib = freshLib(); + List events = new ArrayList<>(); + + NativeLoader.loadOnce(lib, () -> { + // Called while the library is being loaded. + NativeLoader.whenLoaded(lib, () -> events.add("action")); + events.add("loaded"); + }); + NativeLoader.whenLoaded(lib, () -> events.add("later action")); + + assertEquals(Arrays.asList("loaded", "action", "later action"), events); + } + + @Test + void failedLoadRetried() { + String lib = freshLib(); + List events = new ArrayList<>(); + UnsatisfiedLinkError failure = new UnsatisfiedLinkError("failed"); + + UnsatisfiedLinkError error = assertThrows(UnsatisfiedLinkError.class, + () -> NativeLoader.loadOnce(lib, () -> { + NativeLoader.whenLoaded(lib, () -> events.add("action")); + + throw failure; + })); + + assertSame(failure, error); + assertTrue(events.isEmpty()); + + // The action waits for the load that succeeds. + NativeLoader.loadOnce(lib, () -> events.add("loaded")); + + assertEquals(Arrays.asList("loaded", "action"), events); + } + @Test void libraryDirWins() throws Exception { Path dir = Files.createDirectory(root.resolve("installed")); @@ -267,6 +324,14 @@ void detectsMusl() throws Exception { assertFalse(NativeLoader.failureMessage(LIB_FILE, Collections.emptyList(), false).contains("musl")); } + /** + * A library name no test has used yet, since a library stays loaded for + * as long as the JVM runs. + */ + private static String freshLib() { + return LIB + "-" + LIB_COUNTER.incrementAndGet(); + } + private NativeLoader.Locations locations(Path libraryDir, List searchPath) { return new NativeLoader.Locations(libraryDir, root.resolve("extract"), searchPath); } diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneLoadingTests.java b/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneLoadingTests.java index 2a7e81089..39ee9e14c 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneLoadingTests.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneLoadingTests.java @@ -17,6 +17,7 @@ package dev.onvoid.webrtc.internal; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; import java.io.ByteArrayOutputStream; @@ -80,6 +81,11 @@ private static void runAlone(String name) throws Exception { String log = new String(output.toByteArray(), StandardCharsets.UTF_8); assertEquals(0, process.exitValue(), () -> name + " failed in a fresh JVM:\n" + log); + + // A second copy of the library shows on macOS, where the Objective-C + // runtime warns of each class that both copies implement. + assertFalse(log.contains("is implemented in both"), + () -> name + " loaded the native library twice:\n" + log); } /** diff --git a/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneUse.java b/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneUse.java index a042a27c0..4d438849d 100644 --- a/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneUse.java +++ b/webrtc/src/test/java/dev/onvoid/webrtc/internal/StandaloneUse.java @@ -18,6 +18,7 @@ import java.nio.ByteBuffer; +import dev.onvoid.webrtc.logging.Logging; import dev.onvoid.webrtc.media.FourCC; import dev.onvoid.webrtc.media.audio.AudioConverter; import dev.onvoid.webrtc.media.audio.AudioResampler; @@ -41,6 +42,7 @@ public final class StandaloneUse { "AudioResampler", "CustomAudioSource", "CustomVideoSource", + "Logging", "NativeI420Buffer", "VideoBufferConverter", "VideoCapture" @@ -61,6 +63,11 @@ public static void main(String[] args) throws Exception { case "CustomVideoSource": new CustomVideoSource().dispose(); break; + case "Logging": + // Not a native object: the library initializes NativeObject + // while it is being loaded. + Logging.logTimestamps(true); + break; case "NativeI420Buffer": NativeI420Buffer.allocate(2, 2).release(); break;