Skip to content

Commit

Permalink
Change panic on seeking busy file to error.
Browse files Browse the repository at this point in the history
  • Loading branch information
sebpuetz committed Aug 10, 2022
1 parent 9d9488d commit b6405e9
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
7 changes: 6 additions & 1 deletion tokio/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,7 +567,12 @@ impl AsyncSeek for File {

loop {
match inner.state {
Busy(_) => panic!("must wait for poll_complete before calling start_seek"),
Busy(_) => {
return Err(io::Error::new(
io::ErrorKind::Other,
"other file operation is pending, call poll_complete before start_seek",
))
}
Idle(ref mut buf_cell) => {
let mut buf = buf_cell.take().unwrap();

Expand Down
21 changes: 21 additions & 0 deletions tokio/src/fs/file/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -955,3 +955,24 @@ fn partial_read_set_len_ok() {
assert_eq!(n, FOO.len());
assert_eq!(&buf[..n], FOO);
}

#[test]
fn busy_file_seek_error() {
let mut file = MockFile::default();
let mut seq = Sequence::new();
file.expect_inner_write()
.once()
.in_sequence(&mut seq)
.returning(|_| Err(io::ErrorKind::Other.into()));

let mut file = crate::io::BufReader::new(File::from_std(file));
{
let mut t = task::spawn(file.write(HELLO));
assert_ready_ok!(t.poll());
}

pool::run_one();

let mut t = task::spawn(file.seek(SeekFrom::Start(0)));
assert_ready_err!(t.poll());
}

0 comments on commit b6405e9

Please sign in to comment.