-
-
Notifications
You must be signed in to change notification settings - Fork 177
fix!(core): Make HubSwitchGuard !Send to prevent thread corruption #957
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
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d0f44d8
fix!(core): Make HubSwitchGuard !Send to prevent thread corruption (#…
szokeasaurusrex ce0b7b3
fixup! logging for span exited on other thread
szokeasaurusrex 9ef8d5f
fixup! Concurrency issue
szokeasaurusrex 6c1fe0b
fixup! only expect pop for Sentry spans
szokeasaurusrex e85c7b3
fixup! Tests for `!Send`
szokeasaurusrex d442adb
fixup! typo
szokeasaurusrex 2e5895a
fixup! only assert panic with debug_assertions enabled
szokeasaurusrex 8d4d761
fixup! changelog entry
szokeasaurusrex 875f2f9
fixup! delete accidentally-committed files
szokeasaurusrex 0cf92a1
fixup! should_panic only with debug_assertions
szokeasaurusrex File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| //! This module contains code for stack-like storage for `HubSwitchGuard`s keyed | ||
| //! by tracing span ID. | ||
|
|
||
| use std::collections::hash_map::Entry; | ||
| use std::collections::HashMap; | ||
|
|
||
| use sentry_core::HubSwitchGuard; | ||
| use tracing_core::span::Id as SpanId; | ||
|
|
||
| /// Holds per-span stacks of `HubSwitchGuard`s to handle span re-entrancy. | ||
| /// | ||
| /// Each time a span is entered, we should push a new guard onto the stack. | ||
| /// When the span exits, we should pop the guard from the stack. | ||
| pub(super) struct SpanGuardStack { | ||
| /// The map of span IDs to their respective guard stacks. | ||
| guards: HashMap<SpanId, Vec<HubSwitchGuard>>, | ||
| } | ||
|
|
||
| impl SpanGuardStack { | ||
| /// Creates an empty guard stack map. | ||
| pub(super) fn new() -> Self { | ||
| Self { | ||
| guards: HashMap::new(), | ||
| } | ||
| } | ||
|
|
||
| /// Pushes a guard for the given span ID, creating the stack if needed. | ||
| pub(super) fn push(&mut self, id: SpanId, guard: HubSwitchGuard) { | ||
| self.guards.entry(id).or_default().push(guard); | ||
| } | ||
|
|
||
| /// Pops the most recent guard for the span ID, removing the stack when empty. | ||
| #[must_use] | ||
| pub(super) fn pop(&mut self, id: SpanId) -> Option<HubSwitchGuard> { | ||
| match self.guards.entry(id) { | ||
| Entry::Occupied(mut entry) => { | ||
| let stack = entry.get_mut(); | ||
| let guard = stack.pop(); | ||
| if stack.is_empty() { | ||
| entry.remove(); | ||
| } | ||
| guard | ||
| } | ||
| Entry::Vacant(_) => None, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::SpanGuardStack; | ||
| use sentry_core::{Hub, HubSwitchGuard}; | ||
| use std::sync::Arc; | ||
| use tracing_core::span::Id as SpanId; | ||
|
|
||
| #[test] | ||
| fn pop_is_lifo() { | ||
| let initial = Hub::current(); | ||
| let hub_a = Arc::new(Hub::new_from_top(initial.clone())); | ||
| let hub_b = Arc::new(Hub::new_from_top(hub_a.clone())); | ||
|
|
||
| let mut stack = SpanGuardStack::new(); | ||
| let id = SpanId::from_u64(1); | ||
|
|
||
| stack.push(id.clone(), HubSwitchGuard::new(hub_a.clone())); | ||
| assert!(Arc::ptr_eq(&Hub::current(), &hub_a)); | ||
|
|
||
| stack.push(id.clone(), HubSwitchGuard::new(hub_b.clone())); | ||
| assert!(Arc::ptr_eq(&Hub::current(), &hub_b)); | ||
|
|
||
| drop(stack.pop(id.clone()).expect("guard for hub_b")); | ||
| assert!(Arc::ptr_eq(&Hub::current(), &hub_a)); | ||
|
|
||
| drop(stack.pop(id.clone()).expect("guard for hub_a")); | ||
| assert!(Arc::ptr_eq(&Hub::current(), &initial)); | ||
|
|
||
| assert!(stack.pop(id).is_none()); | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.