Skip to content

Commit d262d71

Browse files
committed
web: convert panics into server error, re-raise from spawn_blocking
* when we delay work with `spawn_blocking`, we might get panics. These we just want to resume / re-raise. * panic in the general webserver should be catched and converted into a server error
1 parent 5df77e3 commit d262d71

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ axum-extra = "0.7.0"
9191
hyper = { version = "0.14.15", default-features = false }
9292
tower = "0.4.11"
9393
tower-service = "0.3.2"
94-
tower-http = { version = "0.4.0", features = ["fs", "trace", "timeout"] }
94+
tower-http = { version = "0.4.0", features = ["fs", "trace", "timeout", "catch-panic"] }
9595
mime = "0.3.16"
9696
percent-encoding = "2.2.0"
9797

src/utils/mod.rs

+7-4
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@ mod html;
2020
mod queue;
2121
pub(crate) mod queue_builder;
2222
mod rustc_version;
23-
use anyhow::{Context as _, Result};
23+
use anyhow::Result;
2424
use postgres::Client;
2525
use serde::de::DeserializeOwned;
2626
use serde::Serialize;
27+
use std::panic;
2728
use tracing::error;
2829
pub(crate) mod sized_buffer;
2930

@@ -111,9 +112,11 @@ where
111112
F: FnOnce() -> Result<R> + Send + 'static,
112113
R: Send + 'static,
113114
{
114-
tokio::task::spawn_blocking(f)
115-
.await
116-
.context("failed to join thread")?
115+
match tokio::task::spawn_blocking(f).await {
116+
Ok(result) => result,
117+
Err(err) if err.is_panic() => panic::resume_unwind(err.into_panic()),
118+
Err(err) => Err(err.into()),
119+
}
117120
}
118121

119122
pub(crate) fn retry<T>(mut f: impl FnMut() -> Result<T>, max_attempts: u32) -> Result<T> {

src/web/mod.rs

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ use serde::Serialize;
4949
use std::borrow::Borrow;
5050
use std::{borrow::Cow, net::SocketAddr, sync::Arc};
5151
use tower::ServiceBuilder;
52-
use tower_http::{timeout::TimeoutLayer, trace::TraceLayer};
52+
use tower_http::{catch_panic::CatchPanicLayer, timeout::TimeoutLayer, trace::TraceLayer};
5353
use url::form_urlencoded;
5454

5555
// from https://github.com/servo/rust-url/blob/master/url/src/parser.rs
@@ -275,6 +275,7 @@ pub(crate) fn build_axum_app(
275275
.layer(TraceLayer::new_for_http())
276276
.layer(sentry_tower::NewSentryLayer::new_from_top())
277277
.layer(sentry_tower::SentryHttpLayer::with_transaction())
278+
.layer(CatchPanicLayer::new())
278279
.layer(option_layer(
279280
config
280281
.report_request_timeouts

0 commit comments

Comments
 (0)