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: avoid race condition when starting bgw thread #785

Merged
merged 3 commits into from
Jan 9, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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**:
Expand Down
19 changes: 18 additions & 1 deletion src/sentry_sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand Down Expand Up @@ -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)
{
Expand Down Expand Up @@ -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");
}

Expand Down