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

[async closure] implementation of Send is not general enough using tokio channel #139475

Open
Banyc opened this issue Apr 7, 2025 · 1 comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.

Comments

@Banyc
Copy link

Banyc commented Apr 7, 2025

I tried this code:

use tokio::sync::mpsc;

pub async fn main() {
    tokio::spawn(async move { run().await });
}

pub async fn run() {
    let (tx, _rx) = mpsc::channel(12);
    let f = async || {
        tx.send(()).await;
    };
    f().await; // ok
    run_async_closure(f).await; // error
}

pub async fn run_async_closure(mut f: impl AsyncFnMut()) {
    f().await;
}

I expected to see this happen: no error as this is a very normal use case of async closure.

Instead, this happened: compiler complaining about the lifetime of the tokio mpsc::Sender.

Meta

rustc --version --verbose:

rustc 1.86.0 (05f9846f8 2025-03-31)
binary: rustc
commit-hash: 05f9846f893b09a1be1fc8560e33fc3c815cfecb
commit-date: 2025-03-31
host: aarch64-apple-darwin
release: 1.86.0
LLVM version: 19.1.7
Backtrace

error: implementation of `Send` is not general enough
 --> src/lib.rs:4:5
  |
4 |     tokio::spawn(async move { run().await });
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ implementation of `Send` is not general enough
  |
  = note: `Send` would have to be implemented for the type `&'0 tokio::sync::mpsc::Sender<()>`, for any lifetime `'0`...
  = note: ...but `Send` is actually implemented for the type `&'1 tokio::sync::mpsc::Sender<()>`, for some specific lifetime `'1`

error: could not compile `test_async_closure` (lib) due to 1 previous error

@Banyc Banyc added the C-bug Category: This is a bug. label Apr 7, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Apr 7, 2025
@Banyc
Copy link
Author

Banyc commented Apr 7, 2025

possible duplicate: #138207

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged.
Projects
None yet
Development

No branches or pull requests

2 participants