From b6405e932ed765ddbbc04e8c88a70ec84470ba74 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebastian=20P=C3=BCtz?= Date: Wed, 10 Aug 2022 23:43:56 +0200 Subject: [PATCH] Change panic on seeking busy file to error. --- tokio/src/fs/file.rs | 7 ++++++- tokio/src/fs/file/tests.rs | 21 +++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/tokio/src/fs/file.rs b/tokio/src/fs/file.rs index 2c38e8059f5..b1bc9544a56 100644 --- a/tokio/src/fs/file.rs +++ b/tokio/src/fs/file.rs @@ -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(); diff --git a/tokio/src/fs/file/tests.rs b/tokio/src/fs/file/tests.rs index 18a4c078599..1c90a8d1541 100644 --- a/tokio/src/fs/file/tests.rs +++ b/tokio/src/fs/file/tests.rs @@ -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()); +}