From 8648d3ec1c0bf96b34f478a98ec27cf735f466af Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 13:42:48 +0100 Subject: [PATCH 1/4] fix(native): Recover interrupted crash reports If the crash daemon is killed abruptly, or has not attached yet, the crash is lost: the next launch deletes its `__sentry-event` with the old run folder. This is more likely on Android, where the daemon runs in a service process that the system can kill independently of the app, for example under memory pressure, and that may not have connected yet when the app crashes. Recover the event instead. The daemon removes the file once it has sent the crash or kept its own envelope. --- CHANGELOG.md | 1 + src/backends/native/sentry_crash_daemon.c | 44 +++++++++-------------- src/backends/native/sentry_crash_ipc.c | 32 +++++++++++++++++ src/backends/native/sentry_crash_ipc.h | 3 ++ src/backends/sentry_backend_native.c | 36 ++++++++++++++++++- tests/unit/test_native_backend.c | 41 +++++++++++++++++++++ tests/unit/tests.inc | 1 + 7 files changed, 129 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6c583a617c..8183be2687 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - 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. ([#NNNN](https://github.com/getsentry/sentry-native/pull/NNNN)) ## 1.0.0-beta.1 diff --git a/src/backends/native/sentry_crash_daemon.c b/src/backends/native/sentry_crash_daemon.c index fe808c3be0..f39f7c0842 100644 --- a/src/backends/native/sentry_crash_daemon.c +++ b/src/backends/native/sentry_crash_daemon.c @@ -532,28 +532,6 @@ read_attachment_manifest(const sentry_path_t *run_folder) sentry__path_free(path); return attachments; } - -static sentry_value_t -read_crash_event(const sentry_path_t *run_folder) -{ - sentry_path_t *path = sentry__path_join_str(run_folder, "__sentry-event"); - if (!path) { - return sentry_value_new_null(); - } - size_t len = 0; - char *buf = sentry__path_read_to_buffer(path, &len); - sentry__path_free(path); - if (!buf) { - return sentry_value_new_null(); - } - sentry_value_t event = sentry_value_from_json(buf, len); - sentry_free(buf); - if (sentry_value_get_type(event) != SENTRY_VALUE_TYPE_OBJECT) { - sentry_value_decref(event); - return sentry_value_new_null(); - } - return event; -} #if defined(SENTRY_PLATFORM_ANDROID) static sentry_path_t * make_run_envelope_path( @@ -4212,7 +4190,8 @@ ensure_device_arch(sentry_value_t event) /** * Removes the crash envelope once the transport has sent or dumped the crash, - * or renames it for a later send. + * or renames it for a later send. Either way, it also removes the crash event, + * which the next launch would otherwise send. */ static void retain_crash_envelope(const sentry_options_t *options, @@ -4236,9 +4215,12 @@ retain_crash_envelope(const sentry_options_t *options, if (!ctx->has_on_crashed_last_run) { sentry__path_remove(path); } - } else if (sentry__path_rename(path, run_path) == 0 - && ctx->has_on_crashed_last_run) { - sentry__run_write_crash_marker(options->run, &event_id); + sentry__crash_event_remove(options->run->run_path); + } else if (sentry__path_rename(path, run_path) == 0) { + if (ctx->has_on_crashed_last_run) { + sentry__run_write_crash_marker(options->run, &event_id); + } + sentry__crash_event_remove(options->run->run_path); } } sentry__path_free(path); @@ -4333,8 +4315,9 @@ sentry__process_crash(const sentry_options_t *options, sentry_crash_ipc_t *ipc, sentry_value_t base = sentry__crash_scope_event(&ipc->scope); sentry_path_t *run_folder = sentry__path_from_str(ctx->run_path); - sentry_value_t crash_event - = run_folder ? read_crash_event(run_folder) : sentry_value_new_null(); + sentry_value_t crash_event = run_folder + ? sentry__crash_event_read(run_folder) + : sentry_value_new_null(); if (!sentry_value_is_null(crash_event)) { sentry_value_decref(base); base = crash_event; @@ -4607,6 +4590,11 @@ sentry__process_crash(const sentry_options_t *options, sentry_crash_ipc_t *ipc, sentry__path_free(android_native_envelope_path); #endif + // A crash sent via transport keeps its event until the send has finished. + if (run_folder && sentry_uuid_is_nil(event_id)) { + sentry__crash_event_remove(run_folder); + } + // Keep the original crash envelope for the callback on the next launch. // A crash sent via transport keeps it until the send has finished. if (!ctx->has_on_crashed_last_run && sentry_uuid_is_nil(event_id)) { diff --git a/src/backends/native/sentry_crash_ipc.c b/src/backends/native/sentry_crash_ipc.c index d3c9269517..4d2aa3ff52 100644 --- a/src/backends/native/sentry_crash_ipc.c +++ b/src/backends/native/sentry_crash_ipc.c @@ -3,6 +3,7 @@ #include "sentry_alloc.h" #include "sentry_core.h" #include "sentry_logger.h" +#include "sentry_path.h" #include "sentry_random.h" #include "sentry_sync.h" #include "sentry_utils.h" @@ -1677,6 +1678,37 @@ sentry__crash_scope_event(const sentry_crash_scope_t *scope) return event; } +sentry_value_t +sentry__crash_event_read(const sentry_path_t *run_folder) +{ + sentry_path_t *path = sentry__path_join_str(run_folder, "__sentry-event"); + if (!path) { + return sentry_value_new_null(); + } + size_t len = 0; + char *buf = sentry__path_read_to_buffer(path, &len); + sentry__path_free(path); + if (!buf) { + return sentry_value_new_null(); + } + sentry_value_t event = sentry_value_from_json(buf, len); + sentry_free(buf); + if (sentry_value_get_type(event) != SENTRY_VALUE_TYPE_OBJECT) { + sentry_value_decref(event); + return sentry_value_new_null(); + } + return event; +} + +bool +sentry__crash_event_remove(const sentry_path_t *run_folder) +{ + sentry_path_t *path = sentry__path_join_str(run_folder, "__sentry-event"); + int rv = path ? sentry__path_remove(path) : 1; + sentry__path_free(path); + return rv == 0; +} + // returns false on a hard error, and `sent < len` if the daemon stopped reading #if defined(SENTRY_PLATFORM_WINDOWS) static bool diff --git a/src/backends/native/sentry_crash_ipc.h b/src/backends/native/sentry_crash_ipc.h index d879345b9f..56f13cb211 100644 --- a/src/backends/native/sentry_crash_ipc.h +++ b/src/backends/native/sentry_crash_ipc.h @@ -3,6 +3,7 @@ #include "sentry_boot.h" #include "sentry_crash_context.h" +#include "sentry_path.h" #include "sentry_ringbuffer.h" #include @@ -98,6 +99,8 @@ void sentry__crash_scope_free(sentry_crash_scope_t *scope); bool sentry__crash_scope_apply( sentry_crash_scope_t *scope, const sentry_crash_ipc_message_t *message); sentry_value_t sentry__crash_scope_event(const sentry_crash_scope_t *scope); +sentry_value_t sentry__crash_event_read(const sentry_path_t *run_folder); +bool sentry__crash_event_remove(const sentry_path_t *run_folder); #if defined(SENTRY_PLATFORM_WINDOWS) typedef HANDLE sentry_process_handle_t; diff --git a/src/backends/sentry_backend_native.c b/src/backends/sentry_backend_native.c index c016003392..e15e34c997 100644 --- a/src/backends/sentry_backend_native.c +++ b/src/backends/sentry_backend_native.c @@ -423,6 +423,40 @@ add_attachment(void *data, sentry_value_t attachment) // locations. } +// a daemon removes the crash event once it has sent the crash or kept it +// for the next launch +static bool +send_unprocessed_crash( + const sentry_options_t *options, const sentry_path_t *run_path) +{ + sentry_value_t event = sentry__crash_event_read(run_path); + if (sentry_value_is_null(event)) { + return true; + } + // remove before sending to prevent repeated sends + if (!sentry__crash_event_remove(run_path)) { + sentry_value_decref(event); + return false; + } + + sentry_path_t *path + = sentry__path_join_str(run_path, "__sentry-attachments"); + sentry_value_t attachments = sentry__read_attachment_manifest(path); + sentry__path_free(path); + sentry_envelope_t *envelope + = sentry__enclose_event(options, event, NULL, attachments); + sentry_value_decref(attachments); + if (!envelope) { + return true; + } + if (options->on_crashed_last_run_func) { + options->on_crashed_last_run_func( + envelope, options->on_crashed_last_run_data); + } + sentry__capture_envelope(options->transport, envelope, options); + return true; +} + static bool native_backend_process_old_run(sentry_backend_t *backend, const sentry_options_t *options, const sentry_path_t *run_path) @@ -455,7 +489,7 @@ native_backend_process_old_run(sentry_backend_t *backend, sentry_envelope_free(envelope); } sentry__pathiter_free(it); - return true; + return send_unprocessed_crash(options, run_path); } /** diff --git a/tests/unit/test_native_backend.c b/tests/unit/test_native_backend.c index 98fee62182..be71e30ef5 100644 --- a/tests/unit/test_native_backend.c +++ b/tests/unit/test_native_backend.c @@ -203,6 +203,47 @@ SENTRY_TEST(corrupt_crash_envelope_does_not_block_old_run) #endif } +SENTRY_TEST(interrupted_crash_recovered) +{ +#ifdef SENTRY_BACKEND_NATIVE + SENTRY_TEST_OPTIONS_NEW(options); + TEST_ASSERT(sentry__path_remove_all(options->database_path) == 0); + TEST_ASSERT(sentry__path_create_dir_all(options->database_path) == 0); + + options->run = sentry__run_new(options->database_path); + TEST_ASSERT(!!options->run); + sentry_run_t *old_run = sentry__run_new(options->database_path); + TEST_ASSERT(!!old_run); + sentry__filelock_unlock(old_run->lock); + + // crash handling was interrupted, so the crash event is left behind + sentry_path_t *event_path + = sentry__path_join_str(old_run->run_path, "__sentry-event"); + TEST_ASSERT(!!event_path); + const char event[] = "{\"level\":\"fatal\"}"; + TEST_ASSERT( + sentry__path_write_buffer(event_path, event, sizeof(event) - 1) == 0); + + size_t sent_envelopes = 0; + sentry_transport_t *transport = sentry_transport_new(count_sent_envelopes); + TEST_ASSERT(!!transport); + sentry_transport_set_state(transport, &sent_envelopes); + sentry_options_set_transport(options, transport); + + sentry__process_old_runs(options, 0); + + TEST_CHECK_INT_EQUAL(sent_envelopes, 1); + TEST_CHECK(!sentry__path_is_dir(old_run->run_path)); + + sentry__path_free(event_path); + sentry__run_free(old_run); + sentry__run_clean(options->run, true); + sentry_options_free(options); +#else + SKIP_TEST(); +#endif +} + /** * Test minidump header structure size and alignment */ diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index d0dad688d3..5935e5bffd 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -271,6 +271,7 @@ XX(http_transport_new_null_send_func_fails) XX(init_failure) XX(installation_id) XX(internal_uuid_api) +XX(interrupted_crash_recovered) XX(invalid_dsn) XX(invalid_proxy) XX(is_enabled) From 6e1120ef40a830c93a2ac141217223d843bbe7c0 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 14:44:12 +0100 Subject: [PATCH 2/4] Update CHANGELOG.md --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8183be2687..e8c45ad2ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - 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. ([#NNNN](https://github.com/getsentry/sentry-native/pull/NNNN)) +- Native: recover interrupted crash reports. ([#2330](https://github.com/getsentry/sentry-native/pull/2330)) ## 1.0.0-beta.1 From db67673339e420e2920b47a74b4b66ea11adc377 Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 15:03:57 +0100 Subject: [PATCH 3/4] fix(native): Recover the crash envelope of an interrupted daemon A daemon killed after writing `__sentry-crash.envelope` but before removing `__sentry-event` left both files, and the next launch invoked `on_crashed_last_run` for each. Send the daemon's envelope instead and remove the event. --- src/backends/sentry_backend_native.c | 17 +++++++- tests/unit/test_native_backend.c | 65 +++++++++++++++++++++------- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/backends/sentry_backend_native.c b/src/backends/sentry_backend_native.c index e15e34c997..5399984858 100644 --- a/src/backends/sentry_backend_native.c +++ b/src/backends/sentry_backend_native.c @@ -472,7 +472,14 @@ native_backend_process_old_run(sentry_backend_t *backend, continue; } - sentry_envelope_t *envelope = options->on_crashed_last_run_func + // a daemon interrupted before the crash was sent leaves the event + sentry_path_t *event_path + = sentry__path_join_str(run_path, "__sentry-event"); + bool interrupted = event_path && sentry__path_is_file(event_path); + sentry__path_free(event_path); + + sentry_envelope_t *envelope + = options->on_crashed_last_run_func || interrupted ? sentry__envelope_from_path(file) : NULL; bool materialized = envelope && sentry__envelope_materialize(envelope); @@ -482,10 +489,16 @@ native_backend_process_old_run(sentry_backend_t *backend, sentry__pathiter_free(it); return false; } - if (materialized) { + if (materialized && options->on_crashed_last_run_func) { options->on_crashed_last_run_func( envelope, options->on_crashed_last_run_data); } + // prefer the daemon's envelope to the crash event + if (materialized && interrupted + && sentry__crash_event_remove(run_path)) { + sentry__capture_envelope(options->transport, envelope, options); + envelope = NULL; + } sentry_envelope_free(envelope); } sentry__pathiter_free(it); diff --git a/tests/unit/test_native_backend.c b/tests/unit/test_native_backend.c index be71e30ef5..df3695d91b 100644 --- a/tests/unit/test_native_backend.c +++ b/tests/unit/test_native_backend.c @@ -44,6 +44,14 @@ count_sent_envelopes(sentry_envelope_t *envelope, void *state) (*count)++; sentry_envelope_free(envelope); } + +static void +count_crashed_last_run(const sentry_envelope_t *envelope, void *user_data) +{ + (void)envelope; + size_t *count = user_data; + (*count)++; +} #endif #if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID) @@ -212,31 +220,56 @@ SENTRY_TEST(interrupted_crash_recovered) options->run = sentry__run_new(options->database_path); TEST_ASSERT(!!options->run); - sentry_run_t *old_run = sentry__run_new(options->database_path); - TEST_ASSERT(!!old_run); - sentry__filelock_unlock(old_run->lock); - - // crash handling was interrupted, so the crash event is left behind - sentry_path_t *event_path - = sentry__path_join_str(old_run->run_path, "__sentry-event"); - TEST_ASSERT(!!event_path); - const char event[] = "{\"level\":\"fatal\"}"; - TEST_ASSERT( - sentry__path_write_buffer(event_path, event, sizeof(event) - 1) == 0); size_t sent_envelopes = 0; sentry_transport_t *transport = sentry_transport_new(count_sent_envelopes); TEST_ASSERT(!!transport); sentry_transport_set_state(transport, &sent_envelopes); sentry_options_set_transport(options, transport); + size_t crashed_last_run = 0; + sentry_options_set_on_crashed_last_run( + options, count_crashed_last_run, &crashed_last_run); + + for (size_t i = 1; i <= 2; i++) { + sentry_run_t *old_run = sentry__run_new(options->database_path); + TEST_ASSERT(!!old_run); + sentry__filelock_unlock(old_run->lock); + + // crash handling was interrupted, so the crash event is left behind + sentry_path_t *event_path + = sentry__path_join_str(old_run->run_path, "__sentry-event"); + TEST_ASSERT(!!event_path); + const char event[] = "{\"level\":\"fatal\"}"; + TEST_ASSERT( + sentry__path_write_buffer(event_path, event, sizeof(event) - 1) + == 0); + sentry__path_free(event_path); + + // the daemon's envelope, if any, takes precedence + if (i == 2) { + sentry_path_t *envelope_path = sentry__path_join_str( + old_run->run_path, "__sentry-crash.envelope"); + TEST_ASSERT(!!envelope_path); + sentry_envelope_t *envelope = sentry__envelope_new(NULL); + TEST_ASSERT(!!envelope); + sentry__envelope_add_event(envelope, + sentry_value_new_message_event( + SENTRY_LEVEL_FATAL, NULL, "crash"), + NULL); + TEST_ASSERT( + sentry_envelope_write_to_path(envelope, envelope_path) == 0); + sentry_envelope_free(envelope); + sentry__path_free(envelope_path); + } - sentry__process_old_runs(options, 0); + sentry__process_old_runs(options, 0); - TEST_CHECK_INT_EQUAL(sent_envelopes, 1); - TEST_CHECK(!sentry__path_is_dir(old_run->run_path)); + TEST_CHECK_INT_EQUAL(sent_envelopes, i); + TEST_CHECK_INT_EQUAL(crashed_last_run, i); + TEST_CHECK(!sentry__path_is_dir(old_run->run_path)); + sentry__run_free(old_run); + } - sentry__path_free(event_path); - sentry__run_free(old_run); sentry__run_clean(options->run, true); sentry_options_free(options); #else From a6dc761a0d442a0f83614072c05b5ebd1e85be4b Mon Sep 17 00:00:00 2001 From: J-P Nurmi Date: Fri, 20 Nov 2026 16:56:20 +0100 Subject: [PATCH 4/4] test(native): verify which crash the recovery sends --- tests/unit/test_native_backend.c | 37 +++++++++++++++++++++++++------- tests/unit/tests.inc | 2 +- 2 files changed, 30 insertions(+), 9 deletions(-) diff --git a/tests/unit/test_native_backend.c b/tests/unit/test_native_backend.c index df3695d91b..31e6b29a11 100644 --- a/tests/unit/test_native_backend.c +++ b/tests/unit/test_native_backend.c @@ -17,6 +17,7 @@ #include "sentry_testsupport.h" #include "sentry_tracing.h" #include "sentry_utils.h" +#include "sentry_uuid.h" #include #include #include @@ -45,6 +46,20 @@ count_sent_envelopes(sentry_envelope_t *envelope, void *state) sentry_envelope_free(envelope); } +typedef struct { + size_t count; + sentry_uuid_t event_id; +} sent_envelopes_t; + +static void +record_sent_envelopes(sentry_envelope_t *envelope, void *state) +{ + sent_envelopes_t *sent = state; + sent->count++; + sent->event_id = sentry__envelope_get_event_id(envelope); + sentry_envelope_free(envelope); +} + static void count_crashed_last_run(const sentry_envelope_t *envelope, void *user_data) { @@ -211,7 +226,7 @@ SENTRY_TEST(corrupt_crash_envelope_does_not_block_old_run) #endif } -SENTRY_TEST(interrupted_crash_recovered) +SENTRY_TEST(recover_interrupted_crash) { #ifdef SENTRY_BACKEND_NATIVE SENTRY_TEST_OPTIONS_NEW(options); @@ -221,10 +236,10 @@ SENTRY_TEST(interrupted_crash_recovered) options->run = sentry__run_new(options->database_path); TEST_ASSERT(!!options->run); - size_t sent_envelopes = 0; - sentry_transport_t *transport = sentry_transport_new(count_sent_envelopes); + sent_envelopes_t sent = { 0 }; + sentry_transport_t *transport = sentry_transport_new(record_sent_envelopes); TEST_ASSERT(!!transport); - sentry_transport_set_state(transport, &sent_envelopes); + sentry_transport_set_state(transport, &sent); sentry_options_set_transport(options, transport); size_t crashed_last_run = 0; sentry_options_set_on_crashed_last_run( @@ -239,10 +254,14 @@ SENTRY_TEST(interrupted_crash_recovered) sentry_path_t *event_path = sentry__path_join_str(old_run->run_path, "__sentry-event"); TEST_ASSERT(!!event_path); - const char event[] = "{\"level\":\"fatal\"}"; + sentry_uuid_t event_id = sentry_uuid_new_v4(); + char id[37]; + sentry_uuid_as_string(&event_id, id); + char event[128]; + snprintf(event, sizeof(event), + "{\"event_id\":\"%s\",\"level\":\"fatal\"}", id); TEST_ASSERT( - sentry__path_write_buffer(event_path, event, sizeof(event) - 1) - == 0); + sentry__path_write_buffer(event_path, event, strlen(event)) == 0); sentry__path_free(event_path); // the daemon's envelope, if any, takes precedence @@ -256,6 +275,7 @@ SENTRY_TEST(interrupted_crash_recovered) sentry_value_new_message_event( SENTRY_LEVEL_FATAL, NULL, "crash"), NULL); + event_id = sentry__envelope_get_event_id(envelope); TEST_ASSERT( sentry_envelope_write_to_path(envelope, envelope_path) == 0); sentry_envelope_free(envelope); @@ -264,7 +284,8 @@ SENTRY_TEST(interrupted_crash_recovered) sentry__process_old_runs(options, 0); - TEST_CHECK_INT_EQUAL(sent_envelopes, i); + TEST_CHECK_INT_EQUAL(sent.count, i); + TEST_CHECK(sentry__uuid_eq(&sent.event_id, &event_id)); TEST_CHECK_INT_EQUAL(crashed_last_run, i); TEST_CHECK(!sentry__path_is_dir(old_run->run_path)); sentry__run_free(old_run); diff --git a/tests/unit/tests.inc b/tests/unit/tests.inc index 5935e5bffd..94c71ae10d 100644 --- a/tests/unit/tests.inc +++ b/tests/unit/tests.inc @@ -271,7 +271,6 @@ XX(http_transport_new_null_send_func_fails) XX(init_failure) XX(installation_id) XX(internal_uuid_api) -XX(interrupted_crash_recovered) XX(invalid_dsn) XX(invalid_proxy) XX(is_enabled) @@ -398,6 +397,7 @@ XX(raw_envelope_event_id) XX(read_envelope_from_file) XX(read_write_envelope_to_file_null) XX(read_write_envelope_to_invalid_path) +XX(recover_interrupted_crash) XX(recursive_paths) XX(retry_backoff) XX(retry_cache)