Skip to content

Commit b57211c

Browse files
hannojgmarkushisupervacuus
authored
feat(ndk): expose init return code (#1430)
* ndk: expose init return code from sentry_init * changelog: note SentryNdk.init return code * formatting * Address PR feedback * fix up CHANGELOG.md * release ready fixup --------- Co-authored-by: Markus Hintersteiner <markus.hintersteiner@sentry.io> Co-authored-by: Mischan Toosarani-Hausberger <mischan@abovevacant.com>
1 parent eecc7fa commit b57211c

4 files changed

Lines changed: 67 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
11
# Changelog
22

3-
## 0.12.8
4-
5-
**Fixes**:
6-
7-
- Fix deadlock when re-initializing the SDK while logs or metrics threads are mid-flush. ([#1518](https://github.com/getsentry/sentry-native/pull/1518))
8-
9-
## 0.12.7
3+
## Unreleased
104

115
**Breaking**:
126

137
- inproc: since we split `inproc` into signal-handler/UEF part and a separate handler thread, `before_send` and `on_crash` could be called from other threads than the one that crashed. While this was never part of the contract, if your code relies on this, it will no longer work. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
8+
- Android NDK: `SentryNdk.init(NdkOptions)` now throws an `Exception` if init fails (non-zero return code) rather than silently swallowing the error. ([#1430](https://github.com/getsentry/sentry-native/pull/1430))
149

1510
**Features**:
1611

17-
- Add new offline caching options to persist envelopes locally: `sentry_options_set_cache_keep`, `sentry_options_set_cache_max_items`, `sentry_options_set_cache_max_size`, and `sentry_options_set_cache_max_age`. ([#1490](https://github.com/getsentry/sentry-native/pull/1490), [#1493](https://github.com/getsentry/sentry-native/pull/1493))
18-
- Add support for `abort()` in the `inproc` backend on Windows. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
12+
- Add support for `abort()` in the `inproc` backend on Windows. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
1913

2014
**Fixes**:
2115

22-
- Remove spurious decref in `sentry_capture_user_feedback()` ([#1510](https://github.com/getsentry/sentry-native/pull/1510))
23-
- Prevent double-decref of event in envelope add functions ([#1511](https://github.com/getsentry/sentry-native/pull/1511))
2416
- Make the signal-handler synchronization fully atomic to fix rare race scenarios. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
2517
- Reintroduce an FP-based stack-walker for macOS that can start from a user context. This also makes `inproc` backend functional again on macOS 13+. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
2618
- Split the `inproc` signal handler (and UEF on Windows) into a safe handler part and an "unsafe" handler thread. This minimizes exposure to undefined behavior inside the signal handler. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
@@ -31,6 +23,27 @@
3123
- Introduce PAC tests for `arm64e` on macOS. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
3224
- For Linux, the SDK now has a vendored "nognu" `libunwind` as the default stack walker and links it statically, but with PIC enabled so it can be used in PIE executables. ([#1446](https://github.com/getsentry/sentry-native/pull/1446))
3325

26+
**Thank you**:
27+
28+
- [hannojg](https://github.com/hannojg)
29+
30+
## 0.12.8
31+
32+
**Fixes**:
33+
34+
- Fix deadlock when re-initializing the SDK while logs or metrics threads are mid-flush. ([#1518](https://github.com/getsentry/sentry-native/pull/1518))
35+
36+
## 0.12.7
37+
38+
**Features**:
39+
40+
- Add new offline caching options to persist envelopes locally: `sentry_options_set_cache_keep`, `sentry_options_set_cache_max_items`, `sentry_options_set_cache_max_size`, and `sentry_options_set_cache_max_age`. ([#1490](https://github.com/getsentry/sentry-native/pull/1490), [#1493](https://github.com/getsentry/sentry-native/pull/1493))
41+
42+
**Fixes**:
43+
44+
- Remove spurious decref in `sentry_capture_user_feedback()` ([#1510](https://github.com/getsentry/sentry-native/pull/1510))
45+
- Prevent double-decref of event in envelope add functions ([#1511](https://github.com/getsentry/sentry-native/pull/1511))
46+
3447
## 0.12.6
3548

3649
**Features**:

ndk/lib/src/androidTest/java/io/sentry/ndk/SentryNdkTest.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,30 @@ public void shutdownDoesNotFail() throws IOException {
6565
// it does not crash
6666
}
6767

68+
@Test(expected = IllegalStateException.class)
69+
public void initThrowsException() throws IOException {
70+
final TemporaryFolder temporaryFolder = TemporaryFolder.builder().build();
71+
temporaryFolder.create();
72+
final File outboxPath = temporaryFolder.newFolder("outboxPath");
73+
74+
//noinspection DataFlowIssue
75+
final NdkOptions options =
76+
new NdkOptions(
77+
null,
78+
true,
79+
outboxPath.getAbsolutePath(),
80+
"1.0.0",
81+
"production",
82+
"dist",
83+
100,
84+
"io.sentry.ndk");
85+
86+
// when initialized with a NULL dsn
87+
SentryNdk.init(options);
88+
89+
// then it does crash
90+
}
91+
6892
@Test
6993
public void messageCaught() throws IOException {
7094
final TemporaryFolder temporaryFolder = TemporaryFolder.builder().build();

ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,18 +10,31 @@ public final class SentryNdk {
1010

1111
private SentryNdk() {}
1212

13-
private static native void initSentryNative(@NotNull final NdkOptions options);
13+
/**
14+
* Initializes sentry-native and returns 0 on success, non-zero on failure.
15+
*
16+
* @return -1 if an JNI or options configuration issue occurred, 1 if sentry native itself failed
17+
* to initialize
18+
*/
19+
private static native int initSentryNative(@NotNull final NdkOptions options);
1420

1521
private static native void shutdown();
1622

1723
/**
1824
* Init the NDK integration
1925
*
2026
* @param options the SentryAndroidOptions
27+
* @throws IllegalStateException if sentry-native couldn't be initialized
2128
*/
2229
public static void init(@NotNull final NdkOptions options) {
2330
loadNativeLibraries();
24-
initSentryNative(options);
31+
final int returnCode = initSentryNative(options);
32+
if (returnCode > 0) {
33+
throw new IllegalStateException(
34+
"A sentry-native internal init error occurred, please check the logs for more details.");
35+
} else if (returnCode < 0) {
36+
throw new IllegalStateException("A sentry-native setup failure occurred");
37+
}
2538
}
2639

2740
/** Closes the NDK integration */

ndk/lib/src/main/jni/sentry.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ static void send_envelope(sentry_envelope_t *envelope, void *data) {
247247
sentry_envelope_free(envelope);
248248
}
249249

250-
JNIEXPORT void JNICALL
250+
JNIEXPORT jint JNICALL
251251
Java_io_sentry_ndk_SentryNdk_initSentryNative(
252252
JNIEnv *env,
253253
jclass cls,
@@ -355,8 +355,8 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative(
355355
jfloat traces_sample_rate = (jfloat) (*env)->CallFloatMethod(env, sentry_ndk_options, traces_sample_rate_mid);
356356
sentry_options_set_traces_sample_rate(options, traces_sample_rate);
357357

358-
sentry_init(options);
359-
return;
358+
int rv = sentry_init(options);
359+
return (jint) rv;
360360

361361
fail:
362362
if (!transport_owns_path) {
@@ -366,6 +366,7 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative(
366366
sentry_transport_free(transport);
367367
}
368368
sentry_options_free(options);
369+
return (jint) -1;
369370
}
370371

371372
JNIEXPORT void JNICALL

0 commit comments

Comments
 (0)