Skip to content

Instantly share code, notes, and snippets.

@PhotonQuantum
Created August 5, 2021 13:39
Show Gist options
  • Save PhotonQuantum/ffbae4ec04d413f0d498a5f89678dc1b to your computer and use it in GitHub Desktop.
Save PhotonQuantum/ffbae4ec04d413f0d498a5f89678dc1b to your computer and use it in GitHub Desktop.
use std::time::Duration;
use actix::{Actor, Arbiter, AsyncContext, Context, System};
struct MyActor;
impl Actor for MyActor {
type Context = Context<Self>;
fn started(&mut self, ctx: &mut Self::Context) {
ctx.run_later(Duration::from_secs(1), |_, _| {
panic!("boom");
});
}
}
impl Drop for MyActor {
fn drop(&mut self) {
if std::thread::panicking() {
if let Some(sys) = System::try_current() {
println!("stopping actor");
sys.stop_with_code(1) // comment out this line to compare the output
};
}
}
}
fn main() {
let sys = System::new();
for _ in 0..3 {
let arb = Arbiter::new();
arb.spawn_fn(|| {
MyActor.start();
});
}
Arbiter::new().spawn(async {
actix::clock::sleep(Duration::from_secs(2)).await;
println!("If printed, the panicked actors failed to kill the system.");
});
sys.run().expect("unable to run system");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment