You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Hi, I'm building a proxy. The server side accepts a TlsStream (from the client side) and makes a Tcp connection to the target website. Then we just need to copy everything we get from the TlsStream to the TcpStream and collect the response vice versa.
I was initially using tokio::io::copy for this task. However, I encountered a weird bug that my client never received nearly half of the response from the target website, and the connection just stuck until timeout. I then replace the io::copy with my simplified version:
// (didn't work, same as tokio::io::copy)pubasyncfncopy_tcp<R:AsyncRead + Unpin,W:AsyncWrite + Unpin>(r:&mutR,w:&mutW,) -> Result<()>{letmut buf = [0;2048];loop{let len = r.read(&mut buf).await?;if len == 0{returnOk(());}
w.write(&buf[..len]).await?;}}
And this didn't work until I added a flush after the write call,
pubasyncfncopy_tcp<R:AsyncRead + Unpin,W:AsyncWrite + Unpin>(r:&mutR,w:&mutW,) -> Result<()>{letmut buf = [0;2048];loop{let len = r.read(&mut buf).await?;if len == 0{returnOk(());}
w.write(&buf[..len]).await?;
--> w.flush().await?;}}
Hi, I'm building a proxy. The server side accepts a
TlsStream
(from the client side) and makes a Tcp connection to the target website. Then we just need to copy everything we get from theTlsStream
to theTcpStream
and collect the response vice versa.I was initially using
tokio::io::copy
for this task. However, I encountered a weird bug that my client never received nearly half of the response from the target website, and the connection just stuck until timeout. I then replace theio::copy
with my simplified version:And this didn't work until I added a
flush
after thewrite
call,I found that in tokio-tls/rustls,
tls/tokio-rustls/src/server.rs
Lines 95 to 96 in db01bce
that we are required to manually call
flush
.While in tokio::io::copy,
https://github.com/tokio-rs/tokio/blob/c306bf853a1f8423b154f17fa47926f04eecd9b4/tokio/src/io/util/copy.rs#L69-L74
only when EOF is seen does it call
poll_flush
.So, should we avoid using
tokio::io::copy
for this purpose?The text was updated successfully, but these errors were encountered: