-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Strange debug_assert!
and runtime panics
#1579
Comments
Below is a code sample, but this assertion is not triggered: use axum::{
body::{Body, BoxBody},
extract::Host,
http::{Request, StatusCode},
response::{IntoResponse, Response},
routing::{any, get},
Router,
};
use std::net::SocketAddr;
use tower::ServiceExt;
#[tokio::main]
async fn main() {
let app = Router::new()
.route("/", any(|| async { "hello, world" }))
.route(
"/*path",
any(move |host: Host, req: Request<Body>| any_path(host, req)),
);
let addr = SocketAddr::from(([127, 0, 0, 1], 80));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
}
fn service_a() -> Router {
Router::new().route("/service_a", get(|| async { "this is service A" }))
}
fn service_b() -> Router {
Router::new().route("/service_b", get(|| async { "this is service B" }))
}
pub async fn call_router(router: Router, req: Request<Body>) -> Response<BoxBody> {
router.oneshot(req).await.into_response()
}
async fn any_path(Host(hostname): Host, mut req: Request<Body>) -> Response {
match hostname.as_ref() {
"a.service.domain" => call_router(service_a(), req).await,
"b.service.domain" => call_router(service_b(), req).await,
_ => (StatusCode::NOT_FOUND, "not found").into_response(),
}
} If add let matched = req.extensions_mut().remove::<axum::extract::MatchedPath>();
println!("{:?}", matched); The value of |
The assert guards against possible bugs. That extension shouldn't exist there so be assert that it isn't. Can you provide a minimal reproduction script that hits it? |
It seems this code reproduces the problem use axum::{body::Body, extract::Host, http::Request, routing::any, Router};
use std::net::SocketAddr;
use tower::ServiceExt;
#[tokio::main]
pub async fn main() {
let app = Router::new().route(
"/*path",
any(move |host: Host, mut req: Request<Body>| {
// Uncomment to fix
// let matched = req.extensions_mut().remove::<axum::extract::MatchedPath>();
// println!("{:?}", matched);
Router::new().nest("/", Router::new()).oneshot(req)
}),
);
let addr = SocketAddr::from(([127, 0, 0, 1], 80));
println!("listening on {}", addr);
axum::Server::bind(&addr)
.serve(app.into_make_service())
.await
.unwrap();
} request with
|
This should be closed, right? It was fixed in #1711. |
Yes that is correct. Thanks for noticing! |
Bug Report
Version
0.6.0-rc.5
,0.6.0
Platform
Windows 10 x64
Crates
axum
Description
I am writing a service that selects a router based on the hostname and executes it using
router.oneshot(req)
The code is similar to below:
But I get the following error:
axum/axum/src/extract/matched_path.rs
Lines 142 to 146 in 8d62697
The problem occurs on line 146
axum/axum/src/extract/matched_path.rs
Line 146 in 8d62697
This
debug_assert!
confuses me, why does it exist? Or what is it used for?Currently I avoid triggering this assertion while debugging by adding
req.extensions_mut().remove::<axum::extract::MatchedPath>();
And
matched
will get:Some(MatchedPath("/*path"))
I wrote a simple code sample and it didn't throw this error, so this exception is very strange.
The text was updated successfully, but these errors were encountered: