Skip to content

Commit 49554a1

Browse files
committed
fix: Deal with concurrent hub access better (#245)
1 parent 301fcb7 commit 49554a1

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
**Fixes**:
6+
7+
- Better deal with concurrent Hub access.
8+
39
## 0.19.0
410

511
**Highlights**:

sentry-core/src/client.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rand::random;
1111
use crate::constants::SDK_INFO;
1212
use crate::protocol::{ClientSdkInfo, Event};
1313
use crate::types::{Dsn, Uuid};
14-
use crate::{ClientOptions, Integration, Scope, Transport};
14+
use crate::{ClientOptions, Hub, Integration, Scope, Transport};
1515

1616
impl<T: Into<ClientOptions>> From<T> for Client {
1717
fn from(o: T) -> Client {
@@ -93,6 +93,10 @@ impl Client {
9393
/// If the DSN on the options is set to `None` the client will be entirely
9494
/// disabled.
9595
pub fn with_options(mut options: ClientOptions) -> Client {
96+
// Create the main hub eagerly to avoid problems with the background thread
97+
// See https://github.com/getsentry/sentry-rust/issues/237
98+
Hub::with(|_| {});
99+
96100
let create_transport = || {
97101
options.dsn.as_ref()?;
98102
let factory = options.transport.as_ref()?;

sentry-core/src/hub.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,16 @@ impl HubImpl {
4848
}
4949

5050
fn is_active_and_usage_safe(&self) -> bool {
51-
let guard = match self.stack.try_read() {
52-
Err(TryLockError::Poisoned(err)) => err.into_inner(),
53-
Err(TryLockError::WouldBlock) => return false,
51+
let guard = match self.stack.read() {
52+
Err(err) => err.into_inner(),
5453
Ok(guard) => guard,
5554
};
56-
guard.top().client.is_some()
55+
56+
guard
57+
.top()
58+
.client
59+
.as_ref()
60+
.map_or(false, |c| c.is_enabled())
5761
}
5862
}
5963

sentry-log/src/integration.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ impl Integration for LogIntegration {
4242
log::set_max_level(filter);
4343
}
4444

45-
INIT.call_once(|| log::set_boxed_logger(Box::new(Logger::default())).unwrap());
45+
INIT.call_once(|| {
46+
log::set_boxed_logger(Box::new(Logger::default())).ok();
47+
});
4648
}
4749
}
4850

0 commit comments

Comments
 (0)