Skip to content

Commit

Permalink
Log actor panics in StopSupervisor
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasdezeeuw committed Apr 14, 2024
1 parent dee2619 commit 9b66b9e
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 13 deletions.
13 changes: 1 addition & 12 deletions src/future.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use log::error;

use crate::actor::{self, Actor, NewActor};
use crate::actor_ref::ActorRef;
use crate::panic_message;
use crate::supervisor::{Supervisor, SupervisorStrategy};

/// A [`Future`] that represent an [`Actor`].
Expand Down Expand Up @@ -197,18 +198,6 @@ where
}
}

/// Attempts to extract a message from a panic, defaulting to `<unknown>`.
/// NOTE: be sure to derefence the `Box`!
fn panic_message<'a>(panic: &'a (dyn Any + Send + 'static)) -> &'a str {
match panic.downcast_ref::<&'static str>() {
Some(s) => s,
None => match panic.downcast_ref::<String>() {
Some(s) => s,
None => "<unknown>",
},
}
}

/// Called when we can't create a new receiver for the sync actor.
#[cold]
fn inbox_failure<T>(_: ReceiverConnected) -> T {
Expand Down
12 changes: 12 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,3 +96,15 @@ pub use future::{ActorFuture, ActorFutureBuilder};
pub use supervisor::{Supervisor, SupervisorStrategy, SyncSupervisor};
#[doc(no_inline)]
pub use sync::{SyncActor, SyncActorRunner, SyncActorRunnerBuilder};

/// Attempts to extract a message from a panic, defaulting to `<unknown>`.
/// NOTE: be sure to derefence the `Box`!
fn panic_message<'a>(panic: &'a (dyn std::any::Any + Send + 'static)) -> &'a str {
match panic.downcast_ref::<&'static str>() {
Some(s) => s,
None => match panic.downcast_ref::<String>() {
Some(s) => s,
None => "<unknown>",
},
}
}
20 changes: 19 additions & 1 deletion src/supervisor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ use std::fmt;

use log::warn;

use crate::{Actor, NewActor, SyncActor};
use crate::{panic_message, Actor, NewActor, SyncActor};

/// The supervisor of an [actor].
///
Expand Down Expand Up @@ -387,6 +387,15 @@ where
self.0
);
}

fn decide_on_panic(
&mut self,
panic: Box<dyn Any + Send + 'static>,
) -> SupervisorStrategy<NA::Argument> {
let msg = panic_message(&*panic);
warn!("{} panicked, stopping it: {msg}", self.0);
SupervisorStrategy::Stop
}
}

impl<A> SyncSupervisor<A> for StopSupervisor
Expand All @@ -398,6 +407,15 @@ where
warn!("{} failed, stopping it: {err}", self.0);
SupervisorStrategy::Stop
}

fn decide_on_panic(
&mut self,
panic: Box<dyn Any + Send + 'static>,
) -> SupervisorStrategy<A::Argument> {
let msg = panic_message(&*panic);
warn!("{} panicked, stopping it: {msg}", self.0);
SupervisorStrategy::Stop
}
}

/// Macro to create a supervisor that logs the error and restarts the actor.
Expand Down

0 comments on commit 9b66b9e

Please sign in to comment.