Hyper Future "cannot be sent between threads safely"

let addr = bind.parse().unwrap();
let server_gen = NewServer { singleton: Server::new(pool, store) };
let server = hyper::Server::bind(&addr)
    .serve(server_gen)
    .map_err(|e| error!("Server Error [500]: {}", e));
rt::run(server);

I have a Hyper service (Server) and a struct that implements NewService (NewServer), but when I try to compile this, I get the following error:

error[E0277]: (dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + 'static) cannot be sent between threads safely
–> src/main.rs:55:10 |
55 | .serve(server_gen)
| ^^^^^ (dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + 'static) cannot be sent between threads safely
|
= help: the trait std::marker::Send is not implemented for (dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + 'static)
= note: required because of the requirements on the impl of std::marker::Send for std::ptr::Unique<(dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Bo dy>> + 'static)>
= note: required because it appears within the type std::boxed::Box<(dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + 'static)>

Server implements Send, it looks like it is saying the actual boxed future doesn’t implement Send?

Oh, I think I need to actually add the Send bound to the associated Future type of the Service impl for Server

Can you explain your solution, please?

Please don't revive year old threads.

To answer your question, they needed to change

dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + 'static

to

dyn hyper::rt::Future<Error=hyper::Error, Item=hyper::Response<hyper::Body>> + Send + 'static

In order to send it across threads

@bingli224 You can always reply as a linked topic if you want to:

image

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.