Confusing Send bound error

I am trying to make an asynchronous TCP server with tokio and get this weird error while trying to spawn a thread

tokio::spawn(accept_conn(client, bc_sender.clone(), bc_sender.subscribe())
^^^^^^^^^^^^ `(dyn std::error::Error + 'static)` cannot be sent between threads safely

I do not see any Error trait object here (the accept_conn function is async and returns ()) and do not understand why am I sending it
Here is a code snipped which triggers the error:

while let Ok((client, addr)) = listener.accept().await {
    info!("New connection, address {}", addr.ip());
    tokio::spawn(accept_conn(client, bc_sender.clone(),

And an accept_conn function declaration

async fn accept_conn(client: TcpStream,
                     cast_sender: broadcast::Sender<PixelChangedEvent>,
                     cast_receiver: broadcast::Receiver<PixelChangedEvent>) { ... }

If you print the full error with cargo build rather than looking at the IDE popup, it should point out where the dyn Error comes from. It can be any function used anywhere inside accept_conn.

When you find it, replace the Box<dyn Error> with Box<dyn Error + Send + Sync> or a crate like anyhow.

Thank you! I finally managed to solve it by changing functions' which are called in accept_conn return types to Result<_, Box<dyn Error + Send + Sync>>

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.