google / google/tarpc

Future returned by `Serve::serve()` is not `Send`

Open
#421 11 comments 1 reaction 0 assignees View on GitHub
Dominant language
Rust
Stars
3.7k
Forks
230
PR merge metrics
No merged PRs in 30d

Description

I'm trying to write a function that starts a server with a clonable `Serve`.

```
async fn start_tarpc_server(
addr: SocketAddr,
serve: ServeFn,
) -> std::io::Result<()>
where
ServeFn:
tarpc::server::Serve + Send + 'static + Clone,
ServeFn::Req: Send + 'static + serde::de::DeserializeOwned,
ServeFn::Resp: Send + 'static + serde::ser::Serialize,
{
tokio::spawn(serve.serve());
Ok(())
}
```

That turns out to be impossible because I cannot call `tokio::spawn()` on the future returned by `Serve::serve`. The future is not guaranteed to be `Send`. Same applies to any future that wraps a `serve()` future.

This is a well-known problem caused by [using `aysnc fn` in public traits](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#where-the-gaps-lie). There is a solution described [in the same article](https://blog.rust-lang.org/2023/12/21/async-fn-rpit-in-traits.html#why-do-i-need-trait_variantmake-and-send-bounds). Can we apply the solution here?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.