Search code examples
rustactix-web

How can I stop Actix Web from multiplying error messages?


When debugging an Actix Web 4.2.1 Rust application, every error message gets multiplied 10 times (= my number of physical CPU cores). How do I get it to show the error only once?

use actix_web::*;

#[get("/")]
async fn panic() -> impl Responder {
    if true {panic!("this error message is shown too many times");}  
    HttpResponse::Ok().body("Hello world!")  
}

#[actix_web::main]
async fn main() -> std::io::Result<()> {
    HttpServer::new(|| {
        App::new()
            .service(panic)
    })
    .bind(("127.0.0.1", 8080))?
    .run()
    .await
}

After opening localhost:8080 in a browser, the Rust application above prints the following:

$ cargo run
   Compiling actitest v0.1.0 (/tmp/actitest)
    Finished dev [unoptimized + debuginfo] target(s) in 1.52s
     Running `target/debug/actitest`
thread 'actix-rt|system:0|arbiter:0' panicked at 'this error message is shown too many times', src/main.rs:5:14
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
thread 'actix-rt|system:0|arbiter:1' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:2' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:3' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:4' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:5' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:6' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:7' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:8' panicked at 'this error message is shown too many times', src/main.rs:5:14
thread 'actix-rt|system:0|arbiter:9' panicked at 'this error message is shown too many times', src/main.rs:5:14

How can I get this message to be shown a single time only?


Solution

  • You get multiple messages because your browser tries to be helpful and calls the endpoint multiple times assuming a spurious failure might have occured. Thus you are in fact getting the error multiple times until all arbiters die and have to be generated wich takes a short while. To get around this you could use a less 'smart' tool to access the endpoint:

    curl localhost:8080/
    

    will only produce the error once.