Skip to content

Conversation

@szokeasaurusrex
Copy link
Member

@szokeasaurusrex szokeasaurusrex commented Jan 14, 2026

Description

HubSwitchGuard manages thread-local hub state but was Send, allowing it to be moved to another thread. When dropped on the wrong thread, it could corrupt that thread's hub state instead of restoring the original thread.

This PR makes HubSwitchGuard !Send by adding PhantomData<MutexGuard<'static, ()>> while keeping it Sync. The type system now prevents the guard from being moved across threads at compile time.

To ensure guards are always dropped on the originating thread, sentry-tracing now stores them in thread-local storage keyed by span ID rather than in span extensions.

Issues

@linear
Copy link

linear bot commented Jan 14, 2026

@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/hubswitchguard branch from 21b407b to 1dea844 Compare January 14, 2026 12:28
@szokeasaurusrex szokeasaurusrex changed the title fix(core): Make HubSwitchGuard !Send to prevent thread corruption fix!(core): Make HubSwitchGuard !Send to prevent thread corruption Jan 14, 2026
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/hubswitchguard branch from e78483a to 8abf004 Compare January 14, 2026 12:54
@szokeasaurusrex szokeasaurusrex requested a review from lcian January 26, 2026 16:19
HubSwitchGuard manages thread-local hub state but was Send, allowing it
to be moved to another thread. When dropped on the wrong thread, it would
corrupt that thread's hub state instead of restoring the original thread.

To fix this, add PhantomData<MutexGuard<'static, ()>> to make the guard
!Send while keeping it Sync. This prevents the guard from being moved
across threads at compile time.

Additionally, refactor sentry-tracing to store guards in thread-local
storage keyed by span ID instead of in span extensions. This fixes a
related bug where multiple threads entering the same span would clobber
each other's guards.

Fixes #943
Fixes #946
Refs RUST-130
Refs RUST-132

Co-Authored-By: Claude <noreply@anthropic.com>
@szokeasaurusrex szokeasaurusrex force-pushed the szokeasaurusrex/hubswitchguard branch from 8abf004 to 1255c8a Compare January 28, 2026 12:36
@szokeasaurusrex szokeasaurusrex marked this pull request as ready for review January 28, 2026 14:23
Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HubSwitchGuard should not be Send

3 participants