Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
**Breaking changes**:

- If you use a narrow string path interface (for instance, `sentry_options_set_database_path()`) on _Windows_ rather than one of the wide string variants (`sentry_options_set_database_pathw()`), then the expected encoding is now UTF-8. ([#1413](https://github.com/getsentry/sentry-native/pull/1413))
- Android NDK: `SentryNdk.init(NdkOptions)` now returns an `int` (0 success, non-zero failure) instead of `void`, exposing the result of `sentry_init()`. ([#1430](https://github.com/getsentry/sentry-native/pull/1430))

**Fixes**:

Expand Down
9 changes: 6 additions & 3 deletions ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ public final class SentryNdk {

private SentryNdk() {}

private static native void initSentryNative(@NotNull final NdkOptions options);
/**
* Initializes sentry-native and returns 0 on success, non-zero on failure.
*/
private static native int initSentryNative(@NotNull final NdkOptions options);

private static native void shutdown();

Expand All @@ -19,9 +22,9 @@ private SentryNdk() {}
*
* @param options the SentryAndroidOptions
*/
public static void init(@NotNull final NdkOptions options) {
public static int init(@NotNull final NdkOptions options) {
loadNativeLibraries();
initSentryNative(options);
return initSentryNative(options);
}
Comment on lines +25 to 28
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should keep using void here and throw an exception within init if the error code is non-0. This keeps API compatibility and ties the error code handling closer to the actual C code.

We also don't need any extra handling within sentry-java then, as it's covered by this try-catch here.

Suggested change
public static int init(@NotNull final NdkOptions options) {
loadNativeLibraries();
initSentryNative(options);
return initSentryNative(options);
}
public static void init(@NotNull final NdkOptions options) {
loadNativeLibraries();
final int returnCode = initSentryNative(options);
if (returnCode != 0) {
throw new IllegalStateException("Failed to initialize sentry-native, return code: " + returnCode);
}
}

Copy link
Collaborator

@supervacuus supervacuus Nov 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even go one step further here and check whether the return code was 1 (in which case there is some native-internal issue which could be resolved via logs and there is no need to render a return code) and having an enum range in the negative return code range, that results in an explanatory expection message for each particular return code.

Even if we don't go that far, we should at least differentiate between > 0 and < 0 returnCode exceptions, to clarify where the error happened (in the JNI layer or native proper). Since the primary failure mode in the JNI layer is about allocation, most exceptions from there would not be actionable to client code. However, having a basic differentiation between the source of the exception, would still be useful.


Comment on lines +25 to 29
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The public init() method should document the return value semantics for callers. Add a @return javadoc comment explaining that 0 indicates success and non-zero indicates failure, similar to the documentation added to the private native method.
Severity: MEDIUM

💡 Suggested Fix

Suggested change
public static int init(@NotNull final NdkOptions options) {
loadNativeLibraries();
initSentryNative(options);
return initSentryNative(options);
}
/**
* Initializes sentry-native and returns 0 on success, non-zero on failure.
*
* @param options the NdkOptions
* @return 0 on success, non-zero on failure
*/
public static int init(@NotNull final NdkOptions options) {

Did we get this right? 👍 / 👎 to inform future reviews.

/** Closes the NDK integration */
Expand Down
7 changes: 4 additions & 3 deletions ndk/lib/src/main/jni/sentry.c
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ static void send_envelope(sentry_envelope_t *envelope, void *data) {
sentry_envelope_free(envelope);
}

JNIEXPORT void JNICALL
JNIEXPORT jint JNICALL
Java_io_sentry_ndk_SentryNdk_initSentryNative(
JNIEnv *env,
jclass cls,
Expand Down Expand Up @@ -355,8 +355,8 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative(
jfloat traces_sample_rate = (jfloat) (*env)->CallFloatMethod(env, sentry_ndk_options, traces_sample_rate_mid);
sentry_options_set_traces_sample_rate(options, traces_sample_rate);

sentry_init(options);
return;
int rv = sentry_init(options);
return (jint) rv;

fail:
if (!transport_owns_path) {
Expand All @@ -366,6 +366,7 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative(
sentry_transport_free(transport);
}
sentry_options_free(options);
return (jint) -1;
Comment on lines 366 to +369
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The error handling returns -1 for general initialization failures but returns the actual sentry_init() result on success. This creates inconsistent error code semantics. Consider either: (1) documenting the different error code meanings (what specific non-zero values from sentry_init() mean vs -1 for setup failures), or (2) normalizing the return value to always return -1 for any failure and only 0 for success. This will make error handling more predictable for Java callers.
Severity: MEDIUM

🤖 Prompt for AI Agent

Review the code at the location below. A potential bug has been identified by an AI
agent.
Verify if this is a real issue. If it is, propose a fix; if not, explain why it's not
valid.

Location: ndk/lib/src/main/jni/sentry.c#L358-L369

Potential issue: The error handling returns -1 for general initialization failures but
returns the actual sentry_init() result on success. This creates inconsistent error code
semantics. Consider either: (1) documenting the different error code meanings (what
specific non-zero values from sentry_init() mean vs -1 for setup failures), or (2)
normalizing the return value to always return -1 for any failure and only 0 for success.
This will make error handling more predictable for Java callers.

Did we get this right? 👍 / 👎 to inform future reviews.

}

JNIEXPORT void JNICALL
Expand Down
Loading