Skip to content

Commit

Permalink
fix: deadlock in dynamic sdk-name scope init... (#858)
Browse files Browse the repository at this point in the history
  • Loading branch information
supervacuus authored Jul 3, 2023
1 parent 1a7184b commit e340df1
Show file tree
Hide file tree
Showing 8 changed files with 47 additions and 18 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# Changelog

## Unreleased

**Fixes**:

- Remove deadlock pattern in dynamic sdk-name assignment ([#858](https://github.com/getsentry/sentry-native/pull/858))

## 0.6.4

**Fixes**:
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ The example currently supports the following commands:
- `discarding-before-send`: Installs a `before_send()` callback that discards the event.
- `on-crash`: Installs an `on_crash()` callback that retains the crash event.
- `discarding-on-crash`: Installs an `on_crash()` callback that discards the crash event.
- `override-sdk-name`: Changes the SDK name via the options at runtime.

Only on Windows using crashpad with its WER handler module:

Expand Down
4 changes: 4 additions & 0 deletions examples/example.c
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,10 @@ main(int argc, char **argv)
options, discarding_on_crash_callback, NULL);
}

if (has_arg(argc, argv, "override-sdk-name")) {
sentry_options_set_sdk_name(options, "sentry.native.android.flutter");
}

sentry_init(options);

if (!has_arg(argc, argv, "no-setup")) {
Expand Down
13 changes: 9 additions & 4 deletions src/sentry_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,16 @@ sentry_init(sentry_options_t *options)
g_options = options;

// *after* setting the global options, trigger a scope and consent flush,
// since at least crashpad needs that.
// the only way to get a reference to the scope is by locking it, the macro
// does all that at once, including invoking the backends scope flush hook
// since at least crashpad needs that. At this point we also freeze the
// `client_sdk` in the `scope` because some downstream SDKs want to override
// it at runtime via the options interface.
SENTRY_WITH_SCOPE_MUT (scope) {
(void)scope;
if (options->sdk_name) {
sentry_value_t sdk_name
= sentry_value_new_string(options->sdk_name);
sentry_value_set_by_key(scope->client_sdk, "name", sdk_name);
}
sentry_value_freeze(scope->client_sdk);
}
if (backend && backend->user_consent_changed_func) {
backend->user_consent_changed_func(backend);
Expand Down
13 changes: 3 additions & 10 deletions src/sentry_scope.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,9 @@ get_client_sdk(void)
{
sentry_value_t client_sdk = sentry_value_new_object();

SENTRY_WITH_OPTIONS (options) {
sentry_value_t sdk_name = sentry_value_new_string(options->sdk_name);
sentry_value_set_by_key(client_sdk, "name", sdk_name);
}
// in case the SDK is not initialized yet, fallback to build-time value
if (sentry_value_is_null(sentry_value_get_by_key(client_sdk, "name"))) {
sentry_value_t sdk_name = sentry_value_new_string(SENTRY_SDK_NAME);
sentry_value_set_by_key(client_sdk, "name", sdk_name);
}
// the SDK is not initialized yet, fallback to build-time value
sentry_value_t sdk_name = sentry_value_new_string(SENTRY_SDK_NAME);
sentry_value_set_by_key(client_sdk, "name", sdk_name);

sentry_value_t version = sentry_value_new_string(SENTRY_SDK_VERSION);
sentry_value_set_by_key(client_sdk, "version", version);
Expand All @@ -61,7 +55,6 @@ get_client_sdk(void)
sentry_value_set_by_key(client_sdk, "integrations", integrations);
#endif

sentry_value_freeze(client_sdk);
return client_sdk;
}

Expand Down
20 changes: 20 additions & 0 deletions tests/test_integration_stdout.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,26 @@ def test_capture_stdout(cmake):
assert_event(envelope)


def test_dynamic_sdk_name_override(cmake):
tmp_path = cmake(
["sentry_example"],
{
"SENTRY_BACKEND": "none",
"SENTRY_TRANSPORT": "none",
},
)

output = check_output(
tmp_path,
"sentry_example",
["stdout", "override-sdk-name", "capture-event"],
)
envelope = Envelope.deserialize(output)

assert_meta(envelope, sdk_override="sentry.native.android.flutter")
assert_event(envelope)


def test_sdk_name_override(cmake):
sdk_name = "cUsToM.SDK"
tmp_path = cmake(
Expand Down
6 changes: 3 additions & 3 deletions tests/unit/test_concurrency.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SENTRY_TEST(multiple_inits)
SENTRY_LEVEL_INFO, "root", "Hello World!"));

sentry_value_t obj = sentry_value_new_object();
// something that is not a uuid, as this will be forcibly changed
// something that is not a UUID, as this will be forcibly changed
sentry_value_set_by_key(obj, "event_id", sentry_value_new_int32(1234));
sentry_capture_event(obj);

Expand All @@ -64,7 +64,7 @@ thread_worker(void *called)
SENTRY_LEVEL_INFO, "root", "Hello World!"));

sentry_value_t obj = sentry_value_new_object();
// something that is not a uuid, as this will be forcibly changed
// something that is not a UUID, as this will be forcibly changed
sentry_value_set_by_key(obj, "event_id", sentry_value_new_int32(1234));
sentry_capture_event(obj);

Expand All @@ -75,7 +75,7 @@ SENTRY_TEST(concurrent_init)
{
long called = 0;

#define THREADS_NUM 10
#define THREADS_NUM 100
sentry_threadid_t threads[THREADS_NUM];

for (size_t i = 0; i < THREADS_NUM; i++) {
Expand Down
2 changes: 1 addition & 1 deletion tests/unit/test_options.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ SENTRY_TEST(options_sdk_name_invalid)
const char *sdk_name = NULL;
const int result = sentry_options_set_sdk_name(options, sdk_name);

// then the value should should be ignored
// then the value should be ignored
TEST_CHECK_INT_EQUAL(result, 1);
TEST_CHECK_STRING_EQUAL(
sentry_options_get_sdk_name(options), SENTRY_SDK_NAME);
Expand Down

0 comments on commit e340df1

Please sign in to comment.