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 @@ -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. ([#2330](https://github.com/getsentry/sentry-native/pull/2330))

## 1.0.0-beta.1

Expand Down
44 changes: 16 additions & 28 deletions src/backends/native/sentry_crash_daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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);
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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)) {
Expand Down
32 changes: 32 additions & 0 deletions src/backends/native/sentry_crash_ipc.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions src/backends/native/sentry_crash_ipc.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "sentry_boot.h"
#include "sentry_crash_context.h"
#include "sentry_path.h"
#include "sentry_ringbuffer.h"

#include <stddef.h>
Expand Down Expand Up @@ -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;
Expand Down
53 changes: 50 additions & 3 deletions src/backends/sentry_backend_native.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Comment thread
cursor[bot] marked this conversation as resolved.
Resolved
}

static bool
native_backend_process_old_run(sentry_backend_t *backend,
const sentry_options_t *options, const sentry_path_t *run_path)
Expand All @@ -438,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);
Expand All @@ -448,14 +489,20 @@ 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);
return true;
return send_unprocessed_crash(options, run_path);
}

/**
Expand Down
95 changes: 95 additions & 0 deletions tests/unit/test_native_backend.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "sentry_testsupport.h"
#include "sentry_tracing.h"
#include "sentry_utils.h"
#include "sentry_uuid.h"
#include <stddef.h>
#include <stdlib.h>
#include <string.h>
Expand Down Expand Up @@ -44,6 +45,28 @@ count_sent_envelopes(sentry_envelope_t *envelope, void *state)
(*count)++;
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)
{
(void)envelope;
size_t *count = user_data;
(*count)++;
}
#endif

#if defined(SENTRY_PLATFORM_LINUX) || defined(SENTRY_PLATFORM_ANDROID)
Expand Down Expand Up @@ -203,6 +226,78 @@ SENTRY_TEST(corrupt_crash_envelope_does_not_block_old_run)
#endif
}

SENTRY_TEST(recover_interrupted_crash)
{
#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);

sent_envelopes_t sent = { 0 };
sentry_transport_t *transport = sentry_transport_new(record_sent_envelopes);
TEST_ASSERT(!!transport);
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(
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);
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, strlen(event)) == 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);
event_id = sentry__envelope_get_event_id(envelope);
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);

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);
}

sentry__run_clean(options->run, true);
sentry_options_free(options);
#else
SKIP_TEST();
#endif
}

/**
* Test minidump header structure size and alignment
*/
Expand Down
1 change: 1 addition & 0 deletions tests/unit/tests.inc
Original file line number Diff line number Diff line change
Expand Up @@ -397,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)
Expand Down
Loading