Write to a TcpStream while waiting for it to become writable from another task/thread #5453
-
I have a program that tries to "pipe" two streams together (a I spawn two tasks to do this. One to forward TcpStream's data to libcurl and another to forward data the other way. But my program ends up in a deadlock. In my first task, I wait for my stream.read().await.readable().await.unwrap(); In my second task, I write to the let mut stream = stream.write().await;
println!("Locked!");
stream.write_all(&buffer[..num_bytes]).await.unwrap(); But Are there any "clean" solutions available? I don't consider using |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
Wrapping an IO resource in a mutex or rwlock is almost wrong. You are deadlocking because the reader and the writer are blocking each other. You should split your IO resource instead. For the specific case of |
Beta Was this translation helpful? Give feedback.
Wrapping an IO resource in a mutex or rwlock is almost wrong. You are deadlocking because the reader and the writer are blocking each other. You should split your IO resource instead. For the specific case of
TcpStream
, you can use theTcpStream::into_split
method to get a read and write half that can be used independently.