From 7a437dff1392cf1fe030f06d7c94f673b404dd55 Mon Sep 17 00:00:00 2001 From: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Date: Fri, 26 Jul 2024 00:01:52 +1000 Subject: [PATCH] Fix panic when flush_interval is set to 0 (#136) * Fix panic when flush_interval is set to 0 Fixed #135 Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix checking duration is zero Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Fix integration tests Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Revert previous commit Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Update src/tasks.rs Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> * Test `flush_interval` set to 0 Signed-off-by: Jiahao XU --------- Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com> Signed-off-by: Jiahao XU --- src/tasks.rs | 35 +++++++++++++++++++++-------------- tests/highlevel.rs | 3 ++- 2 files changed, 23 insertions(+), 15 deletions(-) diff --git a/src/tasks.rs b/src/tasks.rs index 1ba6a0c..80dd1d2 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -65,8 +65,13 @@ pub(super) fn create_flush_task( write_end_buffer_size: NonZeroUsize, flush_interval: Duration, ) -> Result<(), Error> { - let mut interval = time::interval(flush_interval); - interval.set_missed_tick_behavior(time::MissedTickBehavior::Delay); + let mut interval = if !flush_interval.is_zero() { + let mut interval = time::interval(flush_interval); + interval.set_missed_tick_behavior(time::MissedTickBehavior::Delay); + Some(interval) + } else { + None + }; let auxiliary = shared_data.get_auxiliary(); let flush_end_notify = &auxiliary.flush_end_notify; @@ -133,18 +138,20 @@ pub(super) fn create_flush_task( flush_end_notify.notified().await; - tokio::select! { - biased; - - _ = interval.tick() => (), - // tokio::sync::Notify is cancel safe, however - // cancelling it would lose the place in the queue. - // - // However, since flush_task is the only one who - // calls `flush_immediately.notified()`, it - // is totally fine to cancel here. - _ = auxiliary.flush_immediately.notified() => (), - }; + if let Some(interval) = interval.as_mut() { + tokio::select! { + biased; + + _ = interval.tick() => (), + // tokio::sync::Notify is cancel safe, however + // cancelling it would lose the place in the queue. + // + // However, since flush_task is the only one who + // calls `flush_immediately.notified()`, it + // is totally fine to cancel here. + _ = auxiliary.flush_immediately.notified() => (), + }; + } } } diff --git a/tests/highlevel.rs b/tests/highlevel.rs index 60b6cf9..acfaf79 100644 --- a/tests/highlevel.rs +++ b/tests/highlevel.rs @@ -58,7 +58,8 @@ async fn sftp_file_basics() { let content = b"HELLO, WORLD!\n".repeat(200); - let (mut child, sftp) = connect(Default::default()).await; + let (mut child, sftp) = + connect(SftpOptions::new().flush_interval(Duration::from_secs(0))).await; let content = &content[..min(sftp.max_write_len() as usize, content.len())];