Skip to content

Commit

Permalink
Avoid wait in drop
Browse files Browse the repository at this point in the history
  • Loading branch information
lemaitre-aneo committed Jul 21, 2024
1 parent 961d8f7 commit abfb494
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 14 deletions.
16 changes: 12 additions & 4 deletions src/client/dir/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,7 @@ impl Dir {

impl Drop for Dir {
fn drop(&mut self) {
match DirClosing::new(self) {
DirClosing(DirClosingState::Closed) => (),
future => _ = futures::executor::block_on(future),
}
DirClosing::new(self).forget()
}
}

Expand Down Expand Up @@ -102,6 +99,17 @@ impl<'a> DirClosing<'a> {
DirClosing(DirClosingState::Stopping(stop))
}
}

fn forget(mut self) {
match std::mem::replace(&mut self.0, DirClosingState::Closed) {
DirClosingState::Closing { dir, handle: _, pending: _ } => {
log::trace!("Directory dropped while not closed");
SftpClientStopping::new(&mut dir.client).forget()
}
DirClosingState::Stopping(stopping) => stopping.forget(),
DirClosingState::Closed => todo!(),
}
}
}

impl Drop for DirClosing<'_> {
Expand Down
16 changes: 12 additions & 4 deletions src/client/file/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,7 @@ impl File {

impl Drop for File {
fn drop(&mut self) {
match FileClosing::new(self) {
FileClosing(FileClosingState::Closed) => (),
future => _ = futures::executor::block_on(future),
}
FileClosing::new(self).forget()
}
}

Expand Down Expand Up @@ -99,6 +96,17 @@ impl<'a> FileClosing<'a> {
FileClosing(FileClosingState::Stopping(stop))
}
}

fn forget(mut self) {
match std::mem::replace(&mut self.0, FileClosingState::Closed) {
FileClosingState::Closing { file, handle: _, pending: _ } => {
log::trace!("File dropped while not closed");
SftpClientStopping::new(&mut file.client).forget()
}
FileClosingState::Stopping(stopping) => stopping.forget(),
FileClosingState::Closed => todo!(),
}
}
}

impl Drop for FileClosing<'_> {
Expand Down
13 changes: 7 additions & 6 deletions src/client/stop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,7 @@ impl SftpClient {

impl Drop for SftpClient {
fn drop(&mut self) {
let stop = SftpClientStopping::new(self);

// Avoid nested execution
if !stop.is_stopped() {
futures::executor::block_on(stop);
}
SftpClientStopping::new(self).forget();
}
}

Expand Down Expand Up @@ -92,6 +87,12 @@ impl<'a> SftpClientStopping<'a> {
pub(super) fn is_stopped(&self) -> bool {
self.request_processor.is_none()
}

pub(super) fn forget(mut self) {
if self.request_processor.take().is_some() {
log::trace!("SftpClient dropped while not stopped");
}
}
}

impl Future for SftpClientStopping<'_> {
Expand Down

0 comments on commit abfb494

Please sign in to comment.