diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f8075224..20ae0d873 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ **Fixes**: - Better error messages in `sentry_transport_curl`. ([#777](https://github.com/getsentry/sentry-native/pull/777)) +- Fix sporadic crash on Windows due to race condition when initializing background-worker thread-id. ([#785](https://github.com/getsentry/sentry-native/pull/785)) - Increased curl headers buffer size to 512 (in `sentry_transport_curl`). ([#784](https://github.com/getsentry/sentry-native/pull/784)) **Internal**: diff --git a/src/sentry_sync.c b/src/sentry_sync.c index a6b6380a1..e81d776c2 100644 --- a/src/sentry_sync.c +++ b/src/sentry_sync.c @@ -20,6 +20,12 @@ typedef struct { } THREADNAME_INFO; # pragma pack(pop) +sentry_threadid_t +sentry__thread_get_current_threadid() +{ + return GetCurrentThread(); +} + int sentry__thread_setname(sentry_threadid_t thread_id, const char *thread_name) { @@ -61,6 +67,12 @@ sentry__thread_setname(sentry_threadid_t thread_id, const char *thread_name) return 0; } #else +sentry_threadid_t +sentry__thread_get_current_threadid() +{ + return pthread_self(); +} + int sentry__thread_setname(sentry_threadid_t thread_id, const char *thread_name) { @@ -218,7 +230,12 @@ worker_thread(void *data) // should be called inside thread itself because of MSVC issues and mac // https://randomascii.wordpress.com/2015/10/26/thread-naming-in-windows-time-for-something-better/ - if (sentry__thread_setname(bgw->thread_id, bgw->thread_name)) { + // Additionally, `bgw->thread_id` cannot be used reliably because it is + // subject to initialization race condition: current thread might be running + // before `bgw->thread_id` is initialized in the thread that started the + // background worker. + if (sentry__thread_setname( + sentry__thread_get_current_threadid(), bgw->thread_name)) { SENTRY_WARN("failed to set background worker thread name"); }