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 5cb7edbe..32ea065b 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 918ef891..b4562595 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 50fd50cd..6eb0dd57 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 bdec801c..7b6ab6ba 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 b6bda84e..6d61c62c 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 2a7e8108..39ee9e14 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 a042a27c..4d438849 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;