2021-07-08 22:04:11 +00:00
|
|
|
use crate::actor::*;
|
|
|
|
use crate::error::Error;
|
|
|
|
|
|
|
|
use preserves::value::Map;
|
|
|
|
|
2021-07-15 07:13:31 +00:00
|
|
|
use std::sync::Arc;
|
2021-07-22 14:53:56 +00:00
|
|
|
use std::marker::PhantomData;
|
2021-07-15 07:13:31 +00:00
|
|
|
|
2021-07-08 22:04:11 +00:00
|
|
|
pub struct During<T>(Map<Handle, DuringRetractionHandler<T>>);
|
2021-07-26 08:53:56 +00:00
|
|
|
pub type DuringRetractionHandler<T> = Box<dyn Send + FnOnce(&mut T, &mut Activation) -> ActorResult>;
|
2021-07-22 14:53:56 +00:00
|
|
|
pub type DuringResult<E> = Result<Option<DuringRetractionHandler<E>>, Error>;
|
2021-07-08 22:04:11 +00:00
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
pub struct DuringEntity<M, E, Fa, Fm, Fs, Fx>
|
2021-07-08 22:04:11 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
M: 'static + Send,
|
|
|
|
E: 'static + Send,
|
|
|
|
Fa: 'static + Send + FnMut(&mut E, &mut Activation, M) -> DuringResult<E>,
|
|
|
|
Fm: 'static + Send + FnMut(&mut E, &mut Activation, M) -> ActorResult,
|
2021-08-30 12:17:40 +00:00
|
|
|
Fs: 'static + Send + FnMut(&mut E, &mut Activation) -> ActorResult,
|
2023-01-30 13:25:58 +00:00
|
|
|
Fx: 'static + Send + FnMut(&mut E, &mut Activation, &Arc<ExitStatus>),
|
2021-07-08 22:04:11 +00:00
|
|
|
{
|
|
|
|
state: E,
|
2021-07-15 07:13:31 +00:00
|
|
|
assertion_handler: Option<Fa>,
|
|
|
|
message_handler: Option<Fm>,
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler: Option<Fs>,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler: Option<Fx>,
|
2021-07-08 22:04:11 +00:00
|
|
|
during: During<E>,
|
2021-07-22 14:53:56 +00:00
|
|
|
phantom: PhantomData<M>,
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> During<T> {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
During(Map::new())
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:56 +00:00
|
|
|
pub fn await_retraction<F: 'static + Send + FnOnce(&mut T, &mut Activation) -> ActorResult>(
|
2021-07-08 22:04:11 +00:00
|
|
|
&mut self,
|
|
|
|
h: Handle,
|
|
|
|
f: F,
|
|
|
|
) -> ActorResult {
|
|
|
|
self.0.insert(h, Box::new(f));
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn retract(&mut self, h: Handle) -> DuringRetractionHandler<T> {
|
|
|
|
self.0.remove(&h).unwrap_or_else(|| Box::new(|_, _| Ok(())))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 08:53:56 +00:00
|
|
|
pub fn entity<M: 'static + Send, E>(
|
2021-07-15 07:13:31 +00:00
|
|
|
state: E
|
2021-07-22 14:53:56 +00:00
|
|
|
) -> DuringEntity<M,
|
|
|
|
E,
|
|
|
|
fn (&mut E, &mut Activation, M) -> DuringResult<E>,
|
2021-08-28 16:50:55 +00:00
|
|
|
fn (&mut E, &mut Activation, M) -> ActorResult,
|
2021-08-30 12:17:40 +00:00
|
|
|
fn (&mut E, &mut Activation) -> ActorResult,
|
2023-01-30 13:25:58 +00:00
|
|
|
fn (&mut E, &mut Activation, &Arc<ExitStatus>)>
|
2021-07-15 07:13:31 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
E: 'static + Send,
|
2021-07-15 07:13:31 +00:00
|
|
|
{
|
2021-08-30 12:17:40 +00:00
|
|
|
DuringEntity::new(state, None, None, None, None)
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
impl<M, E, Fa, Fm, Fs, Fx> DuringEntity<M, E, Fa, Fm, Fs, Fx>
|
2021-07-08 22:04:11 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
M: 'static + Send,
|
|
|
|
E: 'static + Send,
|
|
|
|
Fa: 'static + Send + FnMut(&mut E, &mut Activation, M) -> DuringResult<E>,
|
|
|
|
Fm: 'static + Send + FnMut(&mut E, &mut Activation, M) -> ActorResult,
|
2021-08-30 12:17:40 +00:00
|
|
|
Fs: 'static + Send + FnMut(&mut E, &mut Activation) -> ActorResult,
|
2023-01-30 13:25:58 +00:00
|
|
|
Fx: 'static + Send + FnMut(&mut E, &mut Activation, &Arc<ExitStatus>),
|
2021-07-08 22:04:11 +00:00
|
|
|
{
|
2021-08-28 16:50:55 +00:00
|
|
|
pub fn new(
|
|
|
|
state: E,
|
|
|
|
assertion_handler: Option<Fa>,
|
|
|
|
message_handler: Option<Fm>,
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler: Option<Fs>,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler: Option<Fx>,
|
|
|
|
) -> Self {
|
2021-07-08 22:04:11 +00:00
|
|
|
DuringEntity {
|
|
|
|
state,
|
2021-07-15 07:13:31 +00:00
|
|
|
assertion_handler,
|
|
|
|
message_handler,
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler,
|
2021-07-08 22:04:11 +00:00
|
|
|
during: During::new(),
|
2021-07-22 14:53:56 +00:00
|
|
|
phantom: PhantomData,
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-15 07:13:31 +00:00
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
pub fn on_asserted<Fa1>(self, assertion_handler: Fa1) -> DuringEntity<M, E, Fa1, Fm, Fs, Fx>
|
2021-07-15 07:13:31 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
Fa1: 'static + Send + FnMut(&mut E, &mut Activation, M) -> DuringResult<E>,
|
2021-07-15 07:13:31 +00:00
|
|
|
{
|
|
|
|
DuringEntity {
|
|
|
|
state: self.state,
|
|
|
|
assertion_handler: Some(assertion_handler),
|
|
|
|
message_handler: self.message_handler,
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler: self.stop_handler,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler: self.exit_handler,
|
2021-07-15 07:13:31 +00:00
|
|
|
during: self.during,
|
2021-08-28 16:50:55 +00:00
|
|
|
phantom: self.phantom,
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-28 12:39:00 +00:00
|
|
|
pub fn on_asserted_facet<Fa1>(
|
|
|
|
self,
|
|
|
|
mut assertion_handler: Fa1,
|
2021-08-30 12:17:40 +00:00
|
|
|
) -> DuringEntity<M, E, Box<dyn 'static + Send + FnMut(&mut E, &mut Activation, M) -> DuringResult<E>>, Fm, Fs, Fx>
|
2021-08-28 12:39:00 +00:00
|
|
|
where
|
|
|
|
Fa1: 'static + Send + FnMut(&mut E, &mut Activation, M) -> ActorResult
|
|
|
|
{
|
|
|
|
self.on_asserted(Box::new(move |state, t, a| {
|
2021-08-31 15:01:43 +00:00
|
|
|
let facet_id = t.facet(|t| {
|
|
|
|
// Prevent inertness check because we have a bounded lifetime anyway. This
|
|
|
|
// allows e.g. facets containing Supervisors to Just Work (they go momentarily
|
|
|
|
// inert when their supervisee exits).
|
|
|
|
let _ = t.prevent_inert_check();
|
|
|
|
assertion_handler(state, t, a)
|
|
|
|
})?;
|
2021-10-07 14:59:34 +00:00
|
|
|
Ok(Some(Box::new(move |_state, t| Ok(t.stop_facet(facet_id)))))
|
2021-08-28 12:39:00 +00:00
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
pub fn on_message<Fm1>(self, message_handler: Fm1) -> DuringEntity<M, E, Fa, Fm1, Fs, Fx>
|
2021-07-15 07:13:31 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
Fm1: 'static + Send + FnMut(&mut E, &mut Activation, M) -> ActorResult,
|
2021-07-15 07:13:31 +00:00
|
|
|
{
|
|
|
|
DuringEntity {
|
|
|
|
state: self.state,
|
|
|
|
assertion_handler: self.assertion_handler,
|
|
|
|
message_handler: Some(message_handler),
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler: self.stop_handler,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler: self.exit_handler,
|
2021-07-15 07:13:31 +00:00
|
|
|
during: self.during,
|
2021-08-28 16:50:55 +00:00
|
|
|
phantom: self.phantom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
pub fn on_stop<Fs1>(self, stop_handler: Fs1) -> DuringEntity<M, E, Fa, Fm, Fs1, Fx>
|
|
|
|
where
|
|
|
|
Fs1: 'static + Send + FnMut(&mut E, &mut Activation) -> ActorResult,
|
|
|
|
{
|
|
|
|
DuringEntity {
|
|
|
|
state: self.state,
|
|
|
|
assertion_handler: self.assertion_handler,
|
|
|
|
message_handler: self.message_handler,
|
|
|
|
stop_handler: Some(stop_handler),
|
|
|
|
exit_handler: self.exit_handler,
|
|
|
|
during: self.during,
|
|
|
|
phantom: self.phantom,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn on_exit<Fx1>(self, exit_handler: Fx1) -> DuringEntity<M, E, Fa, Fm, Fs, Fx1>
|
2021-08-28 16:50:55 +00:00
|
|
|
where
|
2023-01-30 13:25:58 +00:00
|
|
|
Fx1: 'static + Send + FnMut(&mut E, &mut Activation, &Arc<ExitStatus>),
|
2021-08-28 16:50:55 +00:00
|
|
|
{
|
|
|
|
DuringEntity {
|
|
|
|
state: self.state,
|
|
|
|
assertion_handler: self.assertion_handler,
|
|
|
|
message_handler: self.message_handler,
|
2021-08-30 12:17:40 +00:00
|
|
|
stop_handler: self.stop_handler,
|
2021-08-28 16:50:55 +00:00
|
|
|
exit_handler: Some(exit_handler),
|
|
|
|
during: self.during,
|
|
|
|
phantom: self.phantom,
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-27 13:31:18 +00:00
|
|
|
pub fn create(self, t: &mut Activation) -> Arc<Ref<M>> {
|
2021-08-30 12:17:40 +00:00
|
|
|
let should_register_stop_action = self.stop_handler.is_some();
|
2021-08-28 16:50:55 +00:00
|
|
|
let should_register_exit_hook = self.exit_handler.is_some();
|
|
|
|
let r = t.create(self);
|
2021-08-30 12:17:40 +00:00
|
|
|
if should_register_stop_action {
|
2021-09-24 14:14:55 +00:00
|
|
|
t.on_stop_notify(&r);
|
2021-08-30 12:17:40 +00:00
|
|
|
}
|
2021-08-28 16:50:55 +00:00
|
|
|
if should_register_exit_hook {
|
|
|
|
t.state.add_exit_hook(&r);
|
|
|
|
}
|
|
|
|
r
|
2021-07-15 07:13:31 +00:00
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
impl<E, Fa, Fm, Fs, Fx> DuringEntity<AnyValue, E, Fa, Fm, Fs, Fx>
|
2021-07-08 22:04:11 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
E: 'static + Send,
|
2021-08-11 21:16:01 +00:00
|
|
|
Fa: 'static + Send + FnMut(&mut E, &mut Activation, AnyValue) -> DuringResult<E>,
|
|
|
|
Fm: 'static + Send + FnMut(&mut E, &mut Activation, AnyValue) -> ActorResult,
|
2021-08-30 12:17:40 +00:00
|
|
|
Fs: 'static + Send + FnMut(&mut E, &mut Activation) -> ActorResult,
|
2023-01-30 13:25:58 +00:00
|
|
|
Fx: 'static + Send + FnMut(&mut E, &mut Activation, &Arc<ExitStatus>),
|
2021-07-08 22:04:11 +00:00
|
|
|
{
|
2021-08-27 13:31:18 +00:00
|
|
|
pub fn create_cap(self, t: &mut Activation) -> Arc<Cap>
|
2021-07-22 14:53:56 +00:00
|
|
|
{
|
2021-08-27 13:31:18 +00:00
|
|
|
Cap::new(&self.create(t))
|
2021-07-21 23:05:08 +00:00
|
|
|
}
|
2021-07-22 14:53:56 +00:00
|
|
|
}
|
2021-07-21 23:05:08 +00:00
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
impl<M, E, Fa, Fm, Fs, Fx> Entity<M> for DuringEntity<M, E, Fa, Fm, Fs, Fx>
|
2021-07-22 14:53:56 +00:00
|
|
|
where
|
2021-07-26 08:53:56 +00:00
|
|
|
M: Send,
|
|
|
|
E: 'static + Send,
|
|
|
|
Fa: 'static + Send + FnMut(&mut E, &mut Activation, M) -> DuringResult<E>,
|
|
|
|
Fm: 'static + Send + FnMut(&mut E, &mut Activation, M) -> ActorResult,
|
2021-08-30 12:17:40 +00:00
|
|
|
Fs: 'static + Send + FnMut(&mut E, &mut Activation) -> ActorResult,
|
2023-01-30 13:25:58 +00:00
|
|
|
Fx: 'static + Send + FnMut(&mut E, &mut Activation, &Arc<ExitStatus>),
|
2021-07-22 14:53:56 +00:00
|
|
|
{
|
|
|
|
fn assert(&mut self, t: &mut Activation, a: M, h: Handle) -> ActorResult {
|
2021-07-15 07:13:31 +00:00
|
|
|
match &mut self.assertion_handler {
|
|
|
|
Some(handler) => match handler(&mut self.state, t, a)? {
|
|
|
|
Some(f) => self.during.await_retraction(h, f),
|
|
|
|
None => Ok(())
|
|
|
|
}
|
|
|
|
None => Ok(()),
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn retract(&mut self, t: &mut Activation, h: Handle) -> ActorResult {
|
|
|
|
self.during.retract(h)(&mut self.state, t)
|
|
|
|
}
|
2021-07-15 07:13:31 +00:00
|
|
|
|
2021-07-22 14:53:56 +00:00
|
|
|
fn message(&mut self, t: &mut Activation, m: M) -> ActorResult {
|
2021-07-15 07:13:31 +00:00
|
|
|
match &mut self.message_handler {
|
|
|
|
Some(handler) => handler(&mut self.state, t, m),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
2021-08-28 16:50:55 +00:00
|
|
|
|
2021-08-30 12:17:40 +00:00
|
|
|
fn stop(&mut self, t: &mut Activation) -> ActorResult {
|
|
|
|
match &mut self.stop_handler {
|
|
|
|
Some(handler) => handler(&mut self.state, t),
|
|
|
|
None => Ok(()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-30 13:25:58 +00:00
|
|
|
fn exit_hook(&mut self, t: &mut Activation, exit_status: &Arc<ExitStatus>) {
|
MAJOR REFACTORING OF CORE ASSERTION-TRACKING STRUCTURES. Little impact on API. Read on for details.
2022-02-01 15:22:30 Two problems.
- If a stop action panics (in `_terminate_facet`), the Facet is dropped before its outbound
handles are removed. With the code as it stands, this leaks assertions (!!).
- The logic for removing an outbound handle seems to be running in the wrong facet context???
(See `f.outbound_handles.remove(&handle)` in the cleanup actions
- I think I need to remove the for_myself mechanism
- and add some callbacks to run only on successful commit
2022-02-02 12:12:33 This is hard.
Here's the current implementation:
- assert
- inserts into outbound_handles of active facet
- adds cleanup action describing how to do the retraction
- enqueues the assert action, which
- calls e.assert()
- retract
- looks up & removes the cleanup action, which
- enqueues the retract action, which
- removes from outbound_handles of the WRONG facet in the WRONG actor
- calls e.retract()
- _terminate_facet
- uses outbound_handles to retract the facet's assertions
- doesn't directly touch cleanup actions, relying on retract to do that
- if one of a facet's stop actions panics, will drop the facet, leaking its assertions
- actually, even if a stop action yields `Err`, it will drop the facet and leak assertions
- yikes
- facet drop
- panics if outbound_handles is nonempty
- actor cleanup
- relies on facet tree to find assertions to retract
Revised plan:
- ✓ revise Activation/PendingEvents structures
- rename `cleanup_actions` to `outbound_assertions`
- remove `for_myself` queues and `final_actions`
- add `pre_commit_actions`, `rollback_actions` and `commit_actions`
- ✓ assert
- as before
- but on rollback, removes from `outbound_handles` (if the facet still exists) and
`outbound_assertions` (always)
- marks the new assertion as "established" on commit
- ✓ retract
- lookup in `outbound_assertions` by handle, using presence as indication it hasn't been
scheduled in this turn
- on rollback, put it back in `outbound_assertions` ONLY IF IT IS MARKED ESTABLISHED -
otherwise it is a retraction of an `assert` that has *also* been rolled back in this turn
- on commit, remove it from `outbound_handles`
- enqueue the retract action, which just calls e.retract()
- ✓ _terminate_facet
- revised quite a bit now we rely on `RunningActor::cleanup` to use `outbound_assertions`
rather than the facet tree.
- still drops Facets on panic, but this is now mostly harmless (reorders retractions a bit)
- handles `Err` from a stop action more gracefully
- slightly cleverer tracking of what needs doing based on a `TerminationDirection`
- now ONLY applies to ORDERLY cleanup of the facet tree. Disorderly cleanup ignores the
facet tree and just retracts the assertions willy-nilly.
- ✓ facet drop
- warn if outbound_handles is nonempty, but don't do anything about it
- ✓ actor cleanup
- doesn't use the facet tree at all.
- cleanly shutting down is done elsewhere
- uses the remaining entries in `outbound_assertions` (previously `cleanup_actions`) to
deal with retractions for dropped facets as well as any other facets that haven't been
cleanly shut down
- ✓ activate
- now has a panic_guard::PanicGuard RAII for conveying a crash to an actor in case the
activation is happening from a linked task or another thread (this wasn't the case in the
examples that provoked this work, though)
- simplified
- explicit commit/rollback decision
- ✓ Actor::run
- no longer uses the same path for crash-termination and success-termination
- instead, for success-termination, takes a turn that calls Activation::stop_root
- this cleans up the facet tree using _terminate_facet
- when the turn ends, it notices that the root facet is gone and shuts down the actor
- so in principle there will be nothing for actor cleanup to do
2022-02-04 13:52:34 This took days. :-(
2022-02-04 12:59:37 +00:00
|
|
|
if let Some(handler) = &mut self.exit_handler {
|
|
|
|
handler(&mut self.state, t, exit_status);
|
2021-08-28 16:50:55 +00:00
|
|
|
}
|
|
|
|
}
|
2021-07-08 22:04:11 +00:00
|
|
|
}
|