Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(services/monoiofs): handle async cancel during file open #5094

Merged
merged 1 commit into from
Sep 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions core/src/services/monoiofs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,15 @@ impl MonoiofsCore {
pub async fn spawn<F, Fut, T>(&self, f: F)
where
F: FnOnce() -> Fut + 'static + Send,
Fut: Future<Output = T>,
Fut: Future<Output = T> + 'static,
T: 'static,
{
let result = self
.tx
.send_async(Box::new(move || {
// task will be spawned on current thread, task panic
// will cause current worker thread panic
monoio::spawn(async move { f().await });
monoio::spawn(f());
}))
.await;
self.unwrap(result);
Expand Down
17 changes: 9 additions & 8 deletions core/src/services/monoiofs/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,16 @@ impl MonoiofsReader {
// worker thread
let file = match result {
Ok(file) => {
open_result_tx
.send(Ok(()))
.expect("send result from worker thread should success");
let Ok(()) = open_result_tx.send(Ok(())) else {
// MonoiofsReader::new is cancelled, exit worker task
return;
};
file
}
Err(e) => {
open_result_tx
.send(Err(new_std_io_error(e)))
.expect("send result from worker thread should success");
// discard the result if send failed due to MonoiofsReader::new
// cancelled since we are going to exit anyway
let _ = open_result_tx.send(Err(new_std_io_error(e)));
return;
}
};
Expand All @@ -89,8 +90,8 @@ impl MonoiofsReader {
match req {
ReaderRequest::Read { pos, buf, tx } => {
let (result, buf) = file.read_at(buf, pos).await;
// buf.len() will be set to n by monoio if read successfully,
// so n is dropped
// buf.len() will be set to n by monoio if read
// successfully, so n is dropped
let result = result.map(move |_| buf).map_err(new_std_io_error);
// discard the result if send failed due to
// MonoiofsReader::read cancelled
Expand Down
13 changes: 7 additions & 6 deletions core/src/services/monoiofs/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,16 @@ impl MonoiofsWriter {
// worker thread
let file = match result {
Ok(file) => {
open_result_tx
.send(Ok(()))
.expect("send result from worker thread should success");
let Ok(()) = open_result_tx.send(Ok(())) else {
// MonoiofsWriter::new is cancelled, exit worker task
return;
};
file
}
Err(e) => {
open_result_tx
.send(Err(new_std_io_error(e)))
.expect("send result from worker thread should success");
// discard the result if send failed due to MonoiofsWriter::new
// cancelled since we are going to exit anyway
let _ = open_result_tx.send(Err(new_std_io_error(e)));
return;
}
};
Expand Down
Loading