Skip to content

Commit 3e596e8

Browse files
fix: re-add setting thread name for Windows transport (#1424)
* add unit test * only run on test_unit_transport * add `sentry__bgworker_setname` again * Update tests/unit/test_basic.c * update CHANGELOG.md
1 parent 0a0d729 commit 3e596e8

File tree

9 files changed

+82
-0
lines changed

9 files changed

+82
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
**Fixes**:
66

77
- Add logs flush on crash. This is not available for macOS with the `crashpad` backend. ([#1404](https://github.com/getsentry/sentry-native/pull/1404))
8+
- Re-add setting thread name for Windows transport. ([#1424](https://github.com/getsentry/sentry-native/pull/1424))
89

910
**Internal**:
1011

src/sentry_sync.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,14 @@ sentry__bgworker_setname(sentry_bgworker_t *bgw, const char *thread_name)
495495
bgw->thread_name = sentry__string_clone(thread_name);
496496
}
497497

498+
#ifdef SENTRY_UNITTEST
499+
const char *
500+
sentry__bgworker_get_thread_name(sentry_bgworker_t *bgw)
501+
{
502+
return bgw ? bgw->thread_name : NULL;
503+
}
504+
#endif
505+
498506
#if defined(SENTRY_PLATFORM_UNIX) || defined(SENTRY_PLATFORM_NX)
499507
# include "sentry_cpu_relax.h"
500508

src/sentry_sync.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -460,6 +460,14 @@ int sentry__bgworker_shutdown(sentry_bgworker_t *bgw, uint64_t timeout);
460460
*/
461461
void sentry__bgworker_setname(sentry_bgworker_t *bgw, const char *thread_name);
462462

463+
#ifdef SENTRY_UNITTEST
464+
/**
465+
* Test helper function to get the thread name from a bgworker.
466+
* Only available in unit tests.
467+
*/
468+
const char *sentry__bgworker_get_thread_name(sentry_bgworker_t *bgw);
469+
#endif
470+
463471
/**
464472
* This will submit a new task to the background thread.
465473
*

src/sentry_transport.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,15 @@ sentry_transport_free(sentry_transport_t *transport)
158158
sentry_free(transport);
159159
}
160160

161+
#ifdef SENTRY_UNITTEST
162+
void *
163+
sentry__transport_get_bgworker(sentry_transport_t *transport)
164+
{
165+
// For HTTP transports (curl/winhttp), the transport state is the bgworker
166+
return transport ? transport->state : NULL;
167+
}
168+
#endif
169+
161170
#ifdef SENTRY_TRANSPORT_COMPRESSION
162171
static bool
163172
gzipped_with_compression(const char *body, const size_t body_len,

src/sentry_transport.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,14 @@ sentry_transport_t *sentry__transport_new_default(void);
5656
size_t sentry__transport_dump_queue(
5757
sentry_transport_t *transport, sentry_run_t *run);
5858

59+
#ifdef SENTRY_UNITTEST
60+
/**
61+
* Test helper function to get the bgworker from a transport.
62+
* Only available in unit tests and only works for HTTP transports.
63+
*/
64+
void *sentry__transport_get_bgworker(sentry_transport_t *transport);
65+
#endif
66+
5967
typedef struct sentry_prepared_http_header_s {
6068
const char *key;
6169
char *value;

src/transports/sentry_transport_winhttp.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,8 @@ sentry__winhttp_transport_start(
9999
state->user_agent = sentry__string_to_wstr(opts->user_agent);
100100
state->debug = opts->debug;
101101

102+
sentry__bgworker_setname(bgworker, opts->transport_thread_name);
103+
102104
const char *env_proxy = opts->dsn
103105
? getenv(opts->dsn->is_secure ? "https_proxy" : "http_proxy")
104106
: NULL;

tests/test_unit.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55

66

77
def test_unit(cmake, unittest):
8+
if unittest in ["basic_transport_thread_name"]:
9+
pytest.skip("excluded from unit test-suite")
810
cwd = cmake(
911
["sentry_test_unit"],
1012
{"SENTRY_BACKEND": "none", "SENTRY_TRANSPORT": "none"},
@@ -27,6 +29,8 @@ def test_unit_transport(cmake, unittest):
2729

2830

2931
def test_unit_with_test_path(cmake, unittest):
32+
if unittest in ["basic_transport_thread_name"]:
33+
pytest.skip("excluded from unit test-suite")
3034
cwd = cmake(
3135
["sentry_test_unit"],
3236
{"SENTRY_BACKEND": "none", "SENTRY_TRANSPORT": "none"},

tests/unit/test_basic.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
#include "sentry_core.h"
22
#include "sentry_database.h"
3+
#include "sentry_options.h"
34
#include "sentry_string.h"
5+
#include "sentry_sync.h"
46
#include "sentry_testsupport.h"
7+
#include "sentry_transport.h"
58

69
static void
710
send_envelope_test_basic(sentry_envelope_t *envelope, void *data)
@@ -304,3 +307,41 @@ SENTRY_TEST(capture_minidump_invalid_path)
304307

305308
sentry_close();
306309
}
310+
311+
SENTRY_TEST(basic_transport_thread_name)
312+
{
313+
const char *expected_thread_name = "sentry::worker_thread";
314+
315+
SENTRY_TEST_OPTIONS_NEW(options);
316+
sentry_options_set_dsn(options, "https://[email protected]/42");
317+
sentry_options_set_transport_thread_name(options, expected_thread_name);
318+
319+
// Initialize sentry which should start the transport and set the thread
320+
// name
321+
TEST_CHECK_INT_EQUAL(sentry_init(options), 0);
322+
323+
// Access the transport through runtime options to check if thread name was
324+
// set
325+
SENTRY_WITH_OPTIONS (runtime_options) {
326+
TEST_ASSERT(!!runtime_options->transport);
327+
328+
// Get the bgworker from the transport (for HTTP transports)
329+
sentry_bgworker_t *bgworker
330+
= (sentry_bgworker_t *)sentry__transport_get_bgworker(
331+
runtime_options->transport);
332+
TEST_ASSERT(!!bgworker);
333+
334+
// Check if the thread name was properly set on the bgworker
335+
const char *actual_thread_name
336+
= sentry__bgworker_get_thread_name(bgworker);
337+
338+
if (actual_thread_name) {
339+
TEST_CHECK_STRING_EQUAL(actual_thread_name, expected_thread_name);
340+
} else {
341+
TEST_CHECK(false); // Fail if thread_name is NULL
342+
TEST_MSG("Transport thread name was not set ");
343+
}
344+
}
345+
346+
sentry_close();
347+
}

tests/unit/tests.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ XX(basic_logging_functionality)
2121
XX(basic_spans)
2222
XX(basic_tracing_context)
2323
XX(basic_transaction)
24+
XX(basic_transport_thread_name)
2425
XX(basic_write_envelope_to_file)
2526
XX(bgworker_flush)
2627
XX(breadcrumb_without_type_or_message_still_valid)

0 commit comments

Comments
 (0)