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

read_line panics if an IO error happens in the middle of a line #1928

Closed
blckngm opened this issue Dec 9, 2019 · 1 comment
Closed

read_line panics if an IO error happens in the middle of a line #1928

blckngm opened this issue Dec 9, 2019 · 1 comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-crash Problems and improvements related to program crashes/panics. M-io Module: tokio/io

Comments

@blckngm
Copy link
Contributor

blckngm commented Dec 9, 2019

Version

tokio 0.2.4

Description

AsyncBufReadExt::read_line panics at

debug_assert_eq!(*read, 0);
if the stream returns an Err in the middle of a line.

Full reproduction:

use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{self, *};

struct MyStream {
    i: u32,
}

impl MyStream {
    fn new() -> Self {
        MyStream { i: 0 }
    }
}

impl AsyncRead for MyStream {
    fn poll_read(
        mut self: Pin<&mut Self>,
        _cx: &mut Context<'_>,
        buf: &mut [u8],
    ) -> Poll<io::Result<usize>> {
        match self.i {
            0 => {
                buf[..5].copy_from_slice(b"hello");
                self.i = 1;
                Poll::Ready(Ok(5))
            }
            1 => Poll::Ready(Err(io::ErrorKind::BrokenPipe.into())),
            _ => unreachable!(),
        }
    }
}

#[tokio::main]
async fn main() -> io::Result<()> {
    let mut stream = BufReader::new(MyStream::new());
    let mut line = String::new();
    stream.read_line(&mut line).await?;
    Ok(())
}
@gparker42
Copy link

Looks like this was fixed by #2541.

@Darksonn Darksonn added A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-crash Problems and improvements related to program crashes/panics. M-io Module: tokio/io labels Jun 8, 2020
@Darksonn Darksonn closed this as completed Jun 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-tokio Area: The main tokio crate C-bug Category: This is a bug. I-crash Problems and improvements related to program crashes/panics. M-io Module: tokio/io
Projects
None yet
Development

No branches or pull requests

3 participants