Skip to content

Unable to use RequestReplier in async task #38

@bemyak

Description

@bemyak

This sample code gives an error:

#[tokio::main]
async fn main() {
    let ctx = tmq::Context::new();
    let handle = tokio::task::spawn(async move {
        let mut socket = tmq::reply(&ctx).bind("tcp://0.0.0.0:5001").unwrap();
        loop {
            let (msg, req) = socket.recv().await.unwrap();
            socket = req.send(msg).await.unwrap();
        }
    });
    handle.await.unwrap();
}

Output:

error: future cannot be sent between threads safely
   --> src/main.rs:4:37
    |
4   |       let handle = tokio::task::spawn(async move {
    |  _____________________________________^
5   | |         let mut socket = tmq::reply(&ctx).bind("tcp://0.0.0.0:5001").unwrap();
6   | |         loop {
7   | |             let (msg, req) = socket.recv().await.unwrap();
8   | |             socket = req.send(msg).await.unwrap();
9   | |         }
10  | |     });
    | |_____^ future created by async block is not `Send`
    |
    = help: within `RequestReceiver`, the trait `Sync` is not implemented for `*mut c_void`
note: future is not `Send` as this value is used across an await
   --> /home/bemyak/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tmq-0.3.2/src/socket_types/request_reply.rs:54:79
    |
54  |         let msg = futures::future::poll_fn(|cx| self.inner.multipart_recv(cx)).await?;
    |                                            ---------------------------------- ^^^^^^ - `|cx| self.inner.multipart_recv(cx)` is later dropped here
    |                                            |                                  |
    |                                            |                                  await occurs here, with `|cx| self.inner.multipart_recv(cx)` maybe used later
    |                                            has type `[closure@RequestReceiver::recv::{closure#0}::{closure#0}]` which is not `Send`
note: required by a bound in `tokio::spawn`
   --> /home/bemyak/.cargo/registry/src/index.crates.io-6f17d22bba15001f/tokio-1.26.0/src/task/spawn.rs:163:21
    |
163 |         T: Future + Send + 'static,
    |                     ^^^^ required by this bound in `spawn`

Publish and Subscribe seem to work fine.

Any hints on how to fix this?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions