Skip to content

[question] Why some code compile error? #139

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

Closed
fawdlstty opened this issue Jan 7, 2021 · 3 comments
Closed

[question] Why some code compile error? #139

fawdlstty opened this issue Jan 7, 2021 · 3 comments

Comments

@fawdlstty
Copy link

I had a compiler crash while compiling a piece of code #79783, after fix the bug, I found that the same piece of code failed to compile after using async-trait. Example:

struct A {}
impl A {
    async fn xx_test (&self) {
        match actix_web::HttpServer::new (|| {
            actix_web::App::new ()
        }).bind ("127.0.0.1:8080") {
            Ok (mut _server) => { _server.run ().await; () },
            Err (_e) => println! ("http listen failed: {}", _e.to_string ()),
        }
    }
}

#[async_trait]
pub trait B: Send + Sync {
    async fn test_func (&self);
}
struct C {}
#[async_trait]
impl B for C {
    async fn test_func (&self) {
        // compile error
        match actix_web::HttpServer::new (|| {
            actix_web::App::new ()
        }).bind ("127.0.0.1:8080") {
            Ok (mut _server) => { _server.run ().await; () },
            Err (_e) => println! ("http listen failed: {}", _e.to_string ()),
        }
    }
}

Why is that? How to fix it?

@fawdlstty
Copy link
Author

If I use the #[async_trait(?Send)], it will compile and pass, but the trait won't Send, and I don't want that

@fawdlstty fawdlstty changed the title some code compile error [question] Why some code compile error? Jan 8, 2021
@dtolnay
Copy link
Owner

dtolnay commented Jan 8, 2021

I'll close this since your question is why actix-web results in non-Send futures, not anything about async-trait.

You can see in the error from this code that the future is non-Send because of what's stored in HttpServer:

use actix_web::{App, HttpServer};

async fn repro() {
    match HttpServer::new(App::new).bind("127.0.0.1:8080") {
        Ok(server) => { let _ = server.run().await; }
        Err(err) => eprintln!("error: {}", err),
    }
}

fn main() {
    fn assert_send(_: impl Send) {}
    assert_send(repro());
}
error: future cannot be sent between threads safely
  --> src/main.rs:12:5
   |
11 |     fn assert_send(_: impl Send) {}
   |                            ---- required by this bound in `assert_send`
12 |     assert_send(repro());
   |     ^^^^^^^^^^^ future returned by `repro` is not `Send`
   |
   = help: the trait `std::marker::Send` is not implemented for `(dyn std::any::Any + 'static)`
note: future is not `Send` as this value is used across an await
  --> src/main.rs:5:33
   |
4  |     match HttpServer::new(App::new).bind("127.0.0.1:8080") {
   |           ------------------------- has type `HttpServer<fn() -> App<actix_web::app_service::AppEntry, actix_web::dev::Body> {App::<actix_web::app_service::AppEntry, actix_web::dev::Body>::new}, App<actix_web::app_service::AppEntry, actix_web::dev::Body>, actix_web::app_service::AppInit<actix_web::app_service::AppEntry, actix_web::dev::Body>, actix_web::dev::Body>` which is not `Send`
5  |         Ok(server) => { let _ = server.run().await; }
   |                                 ^^^^^^^^^^^^^^^^^^ await occurs here, with `HttpServer::new(App::new)` maybe used later
...
8  | }
   | - `HttpServer::new(App::new)` is later dropped here

@dtolnay dtolnay closed this as completed Jan 8, 2021
@fawdlstty
Copy link
Author

So I should use send_wrapper, right? Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants