Troubleshooting - Help and questions

Axum error "the trait bound Handler<_, _> is not satisfied". How do I find the cause?

#axum#compile-errors#debug-handler#troubleshootingasked 0 answers0 views
ReplyWatch
Share
Hacker NewsRedditLinkedInE-mail

Short answer

The compiler says a function is not a Handler without saying why. Add #[axum::debug_handler] above the function and compile again: the macro checks each requirement separately and reports the real cause, for example a value that is not Send being held across an await.

Checked with rustc 1.93.0, axum 0.8.9 with the macros feature, tokio 1.54.1, Windows 11; errors pasted from cargo check

Frequently asked question, written and answered by the Ruststudio team. Replies are open: corrections are welcome.

#1

Symptom#

Router::route("/", get(handler)) fails to compile:

text
error[E0277]: the trait bound `fn() -> impl Future<Output = String> {handler}: Handler<_, _>` is not satisfied
   --> examples\f11.rs:12:53
    |
 12 |     let _app: Router = Router::new().route("/", get(handler));
    |                                                 --- ^^^^^^^ the trait `Handler<_, _>` is not implemented for fn item `fn() -> impl Future<Output = String> {handler}`
    |                                                 |
    |                                                 required by a bound introduced by this call
    |
    = note: Consider using `#[axum::debug_handler]` to improve the error message
...

Cause#

A handler is any async function whose arguments are extractors and whose result converts into a response, and whose future can be sent between threads. When one requirement fails, the compiler can only say that the whole Handler trait is not implemented. This handler keeps an Rc (which is not Send) alive across an .await:

examples/f11.rsrust
use axum::{routing::get, Router};
use std::rc::Rc;

async fn handler() -> String {
    let counter = Rc::new(5);
    tokio::time::sleep(std::time::Duration::from_millis(1)).await;
    format!("{counter}")
}

#[tokio::main]
async fn main() {
    let _app: Router = Router::new().route("/", get(handler));
}

Fix#

Enable the macros feature (axum = { version = "0.8.9", features = ["macros"] }) and add #[debug_handler] above the function. It checks the pieces one by one:

examples/f11b.rsrust
use axum::{debug_handler, routing::get, Router};
use std::rc::Rc;

#[debug_handler]
async fn handler() -> String {
    let counter = Rc::new(5);
    tokio::time::sleep(std::time::Duration::from_millis(1)).await;
    format!("{counter}")
}

#[tokio::main]
async fn main() {
    let _app: Router = Router::new().route("/", get(handler));
}

The error now names the cause:

text
error: future cannot be sent between threads safely
 --> examples\f11b.rs:4:1
  |
4 | #[debug_handler]
  | ^^^^^^^^^^^^^^^^ future returned by `handler` is not `Send`
  |
  = help: within `impl Future<Output = String>`, the trait `Send` is not implemented for `Rc<i32>`
note: future is not `Send` as this value is used across an await
 --> examples\f11b.rs:7:61
  |
6 |     let counter = Rc::new(5);
  |         ------- has type `Rc<i32>` which is not `Send`
7 |     tokio::time::sleep(std::time::Duration::from_millis(1)).await;
  |                                                             ^^^^^ await occurs here, with `counter` maybe used later
note: required by a bound in `__axum_macros_check_handler_future::check`
 --> examples\f11b.rs:4:1
  |
4 | #[debug_handler]
  | ^^^^^^^^^^^^^^^^ required by this bound in `check`
...

Replace Rc with Arc, or end the value's life before the .await by putting it in a smaller block. Remove #[debug_handler] afterwards if you like: it only improves messages.

Common mistakes#

  • Holding a std::sync::MutexGuard over an .await. It is not Send either, and the macro reports it the same way.

Why must the handler's future be Send?#

The runtime may move a request's task from one worker thread to another between awaits, so everything alive across an await must be safe to move.

Sources#

    QuoteReply
    Share
    Hacker NewsRedditLinkedInE-mail

    No answers yet

    No answers yet. If you know, answer it.