Skip to content

Commit ad16975

Browse files
feat: Add JoinHandle::abort() (#2877)
Co-authored-by: Lucas Nogueira <lucas@tauri.studio>
1 parent 3de67cb commit ad16975

2 files changed

Lines changed: 29 additions & 0 deletions

File tree

.changes/join-handle-abort.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"tauri": patch
3+
---
4+
5+
Added `abort` method to `tauri::async_runtime::JoinHandle`.

core/tauri/src/async_runtime.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,17 @@ static RUNTIME: OnceCell<Runtime> = OnceCell::new();
3535
#[derive(Debug)]
3636
pub struct JoinHandle<T>(TokioJoinHandle<T>);
3737

38+
impl<T> JoinHandle<T> {
39+
/// Abort the task associated with the handle.
40+
///
41+
/// Awaiting a cancelled task might complete as usual if the task was
42+
/// already completed at the time it was cancelled, but most likely it
43+
/// will fail with a cancelled `JoinError`.
44+
pub fn abort(&self) {
45+
self.0.abort();
46+
}
47+
}
48+
3849
impl<T> Future for JoinHandle<T> {
3950
type Output = crate::Result<T>;
4051
fn poll(mut self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
@@ -108,4 +119,17 @@ mod tests {
108119
let handle = handle();
109120
assert_eq!(handle.block_on(async { 0 }), 0);
110121
}
122+
123+
#[tokio::test]
124+
async fn handle_abort() {
125+
let handle = handle();
126+
let join = handle.spawn(async { 5 });
127+
join.abort();
128+
if let crate::Error::JoinError(raw_box) = join.await.unwrap_err() {
129+
let raw_error = raw_box.downcast::<tokio::task::JoinError>().unwrap();
130+
assert!(raw_error.is_cancelled());
131+
} else {
132+
panic!("Abort did not result in the expected `JoinError`");
133+
}
134+
}
111135
}

0 commit comments

Comments
 (0)