Skip to content
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

Future cannot be sent between threads safely #8

Closed
aruddy-slb opened this issue Mar 9, 2022 · 3 comments
Closed

Future cannot be sent between threads safely #8

aruddy-slb opened this issue Mar 9, 2022 · 3 comments
Labels
bug Something isn't working

Comments

@aruddy-slb
Copy link

I have worked through the example you provided in the ReadMe, and it works great! I can read/write tags without a hitch.

The issue arises when I attempt to create a background task to handle the reading/writing of tags. Rust Analyzer gives me the following error:

future cannot be sent between threads safely
the trait `std::marker::Send` is not implemented for `dyn std::future::Future<Output = Result<rseip::client::ab_eip::TagValue<i32>, ClientError>>`rustc
[main.rs(18, 32): ]()future is not `Send` as it awaits another future which is not `Send`
[spawn.rs(127, 21): ]()required by a bound in `tokio::spawn` 

However, in the docs, it specifically says that ClientError is Send and TagValue is Send so long as T is also Send. This might just be my inexperience with tokio, but I am wondering if you could shed some light on how to do this properly.

Here is my sample code:

use rseip::client::ab_eip::*;
use rseip::precludes::*;

#[tokio::main]
async fn main(){
    tokio::spawn(async move {
        background().await;
    });
}

async fn background() -> Result<()> {
    let mut client = AbEipClient::new_host_lookup("192.168.0.83")
        .await?
        .with_connection_path(PortSegment::default());
    let tag = EPath::parse_tag("test_car1_x")?;
    println!("read tag...");
    let value: TagValue<i32> = client.read_tag(tag.clone()).await?;
    println!("tag value: {:?}", value);
    client.write_tag(tag, value).await?;
    println!("write tag - done");
    client.close().await?;
    Ok(())
}

Thank you in advance!

@Joylei Joylei added the bug Something isn't working label Mar 12, 2022
@simonkampe
Copy link

simonkampe commented Apr 10, 2022

Can this be worked around using the anyhow crate like so?

fn read_tag() -> anyhow::Result<i32> {
    let tag = EPath::parse_tag("my tag name")?;
    let value: TagValue<i32> = client
        .read_tag(tag.clone())
        .await
        .context("Failed to read tag!")?;
    Ok(value.value)
}

But I agree it would be nice if it was fixed :)

@simonkampe
Copy link

It ain't pretty, but I fixed the issue (I think? It compiles anyways) here: https://github.com/simonkampe/eip-rs

@Joylei
Copy link
Owner

Joylei commented Apr 10, 2022

let me try

Joylei added a commit that referenced this issue Apr 16, 2022
@Joylei Joylei closed this as completed Apr 16, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants