Skip to content

Commit 53ceb2b

Browse files
committed
fix: Initialize Sentry before tokio
The way Sentry does its Hub/Client-propagation across threads means that it has to be initialized on the main thread for this to work, before any other threads are being spawned. In other words, Sentry is notorious for being incompatible with `#[tokio::main]`. So lets split the main function into a sync portion initializing all the observability crates, before spawning the tokio runtime and the main server.
1 parent 9c4be5d commit 53ceb2b

File tree

1 file changed

+19
-17
lines changed

1 file changed

+19
-17
lines changed

objectstore-server/src/main.rs

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -17,35 +17,37 @@ mod http;
1717
mod observability;
1818
mod state;
1919

20-
#[tokio::main]
21-
async fn main() -> Result<()> {
20+
fn main() -> Result<()> {
2221
let config = Config::from_env()?;
22+
tracing::debug!(?config, "Starting service");
2323

2424
// Ensure a rustls crypto provider is installed, required on distroless.
2525
rustls::crypto::ring::default_provider()
2626
.install_default()
2727
.expect("Failed to install rustls crypto provider");
2828

29-
let metrics_guard = maybe_initialize_metrics(&config)?;
3029
let _sentry_guard = maybe_initialize_sentry(&config);
30+
let metrics_guard = maybe_initialize_metrics(&config)?;
3131
initialize_tracing(&config);
3232

33-
tracing::debug!(?config, "Starting service");
34-
let state = State::new(config).await?;
35-
tokio::spawn(http::server(state));
33+
let runtime = tokio::runtime::Runtime::new()?;
34+
runtime.block_on(async move {
35+
let state = State::new(config).await?;
36+
tokio::spawn(http::server(state));
3637

37-
elegant_departure::tokio::depart()
38-
.on_termination()
39-
.on_sigint()
40-
.on_signal(SignalKind::hangup())
41-
.on_signal(SignalKind::quit())
42-
.await;
38+
elegant_departure::tokio::depart()
39+
.on_termination()
40+
.on_sigint()
41+
.on_signal(SignalKind::hangup())
42+
.on_signal(SignalKind::quit())
43+
.await;
4344

44-
if let Some(metrics_guard) = metrics_guard {
45-
metrics_guard.flush(None).await?;
46-
}
45+
if let Some(metrics_guard) = metrics_guard {
46+
metrics_guard.flush(None).await?;
47+
}
4748

48-
tracing::info!("shutting down");
49+
tracing::info!("shutting down");
4950

50-
Ok(())
51+
Ok(())
52+
})
5153
}

0 commit comments

Comments
 (0)