Skip to content

Cannot be shared between threads safely #241

Closed
@frederikbosch

Description

@frederikbosch

Bug Report

I cannot solve the following error. I guess I lack knowledge, but I have no idea where to start solving this one.

error[E0277]: `(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)` cannot be shared between threads safely

Version

cargo / rust 1.40.0
tonic 0.1.0.

Platform

Ubuntu 18.04

Crates

reqwest = { version = "0.10.0", features = ["json"]}

Description

What I am trying to is the following. I have service that receives a gRPC call from client. That service will fire a HTTP request via reqwest before it answers the client. That reqwest is also async. In pseudo-code (sort of) I have the following.

impl Initialization for InitializationService {
  type InitializeStream = Pin<Box<dyn Stream<Item = Result<InitializeResponse, Status>> + Send + Sync + 'static>>;

  async fn initialize(&self, request: Request<tonic::Streaming<InitializeRequest>>) -> Result<Response<Self::InitializeStream>, Status> {
    // ....
    let stream = request.into_inner();
    let output = async_stream::try_stream! {
       futures::pin_mut!(stream);
       while let Some(a) = stream.next().await {
          &lib.fire_http_req(a.x.clone(), y, z).await;
       }
    };
    // ....
  }
}

And then in the library I am calling I have.

impl Lib {
  pub async fn fire_http_req(&self, x: PathBuf, y: String, z: String) -> Result<(), String> {
    let resp: Vec<serde_json::Value> = reqwest::get("http://localhost")
            .await
            .unwrap()
            .json()
            .await
            .unwrap();
   Ok(())
  }
}

The result is huge error.

error[E0277]: `(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)` cannot be shared between threads safely
  |
  = help: the trait `std::marker::Sync` is not implemented for `(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)`
  = note: required because of the requirements on the impl of `std::marker::Sync` for `std::ptr::Unique<(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)>`
  = note: required because it appears within the type `std::boxed::Box<(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)>`
  = note: required because it appears within the type `std::pin::Pin<std::boxed::Box<(dyn core::future::future::Future<Output = std::result::Result<http::response::Response<hyper::body::body::Body>, hyper::error::Error>> + std::marker::Send + 'static)>>`
  = note: required because it appears within the type `hyper::client::ResponseFuture`

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions