From 990b2be35e9b8ec1373805e46cac4d8dbe653b49 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 13:12:10 +0100 Subject: [PATCH 01/14] fix(android): Fall back to a file for crash IPC without memfd `memfd_create` needs Linux 3.17, so creating the crash service's shared memory failed on older kernels, and the SDK disabled native crash reporting. When `memfd_create` fails, create an unlinked file in the database directory instead. Preallocate it with `posix_fallocate()`, because a sparse file raises `SIGBUS` on write when the disk is full. Java passes the directory to the new `SentryNdk.createCrashShm(String)` overload, and `createCrashShm()` keeps using memfd only. --- ndk/lib/api/sentry-native-ndk.api | 1 + .../main/java/io/sentry/ndk/SentryNdk.java | 12 ++++++++-- ndk/lib/src/main/jni/sentry.c | 22 ++++++++++++++---- src/backends/native/sentry_crash_ipc.c | 23 +++++++++++++++++++ src/backends/native/sentry_crash_ipc.h | 8 +++++++ src/backends/sentry_backend_native.c | 9 ++++++-- 6 files changed, 66 insertions(+), 9 deletions(-) diff --git a/ndk/lib/api/sentry-native-ndk.api b/ndk/lib/api/sentry-native-ndk.api index 072749ecb6..a3b63da1e0 100644 --- a/ndk/lib/api/sentry-native-ndk.api +++ b/ndk/lib/api/sentry-native-ndk.api @@ -110,6 +110,7 @@ public final class io/sentry/ndk/NdkOptions { public final class io/sentry/ndk/SentryNdk { public static fun close ()V public static fun createCrashShm ()I + public static fun createCrashShm (Ljava/lang/String;)I public static fun dupCrashShmFd ()I public static fun init (Lio/sentry/ndk/NdkOptions;)V public static fun initCrashDaemon (IIII)V diff --git a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java index 5281676f3f..94d31b5b55 100644 --- a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java +++ b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java @@ -21,7 +21,7 @@ private SentryNdk() {} */ private static native int initSentryNative(@NotNull final NdkOptions options); - private static native int nativeCreateCrashShm(); + private static native int nativeCreateCrashShm(@Nullable String databasePath); private static native void nativeInitCrashDaemon( int shmFd, int notifyFd, int readyFd, int messageFd); @@ -77,8 +77,16 @@ public static void init(@NotNull final NdkOptions options) { /** Creates the shared memory of the crash IPC, or returns -1 if that fails. Needs API 30+. */ public static int createCrashShm() { + return createCrashShm(null); + } + + /** + * Creates the shared memory of the crash IPC, or returns -1 if that fails. Falls back to a file + * in the database directory on kernels without memfd. + */ + public static int createCrashShm(@Nullable String databasePath) { loadNativeLibraries(); - return nativeCreateCrashShm(); + return nativeCreateCrashShm(databasePath); } public static void initCrashDaemon(int shmFd, int notifyFd, int readyFd, int messageFd) { diff --git a/ndk/lib/src/main/jni/sentry.c b/ndk/lib/src/main/jni/sentry.c index 92d8d9355a..38d1f48bab 100644 --- a/ndk/lib/src/main/jni/sentry.c +++ b/ndk/lib/src/main/jni/sentry.c @@ -11,7 +11,8 @@ #include "sentry_http.h" -extern int sentry_android_crash_daemon_create_shm(void) __attribute__((weak)); +extern int sentry_android_crash_daemon_create_shm(const char *database_path) + __attribute__((weak)); extern void sentry_android_crash_daemon_init(int shm_fd, int notify_fd, int ready_fd, int message_fd) __attribute__((weak)); typedef struct sentry_crash_daemon_s sentry_crash_daemon_t; @@ -568,10 +569,21 @@ Java_io_sentry_ndk_SentryNdk_initSentryNative( } JNIEXPORT jint JNICALL -Java_io_sentry_ndk_SentryNdk_nativeCreateCrashShm(JNIEnv *env, jclass cls) { - return sentry_android_crash_daemon_create_shm - ? (jint) sentry_android_crash_daemon_create_shm() - : -1; +Java_io_sentry_ndk_SentryNdk_nativeCreateCrashShm( + JNIEnv *env, + jclass cls, + jstring database_path) { + if (!sentry_android_crash_daemon_create_shm) { + return -1; + } + const char *path_chars = database_path + ? (*env)->GetStringUTFChars(env, database_path, 0) + : NULL; + int fd = sentry_android_crash_daemon_create_shm(path_chars); + if (path_chars) { + (*env)->ReleaseStringUTFChars(env, database_path, path_chars); + } + return (jint) fd; } JNIEXPORT void JNICALL diff --git a/src/backends/native/sentry_crash_ipc.c b/src/backends/native/sentry_crash_ipc.c index 4d2aa3ff52..34591cc7f6 100644 --- a/src/backends/native/sentry_crash_ipc.c +++ b/src/backends/native/sentry_crash_ipc.c @@ -163,6 +163,7 @@ sentry__crash_ipc_message_decode( # include # include # include +# include # include # include # include @@ -221,6 +222,28 @@ init_unlock(sentry_crash_ipc_t *ipc) sentry__mutex_unlock(ipc->init_mutex); } } + +int +sentry__crash_ipc_create_shm_file(const char *dir) +{ + char path[SENTRY_CRASH_MAX_PATH]; + int len = snprintf(path, sizeof(path), "%s/.sentry-shm-XXXXXX", dir); + if (len < 0 || (size_t)len >= sizeof(path)) { + return -1; + } + int fd = mkstemp(path); + if (fd < 0) { + return -1; + } + unlink(path); + // a sparse file raises SIGBUS on write when the disk is full + if (fcntl(fd, F_SETFD, FD_CLOEXEC) < 0 + || posix_fallocate(fd, 0, SENTRY_CRASH_SHM_SIZE) != 0) { + close(fd); + return -1; + } + return fd; +} # else static int shm_create(sentry_crash_ipc_t *ipc) diff --git a/src/backends/native/sentry_crash_ipc.h b/src/backends/native/sentry_crash_ipc.h index 56f13cb211..f30a687d59 100644 --- a/src/backends/native/sentry_crash_ipc.h +++ b/src/backends/native/sentry_crash_ipc.h @@ -215,6 +215,14 @@ sentry_crash_ipc_t *sentry__crash_ipc_init_app(void); int sentry__crash_ipc_create_memfd(void); #endif +#if defined(SENTRY_PLATFORM_ANDROID) +/** + * Creates shared memory backed by an unlinked, preallocated file in `dir`, for + * kernels without memfd. Returns -1 on failure. + */ +int sentry__crash_ipc_create_shm_file(const char *dir); +#endif + /** * Initialize IPC for daemon process. * Attaches to existing shared memory created by app. diff --git a/src/backends/sentry_backend_native.c b/src/backends/sentry_backend_native.c index 5399984858..d2ad14ca67 100644 --- a/src/backends/sentry_backend_native.c +++ b/src/backends/sentry_backend_native.c @@ -77,9 +77,14 @@ static android_crash_daemon_config_t g_android_crash_daemon_config = { false, -1, -1, -1, -1 }; __attribute__((visibility("default"))) int -sentry_android_crash_daemon_create_shm(void) +sentry_android_crash_daemon_create_shm(const char *database_path) { - return sentry__crash_ipc_create_memfd(); + int fd = sentry__crash_ipc_create_memfd(); + if (fd < 0 && database_path) { + // no memfd before kernel 3.17, e.g. below API 26 + fd = sentry__crash_ipc_create_shm_file(database_path); + } + return fd; } __attribute__((visibility("default"))) void From d8d44a480f0c2395a8382823f522301e44260ee5 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 13:13:32 +0100 Subject: [PATCH 02/14] docs(android): Say that createCrashShm() needs memfd The overload without a database directory has no file fallback, so it needs a kernel with memfd rather than a specific API level. --- ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java index 94d31b5b55..d244ce92ee 100644 --- a/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java +++ b/ndk/lib/src/main/java/io/sentry/ndk/SentryNdk.java @@ -75,7 +75,7 @@ public static void init(@NotNull final NdkOptions options) { } } - /** Creates the shared memory of the crash IPC, or returns -1 if that fails. Needs API 30+. */ + /** Creates the shared memory of the crash IPC, or returns -1 if that fails. Needs memfd. */ public static int createCrashShm() { return createCrashShm(null); } From c80d705182b6e87a6c63eb4faacee33d3985a831 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 13:13:32 +0100 Subject: [PATCH 03/14] ci(android): Run the e2e tests on API 21, 26, 31 and 35 Match the Android API levels and NDK versions in `ci.yml`. The crash IPC now works without memfd, so drop the fixture's API 30 `minSdk` and the tests' API 30 skips. --- .github/workflows/e2e-test.yml | 12 ++++++++++++ tests/fixtures/android_e2e/README.md | 2 +- tests/fixtures/android_e2e/build.gradle | 2 +- tests/test_e2e_sentry.py | 9 --------- 4 files changed, 14 insertions(+), 11 deletions(-) diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index 8d81ec38c0..ab30fcfdda 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -76,6 +76,18 @@ jobs: fail-fast: false matrix: include: + - name: E2E Test (Android API 21, NDK 23) + ANDROID_API: 21 + ANDROID_NDK_VERSION: 23.2.8568313 + - name: E2E Test (Android API 26, NDK 27) + ANDROID_API: 26 + ANDROID_NDK_VERSION: 27.3.13750724 + - name: E2E Test (Android API 31, NDK 27) + ANDROID_API: 31 + ANDROID_NDK_VERSION: 27.3.13750724 + - name: E2E Test (Android API 35, NDK 29) + ANDROID_API: 35 + ANDROID_NDK_VERSION: 29.0.14206865 - name: E2E Test (Android API 36, NDK 30) ANDROID_API: 36 ANDROID_NDK_VERSION: 30.0.16138531 diff --git a/tests/fixtures/android_e2e/README.md b/tests/fixtures/android_e2e/README.md index ae25b26c35..b850b284ba 100644 --- a/tests/fixtures/android_e2e/README.md +++ b/tests/fixtures/android_e2e/README.md @@ -11,7 +11,7 @@ pytest -s -v tests/test_e2e_sentry.py Run these commands from the sentry-native root. Testing needs a running emulator or device, `ANDROID_API`, and the `SENTRY_E2E_*` credentials documented in the test -module. The crash service needs API 30+; tombstone merging needs API 31+. +module. Tombstone merging needs API 31+. The setup script builds this checkout's native NDK package and the Java SDK into an isolated Maven repository, overriding the Java SDK's pinned NDK dependency. diff --git a/tests/fixtures/android_e2e/build.gradle b/tests/fixtures/android_e2e/build.gradle index 17b59b228a..db80d35dee 100644 --- a/tests/fixtures/android_e2e/build.gradle +++ b/tests/fixtures/android_e2e/build.gradle @@ -18,7 +18,7 @@ android { defaultConfig { applicationId = "io.sentry.e2e" - minSdk = 30 + minSdk = 21 targetSdk = project.property("targetSdk").toInteger() ndk { abiFilters.add(System.getenv("ANDROID_ARCH")) } } diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index 1d032c6a4c..3c80096a66 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -1092,9 +1092,6 @@ def assert_android_native_event(event, test_id, tombstone, debuggable=True): @pytest.mark.skipif( not is_android or not has_native, reason="Tests need Android + native" ) -@pytest.mark.skipif( - is_android < 30, reason="The native crash service needs Android API 30+" -) @pytest.mark.parametrize( "android_e2e_app, debuggable", [("debug", True), ("nondebuggable", False)], @@ -1139,9 +1136,6 @@ def test_e2e_android_native(android_e2e_app, debuggable, tombstone): @pytest.mark.skipif( not is_android or not has_native, reason="Tests need Android + native" ) -@pytest.mark.skipif( - is_android < 30, reason="The native crash service needs Android API 30+" -) def test_e2e_android_native_broadcast(android_e2e_app): """A process that a broadcast starts in the background gets the crash service.""" test_id = str(uuid.uuid4()) @@ -1173,9 +1167,6 @@ def test_e2e_android_native_broadcast(android_e2e_app): @pytest.mark.skipif( not is_android or not has_native, reason="Tests need Android + native" ) -@pytest.mark.skipif( - is_android < 30, reason="The native crash service needs Android API 30+" -) def test_e2e_android_native_background(android_e2e_app): """The crash service outlives the idle timeout of background services.""" test_id = str(uuid.uuid4()) From 312396fbfb59c228d35df611eb56ec89a7c43140 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 13:39:11 +0100 Subject: [PATCH 04/14] ci(android): Check out the sentry-java API 21 branch for e2e tests The API 21 and 26 e2e jobs need the sentry-java side of running the crash service below API 30. --- .github/workflows/e2e-test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index ab30fcfdda..2e1b00bea4 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -111,7 +111,8 @@ jobs: repository: getsentry/sentry-java # first release with sentry-android-ndk-outproc, bump only when # sentry-native-ndk-outproc changes need matching sentry-java changes - ref: 8.64.0 + # ref: 8.64.0 + ref: jpnurmi/fix/native-android-21 # TODO: pin release path: sentry-java - name: Set up Python From aa0fc781a8972a4e1eee487d21fe94036a5a50e6 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 14:07:36 +0100 Subject: [PATCH 05/14] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index e8c45ad2ce..ed517351e5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ **Fixes**: +- Native/Android: support API 21+ (previously API 30+) with a file fallback for kernels without memfd. ([#2301](https://github.com/getsentry/sentry-native/pull/2301)) - Native: retain crash envelopes on OOM in the crash daemon. ([#2320](https://github.com/getsentry/sentry-native/pull/2320)) - Native: omit crash metadata in minidump-only mode. ([#2324](https://github.com/getsentry/sentry-native/pull/2324)) - Native: recover interrupted crash reports. ([#2330](https://github.com/getsentry/sentry-native/pull/2330)) From 8d45cf95449abf32e5d7c82a4ba616ff12989e30 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Wed, 11 Nov 2026 16:11:37 +0100 Subject: [PATCH 06/14] test(android): Fix the e2e tests on API 31 and 36 API 31 tombstones leave dex frames unnamed, so the merged event has no Java method names. Check them only on later API levels. The fixture crashed 1 s after setting the scope, but sentry-java syncs the scope to native on its single-threaded executor, behind the startup work of the tombstone integration. Now that the crash service starts without waiting, that work can still run when the crash fires, and the event misses the `test.id` tag. Queue the crash on the executor, so that it follows the scope sync. Print logcat when an Android e2e test fails. --- .../java/io/sentry/e2e/CrashReceiver.java | 4 +-- .../main/java/io/sentry/e2e/MainActivity.java | 12 ++++++- tests/test_e2e_sentry.py | 34 +++++++++++++------ 3 files changed, 35 insertions(+), 15 deletions(-) diff --git a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java index 1be9fb5722..5310077566 100644 --- a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java +++ b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java @@ -3,8 +3,6 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; -import android.os.Handler; -import android.os.Looper; import io.sentry.Sentry; public class CrashReceiver extends BroadcastReceiver { @@ -19,6 +17,6 @@ public void onReceive(Context context, Intent intent) { } // keep the process active until the crash goAsync(); - new Handler(Looper.getMainLooper()).postDelayed(MainActivity::triggerNativeCrash, 1000); + MainActivity.crashLater(); } } diff --git a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java index a9f0bd6ff6..2f62f21046 100644 --- a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java +++ b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java @@ -5,6 +5,7 @@ import android.content.Intent; import android.os.Bundle; import android.os.Handler; +import android.os.Looper; import io.sentry.Sentry; import io.sentry.android.core.SentryAndroid; import io.sentry.protocol.User; @@ -17,7 +18,7 @@ public void onCreate(Bundle state) { super.onCreate(state); init(getApplicationContext(), getIntent()); if (getIntent().getBooleanExtra("crash", true)) { - new Handler(getMainLooper()).postDelayed(MainActivity::triggerNativeCrash, 1000); + crashLater(); } } @@ -43,6 +44,15 @@ static void init(Context context, Intent intent) { }); } + static void crashLater() { + Handler handler = new Handler(Looper.getMainLooper()); + // the SDK executor syncs the scope to native first + Sentry.getCurrentScopes() + .getOptions() + .getExecutorService() + .submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, 1000)); + } + private static void callNativeCrash() { crash(); } diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index 3c80096a66..05e282ac86 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -1049,6 +1049,16 @@ def android_e2e_app(request): adb("uninstall", package, check=False) +@pytest.fixture +def android_logcat(request): + adb("logcat", "-c", check=False) + failed = request.session.testsfailed + yield + if request.session.testsfailed > failed: + logcat = adb("logcat", "-d", capture_output=True, text=True).stdout + print(f"\nlogcat:\n{logcat}") + + def assert_android_native_event(event, test_id, tombstone, debuggable=True): assert event["platform"] == "native" assert event["release"]["version"] == "sentry-native-e2e" @@ -1067,14 +1077,16 @@ def assert_android_native_event(event, test_id, tombstone, debuggable=True): if tombstone: functions = [frame.get("function") or "" for frame in frames] assert {"e2e_segfault", "e2e_native_crash"}.issubset(functions) - assert any( - "io.sentry.e2e.MainActivity.triggerNativeCrash" in function - for function in functions - ) - assert any( - "io.sentry.e2e.MainActivity.callNativeCrash" in function - for function in functions - ) + # API 31 tombstones leave dex frames unnamed + if is_android > 31: + assert any( + "io.sentry.e2e.MainActivity.triggerNativeCrash" in function + for function in functions + ) + assert any( + "io.sentry.e2e.MainActivity.callNativeCrash" in function + for function in functions + ) threads = get_threads_from_event(event) # a single thread means the daemon could not read the app's /proc @@ -1100,7 +1112,7 @@ def assert_android_native_event(event, test_id, tombstone, debuggable=True): scope="module", ) @pytest.mark.parametrize("tombstone", [False, True], ids=["native", "tombstone"]) -def test_e2e_android_native(android_e2e_app, debuggable, tombstone): +def test_e2e_android_native(android_e2e_app, android_logcat, debuggable, tombstone): """The crash service sends native crashes, optionally merged with a tombstone.""" if tombstone and is_android < 31: pytest.skip("Tombstone merging needs Android API 31+") @@ -1136,7 +1148,7 @@ def test_e2e_android_native(android_e2e_app, debuggable, tombstone): @pytest.mark.skipif( not is_android or not has_native, reason="Tests need Android + native" ) -def test_e2e_android_native_broadcast(android_e2e_app): +def test_e2e_android_native_broadcast(android_e2e_app, android_logcat): """A process that a broadcast starts in the background gets the crash service.""" test_id = str(uuid.uuid4()) package = android_e2e_app @@ -1167,7 +1179,7 @@ def test_e2e_android_native_broadcast(android_e2e_app): @pytest.mark.skipif( not is_android or not has_native, reason="Tests need Android + native" ) -def test_e2e_android_native_background(android_e2e_app): +def test_e2e_android_native_background(android_e2e_app, android_logcat): """The crash service outlives the idle timeout of background services.""" test_id = str(uuid.uuid4()) package = android_e2e_app From 710d93559482f855905a2db819c77c17ae77b2d5 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 12 Nov 2026 10:05:56 +0100 Subject: [PATCH 07/14] test(android): Run the native backend tests on API 21 `has_native` was false below API 23, so the API 21 e2e job skipped all Android tests and never exercised the file fallback for crash IPC. API 21 is the minimum, so drop the Android clause. --- tests/conditions.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/tests/conditions.py b/tests/conditions.py index e4dccc157a..c8124aa4f1 100644 --- a/tests/conditions.py +++ b/tests/conditions.py @@ -55,8 +55,6 @@ # Native backend works on all platforms (lightweight, no external dependencies) # It's always available - tests explicitly set SENTRY_BACKEND: native in cmake # On macOS ASAN, the signal handling conflicts with ASAN's memory interception -has_native = not (is_asan and sys.platform == "darwin") and ( - not is_android or is_android >= 23 -) +has_native = not (is_asan and sys.platform == "darwin") has_sccache = os.environ.get("USE_SCCACHE") and shutil.which("sccache") From 11ad300784d52635de3a0f03491b057884eb3633 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 12 Nov 2026 10:06:56 +0100 Subject: [PATCH 08/14] test(android): Replace undecodable bytes in the logcat dump A non-UTF-8 byte in `adb logcat` output raised `UnicodeDecodeError` in the fixture teardown and lost the logcat dump of a failed test. --- tests/test_e2e_sentry.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index 05e282ac86..d302a0fea0 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -1055,7 +1055,9 @@ def android_logcat(request): failed = request.session.testsfailed yield if request.session.testsfailed > failed: - logcat = adb("logcat", "-d", capture_output=True, text=True).stdout + logcat = adb( + "logcat", "-d", capture_output=True, text=True, errors="replace" + ).stdout print(f"\nlogcat:\n{logcat}") From 6c454163b77f1701c06d42fc8c878bd04e5066e4 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 12 Nov 2026 10:09:00 +0100 Subject: [PATCH 09/14] test(android): Run the native backend integration test on API 21 The API 23 skip blamed `process_vm_readv`, but below API 23 `sentry__process_vm_readv()` calls the raw syscall. --- tests/test_integration_android.py | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/test_integration_android.py b/tests/test_integration_android.py index 088fe3c7f7..f38ec242c1 100644 --- a/tests/test_integration_android.py +++ b/tests/test_integration_android.py @@ -15,10 +15,6 @@ not has_native, reason="Tests need the native backend enabled", ) -@pytest.mark.skipif( - 0 < is_android < 23, - reason="Android native backend test needs API 23+ (process_vm_readv)", -) def test_native_android(cmake): database_path = "/data/local/tmp/.sentry-native" From 43e2e3368f5a46377dfabe5e2d6d4fa164a20b53 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 12 Nov 2026 10:43:00 +0100 Subject: [PATCH 10/14] ci(android): Use the default emulator image for e2e tests The e2e tests do not use Google Play services. Use the smaller AOSP images, like the regular Android CI jobs. --- .github/workflows/e2e-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/e2e-test.yml b/.github/workflows/e2e-test.yml index 2e1b00bea4..3278d9d014 100644 --- a/.github/workflows/e2e-test.yml +++ b/.github/workflows/e2e-test.yml @@ -149,7 +149,7 @@ jobs: api-level: ${{ env.ANDROID_API }} ndk: ${{ env.ANDROID_NDK_VERSION }} arch: ${{ env.ANDROID_ARCH }} - target: google_apis + target: default emulator-boot-timeout: 1200 script: | export ANDROID_NDK="$ANDROID_HOME/ndk/$ANDROID_NDK_VERSION" From 0b1a6079bbfcbfa06d6d7808cd163a69cc2f748e Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 19 Nov 2026 15:05:01 +0100 Subject: [PATCH 11/14] fix(android): Don't wait for the crash service on a crashed main thread Below API 29, the app receives the crash service connection on its main thread. A native crash on the main thread before the daemon attached froze the app for 10 s, waiting for a daemon that couldn't attach, and then chained. Don't notify it in that case, so the crash chains at once. A crash on another thread still waits, since the main thread is free to attach the daemon. --- src/backends/native/sentry_crash_ipc.c | 13 +++++++++++++ src/backends/native/sentry_crash_ipc.h | 2 ++ tests/unit/test_native_backend.c | 20 ++++++++++++++++++++ tests/unit/tests.inc | 1 + 4 files changed, 36 insertions(+) diff --git a/src/backends/native/sentry_crash_ipc.c b/src/backends/native/sentry_crash_ipc.c index 34591cc7f6..7300db5966 100644 --- a/src/backends/native/sentry_crash_ipc.c +++ b/src/backends/native/sentry_crash_ipc.c @@ -170,6 +170,10 @@ sentry__crash_ipc_message_decode( # include # include +# if defined(SENTRY_PLATFORM_ANDROID) +# include +# endif + # ifndef MFD_CLOEXEC # define MFD_CLOEXEC 0x0001U # endif @@ -466,6 +470,8 @@ sentry__crash_ipc_init_app_with_fds(int shm_fd, int notify_fd, int ready_fd, ipc->init_mutex = init_mutex; // the crash service attaches after the app has started ipc->awaiting_ready = 1; + // below API 29, the app receives the service connection on its main thread + ipc->attach_on_main_thread = android_get_device_api_level() < 29; return init_app(ipc, shm_fd, notify_fd, ready_fd, message_fd); } @@ -1933,6 +1939,13 @@ sentry__crash_ipc_notify(sentry_crash_ipc_t *ipc) if (!ipc) { return false; } +#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) + // a crashed main thread can't attach the daemon + if (ipc->attach_on_main_thread && syscall(SYS_gettid) == getpid() + && !daemon_ready(ipc)) { + return false; + } +#endif if (sentry__atomic_fetch(&ipc->shmem->state) == SENTRY_CRASH_STATE_CRASHING) { return notify_fallback(ipc); diff --git a/src/backends/native/sentry_crash_ipc.h b/src/backends/native/sentry_crash_ipc.h index f30a687d59..01dbf4969f 100644 --- a/src/backends/native/sentry_crash_ipc.h +++ b/src/backends/native/sentry_crash_ipc.h @@ -122,6 +122,8 @@ typedef struct { volatile long stalled; // set until a daemon that attaches later signals ready volatile long awaiting_ready; + // a daemon that attaches later does so through the app's main thread + bool attach_on_main_thread; uint64_t sequence; // frame with an unsent tail, owned by the writer guard char *torn_frame; diff --git a/tests/unit/test_native_backend.c b/tests/unit/test_native_backend.c index df3695d91b..dc9c98a32a 100644 --- a/tests/unit/test_native_backend.c +++ b/tests/unit/test_native_backend.c @@ -2033,3 +2033,23 @@ SENTRY_TEST(crash_ipc_notify_without_daemon) sentry__crash_ipc_free(ipc); #endif } + +SENTRY_TEST(crash_ipc_notify_before_main_thread_attach) +{ +#if !defined(SENTRY_BACKEND_NATIVE) \ + || !(defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)) + SKIP_TEST(); +#else + sentry_crash_ipc_t *ipc = new_test_ipc(); + ipc->awaiting_ready = 1; + ipc->attach_on_main_thread = true; + // the crashed main thread can't attach the daemon + TEST_CHECK(!sentry__crash_ipc_notify(ipc)); + + uint64_t ready = 1; + TEST_CHECK_INT_EQUAL( + write(ipc->ready_fd, &ready, sizeof(ready)), sizeof(ready)); + TEST_CHECK(sentry__crash_ipc_notify(ipc)); + sentry__crash_ipc_free(ipc); +#endif +} diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 5935e5bffd..45ed763eed 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -159,6 +159,7 @@ XX(crash_ipc_message_oversized) XX(crash_ipc_message_partial) XX(crash_ipc_message_roundtrip) XX(crash_ipc_message_unknown_type) +XX(crash_ipc_notify_before_main_thread_attach) XX(crash_ipc_notify_without_daemon) XX(crash_ipc_partial_reads) XX(crash_ipc_reattach_stalled) From 331d443d2259cb542516ecc4ad0f92ea6bf12556 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Thu, 19 Nov 2026 17:33:39 +0100 Subject: [PATCH 12/14] fix(android): Don't consume the crash service ready signal A crash on the main thread checks whether the crash service attached without the writer lock. A concurrent send could read the ready signal between the crash handler's poll and read, and the handler then blocked on the drained pipe. Poll without reading, so that every caller sees the signal. A reattach replaces the pipe, which drops the old signal. --- src/backends/native/sentry_crash_ipc.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/backends/native/sentry_crash_ipc.c b/src/backends/native/sentry_crash_ipc.c index 7300db5966..5899ebc0e9 100644 --- a/src/backends/native/sentry_crash_ipc.c +++ b/src/backends/native/sentry_crash_ipc.c @@ -1836,10 +1836,9 @@ daemon_ready(sentry_crash_ipc_t *ipc) { #if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) if (sentry__atomic_fetch(&ipc->awaiting_ready)) { + // no read, so that concurrent callers all see the signal struct pollfd pfd = { ipc->ready_fd, POLLIN, 0 }; - uint64_t val; - if (poll(&pfd, 1, 0) != 1 || !(pfd.revents & POLLIN) - || read(ipc->ready_fd, &val, sizeof(val)) != sizeof(val)) { + if (poll(&pfd, 1, 0) != 1 || !(pfd.revents & POLLIN)) { return false; } sentry__atomic_store(&ipc->awaiting_ready, 0); From bfa2cef1ff86364c648eb5c05fffd0c683f3f81a Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 11:20:12 +0100 Subject: [PATCH 13/14] test(android): Crash right after init in the e2e test The e2e fixture crashed 1 s after `SentryAndroid.init`, so no test covered a crash on the main thread before the crash service connects. Add an early variant that crashes as soon as the scope is synced. It runs on API 29 and later, where the service connects on a binder thread while the crash handler waits. --- .../src/main/java/io/sentry/e2e/CrashReceiver.java | 2 +- .../src/main/java/io/sentry/e2e/MainActivity.java | 7 ++++--- tests/test_e2e_sentry.py | 12 +++++++++++- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java index 5310077566..5e235b5748 100644 --- a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java +++ b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/CrashReceiver.java @@ -17,6 +17,6 @@ public void onReceive(Context context, Intent intent) { } // keep the process active until the crash goAsync(); - MainActivity.crashLater(); + MainActivity.crashLater(intent); } } diff --git a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java index 2f62f21046..292fcbfa5b 100644 --- a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java +++ b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java @@ -18,7 +18,7 @@ public void onCreate(Bundle state) { super.onCreate(state); init(getApplicationContext(), getIntent()); if (getIntent().getBooleanExtra("crash", true)) { - crashLater(); + crashLater(getIntent()); } } @@ -44,13 +44,14 @@ static void init(Context context, Intent intent) { }); } - static void crashLater() { + static void crashLater(Intent intent) { Handler handler = new Handler(Looper.getMainLooper()); + int delay = intent.getIntExtra("crash_delay", 1000); // the SDK executor syncs the scope to native first Sentry.getCurrentScopes() .getOptions() .getExecutorService() - .submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, 1000)); + .submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, delay)); } private static void callNativeCrash() { diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index d302a0fea0..a13571e248 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -1114,10 +1114,17 @@ def assert_android_native_event(event, test_id, tombstone, debuggable=True): scope="module", ) @pytest.mark.parametrize("tombstone", [False, True], ids=["native", "tombstone"]) -def test_e2e_android_native(android_e2e_app, android_logcat, debuggable, tombstone): +@pytest.mark.parametrize("delay", [1000, 0], ids=["late", "early"]) +def test_e2e_android_native( + android_e2e_app, android_logcat, debuggable, tombstone, delay +): """The crash service sends native crashes, optionally merged with a tombstone.""" if tombstone and is_android < 31: pytest.skip("Tombstone merging needs Android API 31+") + if not delay and is_android < 29: + pytest.skip( + "Below API 29, a crash on the main thread before the service connects is not reported" + ) test_id = str(uuid.uuid4()) package = android_e2e_app adb("shell", "pm", "clear", package, check=True, capture_output=True) @@ -1137,6 +1144,9 @@ def test_e2e_android_native(android_e2e_app, android_logcat, debuggable, tombsto "--ez", "tombstone", str(tombstone).lower(), + "--ei", + "crash_delay", + str(delay), check=True, capture_output=True, ) From cb7af51a8b2c20e2db785bc9b1b190fcf46e3b49 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 13:39:06 +0100 Subject: [PATCH 14/14] test(android): Report an early main-thread crash below API 29 Crash synchronously right after init, so that below API 29 the main looper cannot connect the crash service first, and relaunch the app to send the crash. --- .../main/java/io/sentry/e2e/MainActivity.java | 18 ++- tests/test_e2e_sentry.py | 109 ++++++++++++------ 2 files changed, 90 insertions(+), 37 deletions(-) diff --git a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java index 292fcbfa5b..65b751f16b 100644 --- a/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java +++ b/tests/fixtures/android_e2e/src/main/java/io/sentry/e2e/MainActivity.java @@ -6,6 +6,7 @@ import android.os.Bundle; import android.os.Handler; import android.os.Looper; +import io.sentry.ISentryExecutorService; import io.sentry.Sentry; import io.sentry.android.core.SentryAndroid; import io.sentry.protocol.User; @@ -48,10 +49,19 @@ static void crashLater(Intent intent) { Handler handler = new Handler(Looper.getMainLooper()); int delay = intent.getIntExtra("crash_delay", 1000); // the SDK executor syncs the scope to native first - Sentry.getCurrentScopes() - .getOptions() - .getExecutorService() - .submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, delay)); + ISentryExecutorService executor = Sentry.getCurrentScopes().getOptions().getExecutorService(); + if (delay == 0) { + // below API 29, the main looper connects the crash service, so crash before + // returning to it + try { + executor.submit(() -> {}).get(); + } catch (Exception e) { + throw new RuntimeException(e); + } + triggerNativeCrash(); + return; + } + executor.submit(() -> handler.postDelayed(MainActivity::triggerNativeCrash, delay)); } private static void callNativeCrash() { diff --git a/tests/test_e2e_sentry.py b/tests/test_e2e_sentry.py index a13571e248..7d46116a0e 100644 --- a/tests/test_e2e_sentry.py +++ b/tests/test_e2e_sentry.py @@ -28,6 +28,7 @@ import requests from . import adb, run, check_output, sourcedir, Envelope +from .assertions import wait_for from .conditions import ( has_breakpad, has_crashpad, @@ -1061,7 +1062,9 @@ def android_logcat(request): print(f"\nlogcat:\n{logcat}") -def assert_android_native_event(event, test_id, tombstone, debuggable=True): +def assert_android_native_event( + event, test_id, tombstone, debuggable=True, daemon=True +): assert event["platform"] == "native" assert event["release"]["version"] == "sentry-native-e2e" assert event["user"]["id"] == test_id @@ -1090,11 +1093,12 @@ def assert_android_native_event(event, test_id, tombstone, debuggable=True): for function in functions ) - threads = get_threads_from_event(event) - # a single thread means the daemon could not read the app's /proc - assert len(threads["values"]) >= 2 - assert any(thread["crashed"] for thread in threads["values"]) - verify_no_thread_duplication(threads, "test_e2e_android_native") + if daemon: + threads = get_threads_from_event(event) + # a single thread means the daemon could not read the app's /proc + assert len(threads["values"]) >= 2 + assert any(thread["crashed"] for thread in threads["values"]) + verify_no_thread_duplication(threads, "test_e2e_android_native") images = get_debug_meta_from_event(event)["images"] # default packaging maps the libraries straight from the APK @@ -1121,38 +1125,77 @@ def test_e2e_android_native( """The crash service sends native crashes, optionally merged with a tombstone.""" if tombstone and is_android < 31: pytest.skip("Tombstone merging needs Android API 31+") - if not delay and is_android < 29: - pytest.skip( - "Below API 29, a crash on the main thread before the service connects is not reported" - ) + # below API 29, a crashed main thread can't connect the crash service, so + # the next launch sends the crash + relaunch = not delay and is_android < 29 test_id = str(uuid.uuid4()) package = android_e2e_app adb("shell", "pm", "clear", package, check=True, capture_output=True) try: - adb( - "shell", - "am", - "start", - "-n", - f"{package}/.MainActivity", - "--es", - "dsn", - os.environ["SENTRY_E2E_DSN"], - "--es", - "test_id", - test_id, - "--ez", - "tombstone", - str(tombstone).lower(), - "--ei", - "crash_delay", - str(delay), - check=True, - capture_output=True, - ) - # no restart: delivery must happen from the surviving crash service + try: + adb( + "shell", + "am", + "start", + # the relaunch must not race with the start of the crashing process + *(["-W"] if relaunch else []), + "-n", + f"{package}/.MainActivity", + "--es", + "dsn", + os.environ["SENTRY_E2E_DSN"], + "--es", + "test_id", + test_id, + "--ez", + "tombstone", + str(tombstone).lower(), + "--ei", + "crash_delay", + str(delay), + check=True, + capture_output=True, + timeout=10 if relaunch else None, + ) + except subprocess.TimeoutExpired: + # older Android waits forever for a launch that crashes in onCreate + pass + if relaunch: + + def running(): + # API 21 has no pidof, and API 26+ lists all processes only with -A + ps = adb( + "shell", + "ps", + *(["-A"] if is_android >= 26 else []), + capture_output=True, + text=True, + ).stdout + return any(line.split()[-1:] == [package] for line in ps.splitlines()) + + # relaunch only once the crashed process has exited + assert wait_for(lambda: not running(), timeout=30) + adb( + "shell", + "am", + "start", + "-n", + f"{package}/.MainActivity", + "--es", + "dsn", + os.environ["SENTRY_E2E_DSN"], + "--es", + "test_id", + test_id, + "--ez", + "crash", + "false", + check=True, + capture_output=True, + ) + # otherwise, no restart: delivery must happen from the surviving crash service event = poll_sentry_for_event(test_id) - assert_android_native_event(event, test_id, tombstone, debuggable) + assert_android_native_event(event, test_id, tombstone, debuggable, not relaunch) finally: adb("shell", "am", "force-stop", package, check=False)