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

ReadExactError::FinishedEarly byte count is sometimes incorrect #1861

Closed
BiagioFesta opened this issue May 13, 2024 · 1 comment · Fixed by #1875
Closed

ReadExactError::FinishedEarly byte count is sometimes incorrect #1861

BiagioFesta opened this issue May 13, 2024 · 1 comment · Fixed by #1875
Labels
bug Something isn't working

Comments

@BiagioFesta
Copy link
Contributor

BiagioFesta commented May 13, 2024

Maybe I am missing something, but:

Client

The client opens a unidirectional stream, it writes 5 bytes, and after 1 second idle it FIN the stream.

let mut stream = conn.open_uni().await?;
send.write_all(b"hello").await?;
tokio::time::sleep(Duration::from_secs(1)).await;
stream.finish()?;
stream.stopped().await;

Server

The server accepts the unidirectional stream, it tries to read 1024 bytes.

let mut buffer = [0; 1024];

let mut stream = connection.accept_uni().await?;
stream.read_exact(&mut buffer).await?;

Expected behavior:

On the server side, read_exact should fail with FinishedEarly(5), as it read the FIN bit after 5 bytes (without filling the entire buffer).

ERROR server: stream finished early (5 bytes read)

Current behavior (commit: 393b617ef1c76c7a6f0dad33905466fb266580bd)

On the server side, read_exact instead fails with FinishedEarly(0). Note the 0 bytes are read.

ERROR server: stream finished early (0 bytes read)

Additional Notes

It works as expected if tokio::time::sleep is removed.

Attached, a dirty-modified version of quinn/examples/(client|server).rs that I've used to reproduce the above scenario
example_finish_early.txt

@Ralith
Copy link
Collaborator

Ralith commented May 14, 2024

Thanks for the report! This is probably a bug in the ReadExact::poll implementation that arises when the finish notification is delivered separately from other data, or perhaps whenever multiple operations are required. The math is a bit difficult to follow so it's not immediately obvious to me what the bug is, but that difficulty makes it highly suspect and indicates a need for revision regardless.

fn poll(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Self::Output> {
let this = self.get_mut();
let total = this.buf.remaining();
let mut remaining = total;
while remaining > 0 {
ready!(this.stream.poll_read_buf(cx, &mut this.buf))?;
let new = this.buf.remaining();
if new == remaining {
let read = total - remaining;
return Poll::Ready(Err(ReadExactError::FinishedEarly(read)));
}
remaining = new;
}
Poll::Ready(Ok(()))
}

@Ralith Ralith added the bug Something isn't working label May 14, 2024
@Ralith Ralith changed the title FinishedEarly with sleep ReadExactError::FinishedEarly byte count is sometimes incorrect May 20, 2024
@djc djc closed this as completed in #1875 May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants