Skip to content

Commit

Permalink
Fix Drop impl for TokioCompatFile & ReadDir
Browse files Browse the repository at this point in the history
Only spawn future if tokio runtime is available

Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
  • Loading branch information
NobodyXu committed Aug 6, 2023
1 parent d1503b9 commit c20d85f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
7 changes: 7 additions & 0 deletions src/changelog.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#[allow(unused_imports)]
use crate::*;

/// # Fixed
///
/// `Drop` implementation to make sure they never panic
/// if tokio runtime is not avilable.
///
/// - [`file::TokioCompatFile::Drop`]
/// - [`dir::ReadDir::Drop`]
#[doc(hidden)]
pub mod unreleased {}

Expand Down
21 changes: 13 additions & 8 deletions src/file/tokio_compat_file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,10 @@ use std::{
use bytes::{Buf, Bytes, BytesMut};
use derive_destructure2::destructure;
use pin_project::{pin_project, pinned_drop};
use tokio::io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, ReadBuf};
use tokio::{
io::{AsyncBufRead, AsyncRead, AsyncSeek, AsyncWrite, ReadBuf},
runtime,
};
use tokio_io_utility::ready;
use tokio_util::sync::WaitForCancellationFutureOwned;

Expand Down Expand Up @@ -760,13 +763,15 @@ impl PinnedDrop for TokioCompatFile {

let do_drop_fut = Self::do_drop(file, read_future, write_futures);

tokio::spawn(async move {
tokio::select! {
biased;
if let Ok(handle) = runtime::Handle::try_current() {
handle.spawn(async move {
tokio::select! {
biased;

_ = cancellation_fut => (),
_ = do_drop_fut => (),
}
});
_ = cancellation_fut => (),
_ = do_drop_fut => (),
}
});
}
}
}
17 changes: 10 additions & 7 deletions src/fs/dir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use std::{

use futures_core::stream::{FusedStream, Stream};
use pin_project::{pin_project, pinned_drop};
use tokio::runtime;
use tokio_util::sync::WaitForCancellationFutureOwned;

type ResponseFuture = crate::lowlevel::AwaitableNameEntriesFuture<crate::Buffer>;
Expand Down Expand Up @@ -194,13 +195,15 @@ impl PinnedDrop for ReadDir {
let cancellation_fut = dir.0.get_auxiliary().cancel_token.clone().cancelled_owned();
let do_drop_fut = Self::do_drop(dir, future);

tokio::spawn(async move {
tokio::select! {
biased;
if let Ok(handle) = runtime::Handle::try_current() {
handle.spawn(async move {
tokio::select! {
biased;

_ = cancellation_fut => (),
_ = do_drop_fut => (),
}
});
_ = cancellation_fut => (),
_ = do_drop_fut => (),
}
});
}
}
}

0 comments on commit c20d85f

Please sign in to comment.