Skip to content

Commit e340df1

Browse files
authored
fix: deadlock in dynamic sdk-name scope init... (#858)
1 parent 1a7184b commit e340df1

File tree

8 files changed

+47
-18
lines changed

8 files changed

+47
-18
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Fixes**:
6+
7+
- Remove deadlock pattern in dynamic sdk-name assignment ([#858](https://github.com/getsentry/sentry-native/pull/858))
8+
39
## 0.6.4
410

511
**Fixes**:

CONTRIBUTING.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,7 @@ The example currently supports the following commands:
144144
- `discarding-before-send`: Installs a `before_send()` callback that discards the event.
145145
- `on-crash`: Installs an `on_crash()` callback that retains the crash event.
146146
- `discarding-on-crash`: Installs an `on_crash()` callback that discards the crash event.
147+
- `override-sdk-name`: Changes the SDK name via the options at runtime.
147148

148149
Only on Windows using crashpad with its WER handler module:
149150

examples/example.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ main(int argc, char **argv)
218218
options, discarding_on_crash_callback, NULL);
219219
}
220220

221+
if (has_arg(argc, argv, "override-sdk-name")) {
222+
sentry_options_set_sdk_name(options, "sentry.native.android.flutter");
223+
}
224+
221225
sentry_init(options);
222226

223227
if (!has_arg(argc, argv, "no-setup")) {

src/sentry_core.c

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,11 +163,16 @@ sentry_init(sentry_options_t *options)
163163
g_options = options;
164164

165165
// *after* setting the global options, trigger a scope and consent flush,
166-
// since at least crashpad needs that.
167-
// the only way to get a reference to the scope is by locking it, the macro
168-
// does all that at once, including invoking the backends scope flush hook
166+
// since at least crashpad needs that. At this point we also freeze the
167+
// `client_sdk` in the `scope` because some downstream SDKs want to override
168+
// it at runtime via the options interface.
169169
SENTRY_WITH_SCOPE_MUT (scope) {
170-
(void)scope;
170+
if (options->sdk_name) {
171+
sentry_value_t sdk_name
172+
= sentry_value_new_string(options->sdk_name);
173+
sentry_value_set_by_key(scope->client_sdk, "name", sdk_name);
174+
}
175+
sentry_value_freeze(scope->client_sdk);
171176
}
172177
if (backend && backend->user_consent_changed_func) {
173178
backend->user_consent_changed_func(backend);

src/sentry_scope.c

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,9 @@ get_client_sdk(void)
2929
{
3030
sentry_value_t client_sdk = sentry_value_new_object();
3131

32-
SENTRY_WITH_OPTIONS (options) {
33-
sentry_value_t sdk_name = sentry_value_new_string(options->sdk_name);
34-
sentry_value_set_by_key(client_sdk, "name", sdk_name);
35-
}
36-
// in case the SDK is not initialized yet, fallback to build-time value
37-
if (sentry_value_is_null(sentry_value_get_by_key(client_sdk, "name"))) {
38-
sentry_value_t sdk_name = sentry_value_new_string(SENTRY_SDK_NAME);
39-
sentry_value_set_by_key(client_sdk, "name", sdk_name);
40-
}
32+
// the SDK is not initialized yet, fallback to build-time value
33+
sentry_value_t sdk_name = sentry_value_new_string(SENTRY_SDK_NAME);
34+
sentry_value_set_by_key(client_sdk, "name", sdk_name);
4135

4236
sentry_value_t version = sentry_value_new_string(SENTRY_SDK_VERSION);
4337
sentry_value_set_by_key(client_sdk, "version", version);
@@ -61,7 +55,6 @@ get_client_sdk(void)
6155
sentry_value_set_by_key(client_sdk, "integrations", integrations);
6256
#endif
6357

64-
sentry_value_freeze(client_sdk);
6558
return client_sdk;
6659
}
6760

tests/test_integration_stdout.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,26 @@ def test_capture_stdout(cmake):
4646
assert_event(envelope)
4747

4848

49+
def test_dynamic_sdk_name_override(cmake):
50+
tmp_path = cmake(
51+
["sentry_example"],
52+
{
53+
"SENTRY_BACKEND": "none",
54+
"SENTRY_TRANSPORT": "none",
55+
},
56+
)
57+
58+
output = check_output(
59+
tmp_path,
60+
"sentry_example",
61+
["stdout", "override-sdk-name", "capture-event"],
62+
)
63+
envelope = Envelope.deserialize(output)
64+
65+
assert_meta(envelope, sdk_override="sentry.native.android.flutter")
66+
assert_event(envelope)
67+
68+
4969
def test_sdk_name_override(cmake):
5070
sdk_name = "cUsToM.SDK"
5171
tmp_path = cmake(

tests/unit/test_concurrency.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ SENTRY_TEST(multiple_inits)
4343
SENTRY_LEVEL_INFO, "root", "Hello World!"));
4444

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

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

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

@@ -75,7 +75,7 @@ SENTRY_TEST(concurrent_init)
7575
{
7676
long called = 0;
7777

78-
#define THREADS_NUM 10
78+
#define THREADS_NUM 100
7979
sentry_threadid_t threads[THREADS_NUM];
8080

8181
for (size_t i = 0; i < THREADS_NUM; i++) {

tests/unit/test_options.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ SENTRY_TEST(options_sdk_name_invalid)
4343
const char *sdk_name = NULL;
4444
const int result = sentry_options_set_sdk_name(options, sdk_name);
4545

46-
// then the value should should be ignored
46+
// then the value should be ignored
4747
TEST_CHECK_INT_EQUAL(result, 1);
4848
TEST_CHECK_STRING_EQUAL(
4949
sentry_options_get_sdk_name(options), SENTRY_SDK_NAME);

0 commit comments

Comments
 (0)