I have the following base-structure to an async tokio app, where multiple jobs/services are running asynchronously:
use tokio::{runtime::Runtime, signal};
use futures::future;
use tokio_schedule::{every, Job};
fn main() {
let runtime = Runtime::new().unwrap();
let handle = runtime.handle().clone();
handle.spawn(async move {
let _ = signal::ctrl_c().await;
runtime.shutdown_background();
});
let job1 = every(3).seconds().perform(|| async {
// do some work
println!("3s scheduled execution");
});
let job2 = every(2).seconds().perform(|| async {
// do some work
println!("2s scheduled execution");
});
handle.block_on(future::join_all([job1, job2]));
}
When I shut this application down, I get a panic A Tokio 1.x context was found, but it is being shutdown. from one of the scheduled tasks. This is a recent refactoring from using task::spawn where cleanup of the tasks was not performed correctly.
If the Handle’s associated Runtime has been shut down (through Runtime::shutdown_background, Runtime::shutdown_timeout, or by dropping it) and Handle::block_on is used it might return an error or panic. Specifically IO resources will return an error and timers will panic. Runtime independent futures will run as normal.