Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Flush scope immediately when starting Crashpad Backend #272

Merged
merged 1 commit into from
Jun 4, 2020
Merged
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
72 changes: 39 additions & 33 deletions src/backends/sentry_backend_crashpad.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,39 @@ sentry__crashpad_backend_user_consent_changed(sentry_backend_t *backend)
data->db->GetSettings()->SetUploadsEnabled(!sentry__should_skip_upload());
}

static void
sentry__crashpad_backend_flush_scope(
sentry_backend_t *backend, const sentry_scope_t *UNUSED(scope))
{
const crashpad_state_t *data = (crashpad_state_t *)backend->data;
if (!data->event_path) {
return;
}

// This here is an empty object that we copy the scope into.
// Even though the API is specific to `event`, an `event` has a few default
// properties that we do not want here.
sentry_value_t event = sentry_value_new_object();
SENTRY_WITH_SCOPE (scope) {
// we want the scope without any modules or breadcrumbs
sentry__scope_apply_to_event(scope, event, SENTRY_SCOPE_NONE);
}

size_t mpack_size;
char *mpack = sentry_value_to_msgpack(event, &mpack_size);
sentry_value_decref(event);
if (!mpack) {
return;
}

int rv = sentry__path_write_buffer(data->event_path, mpack, mpack_size);
sentry_free(mpack);

if (rv != 0) {
SENTRY_DEBUG("flushing scope to msgpack failed");
}
}

static void
sentry__crashpad_backend_shutdown(sentry_backend_t *backend)
{
Expand Down Expand Up @@ -131,6 +164,12 @@ sentry__crashpad_backend_startup(sentry_backend_t *backend)
sentry__path_touch(data->breadcrumb1_path);
sentry__path_touch(data->breadcrumb2_path);

// now that we have the files, we flush the scope into the event right away,
// so that we do have something in case we crash without triggering a scope
// flush through other methods. The `scope` param is unused, so its safe
// to pass `NULL` here.
sentry__crashpad_backend_flush_scope(backend, NULL);

file_attachments.emplace(
"__sentry-event", base::FilePath(data->event_path->path));
file_attachments.emplace(
Expand Down Expand Up @@ -173,39 +212,6 @@ sentry__crashpad_backend_startup(sentry_backend_t *backend)
}
}

static void
sentry__crashpad_backend_flush_scope(
sentry_backend_t *backend, const sentry_scope_t *UNUSED(scope))
{
const crashpad_state_t *data = (crashpad_state_t *)backend->data;
if (!data->event_path) {
return;
}

// This here is an empty object that we copy the scope into.
// Even though the API is specific to `event`, an `event` has a few default
// properties that we do not want here.
sentry_value_t event = sentry_value_new_object();
SENTRY_WITH_SCOPE (scope) {
// we want the scope without any modules or breadcrumbs
sentry__scope_apply_to_event(scope, event, SENTRY_SCOPE_NONE);
}

size_t mpack_size;
char *mpack = sentry_value_to_msgpack(event, &mpack_size);
sentry_value_decref(event);
if (!mpack) {
return;
}

int rv = sentry__path_write_buffer(data->event_path, mpack, mpack_size);
sentry_free(mpack);

if (rv != 0) {
SENTRY_DEBUG("flushing scope to msgpack failed");
}
}

static void
sentry__crashpad_backend_add_breadcrumb(
sentry_backend_t *backend, sentry_value_t breadcrumb)
Expand Down
1 change: 1 addition & 0 deletions src/sentry_logger.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <stdio.h>
#include <string.h>

#include "sentry_logger.h"
#include "sentry_options.h"
Expand Down