Skip to content

Support for passing async fn into advertise_service #108

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
entire opened this issue Aug 23, 2023 · 13 comments · Fixed by #228
Closed

Support for passing async fn into advertise_service #108

entire opened this issue Aug 23, 2023 · 13 comments · Fixed by #228
Assignees
Labels
enhancement New feature or request

Comments

@entire
Copy link
Contributor

entire commented Aug 23, 2023

In the service_server.rs example, I've noticed that there is:

    let _handle = client
        .advertise_service::<std_srvs::SetBool>("/my_set_bool", my_service)
        .await?;

and my_service is a simple fn here, but many services are often going to require async/await type functions to run within the function. Is there plan to support async fn here?

If not, I'm happy to look into creating a PR to add support for this especially because I'd like to use async functions within my service callback that I pass in.

@ssnover
Copy link
Collaborator

ssnover commented Aug 23, 2023

If you're blocked on this you still have the option of calling tokio::spawn here.

@Carter12s
Copy link
Collaborator

I think it is definitely something we'd like to support if possible. It shouldn't be too hard to try experimenting with our internal type erasure to see if this works, but I'm suspicious we're going to hit some limitation of async fn in rust that might require a more significant overhaul. If you'd like to dig on on PR trying to implement it @entire that would be awesome. If not, I'll probably take a look at it in the next week or so.

@Carter12s Carter12s added the enhancement New feature or request label Aug 23, 2023
@entire
Copy link
Contributor Author

entire commented Aug 24, 2023

@ssnover agreed - i think it would just be nicer overall though for the functions to be async in general though.

@Carter12s - thanks, makes sense. let me take a look at it and I'll take a stab at it if it seems relatively not difficult.

@ssnover
Copy link
Collaborator

ssnover commented Sep 7, 2023

Having glanced at this for a bit, the main challenge is our paradigm around type erasure with the closure. Async closures are unstable, which means the message: &str parameter will have to find another way in.

If you look at implementing this: I'd recommend tokio::spawn with a channel. Wrap the tokio::JoinHandle in an abort_on_drop::ChildTask and add that to the struct that gets store in the services map along with the sender side of the channel for messages.

@Carter12s
Copy link
Collaborator

Update!

Async closures have merged! rust-lang/rust#132706

We should be able to build this out once the next version of rust ships (could build it out on nightly right now).

Once built we can put it behind a feature flag to not affect out min rust version too much. But super excited this is coming.

@Carter12s Carter12s self-assigned this Feb 21, 2025
@Carter12s
Copy link
Collaborator

Rust 1.85 is officially out with async closures are fully supported!!!

We're ready to start work on this one.

@ssnover
Copy link
Collaborator

ssnover commented Feb 21, 2025

Do we have an established MSRV target? It's pretty common for the ecosystem to support current stable and the two previous versions.

@Carter12s
Copy link
Collaborator

README currently calls out 1.75, but I don't think we've got that in the Cargo.tomls yet.

My thought here is that we build this feature out under a feature flag ["async-closures"], and when ~1.87 drops we enable that feature as default.

I'm open to opinions on the API, but it feels like it is worth keeping the non-async versions around? Have both advertise_service and advertise_async_service? That makes the change non-breaking, additive, and pretty clean behind a feature flag.

@Carter12s
Copy link
Collaborator

Okay I took a stab at building this out (#227), but did NOT have a lot of luck. Ran into the following issues:

  1. AsyncFn is not dyn compatible, so storing a list of AsyncFns in some kind of data structure ends up being a massive pain in the ass. Instead you have to do a bunch of shenanigans to hide the async closures in a manually created closure that returns a future. This made writing the feature very challenging, but was surmountable.
  2. An async closure which captures ANY context by move is not Fn and is instead FnOnce. This is because multiple futures can be spawned from the async closure in parallel and they can't all have ownership over the moved context at the same time. These leads to fairly horrible ergonomics for users of the feature.

Where you'd like to write:

         let service = async move |request: std_srvs::SetBoolRequest| -> std::result::Result<
             std_srvs::SetBoolResponse,
             Box<dyn std::error::Error + Send + Sync>,
         > {
             tx.send(request.data).await.unwrap();
             Ok(std_srvs::SetBoolResponse {
                 success: true,
                 message: "You set my bool!".to_string(),
             })
         };

You instead have to write:

        let service = move |request: std_srvs::SetBoolRequest| {
            let tx = tx.clone();
            Box::pin(async move {
                tx.send(request.data).await.unwrap();
                Ok(std_srvs::SetBoolResponse {
                    success: true,
                    message: "You set my bool!".to_string(),
                })
            })
        };

I may have totally screwed something up in the implementation, so would love other eyes on my prototype. But, based on how this initial try wen't I'm not sure I want to move forward with this.

@ssnover
Copy link
Collaborator

ssnover commented Mar 31, 2025 via email

@Carter12s
Copy link
Collaborator

^Yeah I think an API that provides a runtime handle, or frankly an API which sets up a pair of channels and makes it easy for the user to spawn a tokio::task() to be the service are both more likely to end up ergonomic than actually taking in an async closure...

Would be worth doing a bit of an API study and seeing what other systems have come up with.

@Carter12s
Copy link
Collaborator

Okay I took a second swing at this based and ultimately decided that having a single advertise_service function was the best API.

Inside the user's service callback they can use tokio::Handle::current().block_on to do whatever async stuff they need, and this gives much more clear and understandable compiler errors to the user. Everything with AsyncFn just led to very arcane and non-obvious error messages.

To support this change all service functions need to be executing inside a tokio::task::spawn_blocking which was probably the better design call from the get-go.

I'd like to continue to improve documentation for this, but I think having an example and some bread-crumbs is good enough for now.

@Carter12s
Copy link
Collaborator

Roslibrust v0.14.0 published with examples on how to do this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
3 participants