Skip to content

Commit

Permalink
Fix panic when flush_interval is set to 0 (#136)
Browse files Browse the repository at this point in the history
* 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 <Jiahao_XU@outlook.com>

---------

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu authored Jul 25, 2024
1 parent 2684973 commit 7a437df
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 15 deletions.
35 changes: 21 additions & 14 deletions src/tasks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,13 @@ pub(super) fn create_flush_task<W: AsyncWrite + Send + 'static>(
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;
Expand Down Expand Up @@ -133,18 +138,20 @@ pub(super) fn create_flush_task<W: AsyncWrite + Send + 'static>(

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() => (),
};
}
}
}

Expand Down
3 changes: 2 additions & 1 deletion tests/highlevel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())];

Expand Down

0 comments on commit 7a437df

Please sign in to comment.