diff --git a/actix-rt/src/lib.rs b/actix-rt/src/lib.rs index 23aa6509..c45251b6 100644 --- a/actix-rt/src/lib.rs +++ b/actix-rt/src/lib.rs @@ -155,10 +155,36 @@ pub mod task { pub use tokio::task::{spawn_blocking, yield_now, JoinError, JoinHandle}; } -/// Spawns a future on the current thread. +/// Spawns a future on the current thread as a new task. +/// +/// If not immediately awaited, the task can be cancelled using [`JoinHandle::abort`]. +/// +/// The provided future is spawned as a new task; therefore, panics are caught. /// /// # Panics /// Panics if Actix system is not running. +/// +/// # Examples +/// ``` +/// # use std::time::Duration; +/// # actix_rt::Runtime::new().unwrap().block_on(async { +/// // task resolves successfully +/// assert_eq!(actix_rt::spawn(async { 1 }).await.unwrap(), 1); +/// +/// // task panics +/// assert!(actix_rt::spawn(async { +/// panic!("panic is caught at task boundary"); +/// }) +/// .await +/// .unwrap_err() +/// .is_panic()); +/// +/// // task is cancelled before completion +/// let handle = actix_rt::spawn(actix_rt::time::sleep(Duration::from_secs(100))); +/// handle.abort(); +/// assert!(handle.await.unwrap_err().is_cancelled()); +/// # }); +/// ``` #[inline] pub fn spawn(f: Fut) -> JoinHandle where diff --git a/actix-rt/tests/tests.rs b/actix-rt/tests/tests.rs index ff91769c..b82a4380 100644 --- a/actix-rt/tests/tests.rs +++ b/actix-rt/tests/tests.rs @@ -1,4 +1,5 @@ use std::{ + future::Future, sync::{ atomic::{AtomicBool, Ordering}, mpsc::channel, @@ -9,7 +10,6 @@ use std::{ }; use actix_rt::{Arbiter, System}; -use futures_core::Future; use tokio::sync::oneshot; #[test]