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: possible race conditions in init and close #545

Merged
merged 16 commits into from
Jul 6, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
22 changes: 20 additions & 2 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@

static sentry_options_t *g_options = NULL;
static sentry_mutex_t g_options_lock = SENTRY__MUTEX_INIT;
static sentry_mutex_t g_init_lock = SENTRY__MUTEX_INIT;
This conversation was marked as resolved.
Show resolved Hide resolved

const sentry_options_t *
sentry__options_getref(void)
Expand Down Expand Up @@ -72,7 +73,15 @@ sentry__should_skip_upload(void)
int
sentry_init(sentry_options_t *options)
{
sentry_close();
// this function is to be called only once, so we do not allow more than
// one call to be active. If the function is called twice, create an error
// and return fail (1)
sentry__mutex_lock(&g_init_lock);
if (g_options != NULL) {
This conversation was marked as resolved.
Show resolved Hide resolved
SENTRY_ERROR("sentry_init() may only be called once");
sentry__mutex_unlock(&g_init_lock);
return 1;
This conversation was marked as resolved.
Show resolved Hide resolved
}

sentry_logger_t logger = { NULL, NULL };
if (options->debug) {
Expand All @@ -85,6 +94,7 @@ sentry_init(sentry_options_t *options)
SENTRY_WARN("failed to create database directory or there is no write "
"access to this directory");
sentry_options_free(options);
This conversation was marked as resolved.
Show resolved Hide resolved
sentry__mutex_unlock(&g_init_lock);
return 1;
}
sentry_transport_t *transport = options->transport;
Expand Down Expand Up @@ -167,6 +177,7 @@ sentry_init(sentry_options_t *options)
sentry_start_session();
}

sentry__mutex_unlock(&g_init_lock);
return 0;

fail:
Expand All @@ -175,6 +186,7 @@ sentry_init(sentry_options_t *options)
sentry__transport_shutdown(transport, 0);
}
sentry_options_free(options);
sentry__mutex_unlock(&g_init_lock);
return 1;
}

Expand All @@ -183,9 +195,10 @@ sentry_close(void)
{
sentry_end_session();

// keep the global options until end of function if other calls need the
This conversation was marked as resolved.
Show resolved Hide resolved
// data while we still clean up to avoid crashes
sentry__mutex_lock(&g_options_lock);
sentry_options_t *options = g_options;
g_options = NULL;
sentry__mutex_unlock(&g_options_lock);

size_t dumped_envelopes = 0;
Expand Down Expand Up @@ -216,6 +229,11 @@ sentry_close(void)

sentry__scope_cleanup();
sentry_clear_modulecache();

sentry__mutex_lock(&g_options_lock);
g_options = NULL;
sentry__mutex_unlock(&g_options_lock);

return (int)dumped_envelopes;
}

Expand Down
2 changes: 2 additions & 0 deletions src/sentry_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,6 @@ void sentry__logger_log(sentry_level_t level, const char *message, ...);

#define SENTRY_WARN(message) sentry__logger_log(SENTRY_LEVEL_WARNING, message)

#define SENTRY_ERROR(message) sentry__logger_log(SENTRY_LEVEL_ERROR, message)

#endif