Skip to content

Commit 858c33f

Browse files
authored
Merge branch 'main' into feat/attach-anr-dump-meta
2 parents a176e4d + 6259a9f commit 858c33f

File tree

2 files changed

+41
-14
lines changed

2 files changed

+41
-14
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
- Deprecate `enableTracing` option ([#3777](https://github.com/getsentry/sentry-java/pull/3777))
1212
- Vendor `java.util.Random` and replace `java.security.SecureRandom` usages ([#3783](https://github.com/getsentry/sentry-java/pull/3783))
1313
- Fix potential ANRs due to NDK scope sync ([#3754](https://github.com/getsentry/sentry-java/pull/3754))
14+
- Fix potential ANRs due to NDK System.loadLibrary calls ([#3670](https://github.com/getsentry/sentry-java/pull/3670))
1415

1516
## 7.15.0
1617

sentry-android-ndk/src/main/java/io/sentry/android/ndk/SentryNdk.java

+40-14
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,40 @@
11
package io.sentry.android.ndk;
22

33
import io.sentry.android.core.SentryAndroidOptions;
4+
import java.util.concurrent.CountDownLatch;
5+
import java.util.concurrent.TimeUnit;
46
import org.jetbrains.annotations.ApiStatus;
57
import org.jetbrains.annotations.NotNull;
68

79
@ApiStatus.Internal
810
public final class SentryNdk {
911

12+
private static final @NotNull CountDownLatch loadLibraryLatch = new CountDownLatch(1);
13+
1014
private SentryNdk() {}
1115

1216
static {
13-
// On older Android versions, it was necessary to manually call "`System.loadLibrary` on all
14-
// transitive dependencies before loading [the] main library."
15-
// The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
16-
// See
17-
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
18-
System.loadLibrary("log");
19-
System.loadLibrary("sentry");
20-
System.loadLibrary("sentry-android");
17+
new Thread(
18+
() -> {
19+
// On older Android versions, it was necessary to manually call "`System.loadLibrary`
20+
// on all
21+
// transitive dependencies before loading [the] main library."
22+
// The dependencies of `libsentry.so` are currently `lib{c,m,dl,log}.so`.
23+
// See
24+
// https://android.googlesource.com/platform/bionic/+/master/android-changes-for-ndk-developers.md#changes-to-library-dependency-resolution
25+
try {
26+
System.loadLibrary("log");
27+
System.loadLibrary("sentry");
28+
System.loadLibrary("sentry-android");
29+
} catch (Throwable t) {
30+
// ignored
31+
// if loadLibrary() fails, the later init() will throw an exception anyway
32+
} finally {
33+
loadLibraryLatch.countDown();
34+
}
35+
},
36+
"SentryNdkLoadLibs")
37+
.start();
2138
}
2239

2340
private static native void initSentryNative(@NotNull final SentryAndroidOptions options);
@@ -31,14 +48,23 @@ private SentryNdk() {}
3148
*/
3249
public static void init(@NotNull final SentryAndroidOptions options) {
3350
SentryNdkUtil.addPackage(options.getSdkVersion());
34-
initSentryNative(options);
51+
try {
52+
if (loadLibraryLatch.await(2000, TimeUnit.MILLISECONDS)) {
53+
initSentryNative(options);
3554

36-
// only add scope sync observer if the scope sync is enabled.
37-
if (options.isEnableScopeSync()) {
38-
options.addScopeObserver(new NdkScopeObserver(options));
39-
}
55+
// only add scope sync observer if the scope sync is enabled.
56+
if (options.isEnableScopeSync()) {
57+
options.addScopeObserver(new NdkScopeObserver(options));
58+
}
4059

41-
options.setDebugImagesLoader(new DebugImagesLoader(options, new NativeModuleListLoader()));
60+
options.setDebugImagesLoader(new DebugImagesLoader(options, new NativeModuleListLoader()));
61+
} else {
62+
throw new IllegalStateException("Timeout waiting for Sentry NDK library to load");
63+
}
64+
} catch (InterruptedException e) {
65+
throw new IllegalStateException(
66+
"Thread interrupted while waiting for NDK libs to be loaded", e);
67+
}
4268
}
4369

4470
/** Closes the NDK integration */

0 commit comments

Comments
 (0)