From ad0d85418456b82dc4f392c7e3ad0ecd952891d5 Mon Sep 17 00:00:00 2001 From: bryan Date: Thu, 22 Oct 2026 09:41:34 -0700 Subject: [PATCH 1/3] fix(minimald): a listing reads a session's host from a slot the actor publishes A listing's attrs probe went through the actor's mailbox, so an actor parked in a long message (a package-graph clone inside ConfigureLoadout, an inline attach) held every `min ls` for the probe's full deadline: 2 s per call through the whole of a stalled activation (710d). The actor now publishes the host it holds to a slot its handle reads, after every message and wherever a host is installed, and empties it when the actor ends. Ported onto main's split box_actor files, on top of #2224's rebase. The ended-actor test puts its host until the put survives the actor's own publishes, which is what made it flaky on a two-worker runtime: the actor replies to a message before the publish that follows it. --- crates/minimald/src/box_actor.rs | 70 +++++++++------ crates/minimald/src/box_actor/handle.rs | 93 +++++++++++++++++--- crates/minimald/src/box_actor/host.rs | 7 ++ crates/minimald/src/box_actor/kind.rs | 3 +- crates/minimald/src/box_actor/tests.rs | 8 +- crates/minimald/src/box_manager/tests.rs | 101 ++++++++++++++++++++++ crates/minimald/src/box_runtime/handle.rs | 19 ++++ 7 files changed, 259 insertions(+), 42 deletions(-) diff --git a/crates/minimald/src/box_actor.rs b/crates/minimald/src/box_actor.rs index f77a875900..3a03ab2730 100644 --- a/crates/minimald/src/box_actor.rs +++ b/crates/minimald/src/box_actor.rs @@ -189,7 +189,6 @@ enum BoxMessage { /// The attaching connection's liveness, see [`BoxActor::creator`]. Option>, ), - GetHostAttrs(oneshot::Sender>), /// Hand back this session's host, minting one — with no channel bound to /// it — if the session has none running. Used by the exec path, which runs /// a command inside the session's sandbox and so needs the sandbox up, but @@ -246,6 +245,10 @@ enum BoxMessage { /// would strand it) or an `Active` with a minted host. A `Draft` that /// was merely created isn't busy — nothing is in flight to strand. IsBusy(oneshot::Sender), + /// Test-only: park the actor for the duration, holding its mailbox the + /// way a long inline message does. + #[cfg(test)] + Park(std::time::Duration), /// Shutdown-stop: kill the host (if any), withdraw the hostname, and stop /// the actor — the on-disk record is kept. Stop(oneshot::Sender<()>), @@ -399,6 +402,8 @@ enum BoxMessage { #[derive(Debug)] pub struct BoxActor { receiver: crate::traced::Receiver, + /// The slot this actor's handle reads its host from; see [`HostSlot`]. + host_slot: HostSlot, minimal_state_dir: DaemonAbsPath, minimal_cache_dir: DaemonAbsPath, daemon_ctx: Arc, @@ -633,6 +638,7 @@ impl BoxActor { workspace_uploads_in_flight: 0, creator, manager, + host_slot: Arc::clone(&weak_self.2), weak_self, destroying, // No host yet; the first launch sets this. `Interactive` is the @@ -781,7 +787,12 @@ impl BoxActor { } // A weak self-handle so the actor can hand its own mailbox to the // runtime objects it spawns without a caller threading it in. - let weak_self = WeakBoxHandle(sender.downgrade(), destroying.clone()); + let host_slot = HostSlot::default(); + let weak_self = WeakBoxHandle( + sender.downgrade(), + destroying.clone(), + Arc::clone(&host_slot), + ); let actor = Self::assemble(conf, receiver, inner, weak_self, destroying.clone()); // Register the PTask hostname before the actor goes live, so the @@ -808,7 +819,28 @@ impl BoxActor { session_id = %actor.record.id(), ); crate::traced::spawn_detached(root, actor.mainloop()); - Ok(BoxHandle(sender, destroying)) + Ok(BoxHandle(sender, destroying, host_slot)) + } + + /// Publishes the host this actor holds to the slot its handle reads + /// ([`HostSlot`]). Called after every message, so between messages, and + /// for as long as one parks the actor, the slot holds the host the actor + /// last had, and a listing's probe never waits on this mailbox. Also + /// called where a host is installed ([`Self::ensure_host`], + /// [`Self::mint_session_host`], [`Self::launch_host_for_hooks`]), which + /// is before the message that launched it replies: a caller that awaits + /// an attach or `ensure_host` and then reads attrs sees the new host. + pub(super) fn publish_host(&self) { + let host = match &self.inner { + BoxInner::Active { + host: Some((h, _)), .. + } => Some(AttrsProbe(h.clone())), + _ => None, + }; + *self + .host_slot + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = host; } /// Tears the session down for good: its `on_destroy` hooks (failures @@ -870,6 +902,8 @@ impl BoxActor { /// instead of deadlocking against a full manager mailbox. async fn mainloop(mut self) { let mut teardown = Teardown::ManagerInitiated; + let _clear_slot = ClearSlotOnExit(Arc::clone(&self.host_slot)); + self.publish_host(); while let Some((msg, caller)) = self.receiver.recv().await { // Opened now, a child of the sender's ids (see // `traced::Receiver::recv`), so compose, graph resolution and @@ -889,6 +923,7 @@ impl BoxActor { teardown = t; break; } + self.publish_host(); } if matches!(teardown, Teardown::SelfInitiated) { let session_id = *self.record.id(); @@ -922,29 +957,6 @@ impl BoxActor { BoxMessage::EnsureHost(r, session_hnd, conn_username) => { let _ = r.send(self.ensure_host(session_hnd, conn_username).await); } - BoxMessage::GetHostAttrs(r) => match &self.inner { - BoxInner::Active { - host: Some((h, _)), .. - } => { - // Forwarded off-actor for the same reason as - // `GetWorkspaceDelta`: the host loop can be mid-attach or - // mid-teardown, and awaiting it from here parks this - // actor — which the manager awaits in turn, from its own - // mainloop, where a park costs every session RPC. - // - // Bounded *inside* the task: dropping the caller's - // receiver does not cancel a spawned future, so an - // unbounded probe would strand one task per poll against - // a host that never answers. - let h = h.clone(); - crate::traced::spawn(async move { - let _ = r.send(probe_host(h.get_attrs()).await); - }); - } - _ => { - let _ = r.send(None); - } - }, BoxMessage::GetWorkspaceDelta(r) => match &self.inner { BoxInner::Active { host: Some((h, _)), .. @@ -966,7 +978,7 @@ impl BoxActor { host: Some((h, _)), .. } => { // Off-actor and bounded for the same reasons as - // `GetHostAttrs`. + // `GetWorkspaceDelta`. let h = h.clone(); crate::traced::spawn(async move { let _ = r.send(probe_host(h.get_screen()).await); @@ -1027,6 +1039,10 @@ impl BoxActor { BoxMessage::Rename(new_name, r) => { let _ = r.send(self.rename(new_name).await); } + #[cfg(test)] + BoxMessage::Park(d) => { + tokio::time::sleep(d).await; + } BoxMessage::IsBusy(r) => { let _ = r.send(match &self.inner { // Awaiting a verdict: a client is mid create flow. diff --git a/crates/minimald/src/box_actor/handle.rs b/crates/minimald/src/box_actor/handle.rs index d1aa46e8a1..ece9c03df8 100644 --- a/crates/minimald/src/box_actor/handle.rs +++ b/crates/minimald/src/box_actor/handle.rs @@ -1,17 +1,56 @@ use super::*; +/// The host a box actor last published, read by its handle: a listing's +/// attrs probe goes to the host through this slot rather than through the +/// actor's mailbox, so an actor that is parked (a `ConfigureLoadout` inside +/// its package-graph clone, an inline attach) costs the listing nothing, and +/// a box with no host reads as `None` at once instead of after +/// [`HOST_PROBE_TIMEOUT`]. The actor refreshes it after every message it +/// handles ([`BoxActor::publish_host`]). +pub(crate) type HostSlot = Arc>>; + +/// Empties a [`HostSlot`] when the actor that publishes it ends, however it +/// ends: a teardown's `break` (which publishes nothing after it), or a panic +/// unwinding the actor's task. A listing then reads no host for an actor +/// that is gone, and the slot holds no sender that would keep the host's +/// mailbox open. +pub(super) struct ClearSlotOnExit(pub(super) HostSlot); + +impl Drop for ClearSlotOnExit { + fn drop(&mut self) { + *self + .0 + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = None; + } +} + +/// What the [`HostSlot`] hands out: a host's attrs, and nothing else. The +/// slot is read by every holder of a [`BoxHandle`], so it carries no way to +/// kill, attach to or rename the host: those stay with the actor, which +/// serializes them and checks an attach before binding a channel. +#[derive(Debug, Clone)] +pub(crate) struct AttrsProbe(pub(super) crate::box_runtime::HostHandle); + +impl AttrsProbe { + async fn get_attrs(&self) -> Result { + self.0.get_attrs().await + } +} + /// The handle to the session. #[derive(Debug, Clone)] pub struct BoxHandle( pub(super) crate::traced::Sender, pub(super) CancellationToken, + pub(super) HostSlot, ); impl BoxHandle { /// Returns a non-owning handle to this session. #[must_use] pub fn downgrade(&self) -> WeakBoxHandle { - WeakBoxHandle(self.0.downgrade(), self.1.clone()) + WeakBoxHandle(self.0.downgrade(), self.1.clone(), Arc::clone(&self.2)) } /// Whether this session is being destroyed. @@ -194,14 +233,41 @@ impl BoxHandle { std::io::Error::new(std::io::ErrorKind::NotConnected, "session actor is gone") }) } - /// Returns the host attributes of the running session, if any. A dead - /// actor (self-terminated or mid-teardown) reads as `None` rather than a - /// panic — the manager polls this while actors come and go. + /// Returns the host attributes of the running session, if any: `None` + /// for a session without a host, a dead host, or a host that misses + /// [`HOST_PROBE_TIMEOUT`]. Read through the host slot, not the actor's + /// mailbox (see [`HostSlot`]): the manager polls this for every running + /// session on each listing, and an actor parked in a long message held + /// every `min ls` for the probe's full deadline (710d: 2 s per call + /// through a stalled clone, on every distro). pub async fn get_attrs(&self) -> Option { - let (send, recv) = oneshot::channel(); - // Ignore send errors - the recv will also fail. - let _ = self.0.send(BoxMessage::GetHostAttrs(send)).await; - recv.await.ok().flatten() + let host = self + .2 + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) + .clone(); + probe_host(host?.get_attrs()).await + } + + /// Test-only: puts `host` in the slot a listing reads, as the actor does + /// after a message; the actor's next message overwrites it. + #[cfg(test)] + pub(crate) fn put_host_for_test(&self, host: crate::box_runtime::HostHandle) { + *self + .2 + .lock() + .unwrap_or_else(std::sync::PoisonError::into_inner) = Some(AttrsProbe(host)); + } + + /// Test-only: parks the actor for `d`, the shape of a message that holds + /// its mailbox (a clone inside `ConfigureLoadout`, an inline attach). + #[cfg(test)] + pub(crate) async fn park_for(&self, d: std::time::Duration) { + #[expect( + clippy::let_underscore_must_use, + reason = "a gone actor cannot be parked, and the test's assertion says so" + )] + let _ = self.0.send(BoxMessage::Park(d)).await; } /// Returns the workspace's at-risk report (what a destroy would lose), @@ -734,6 +800,7 @@ impl BoxHandle { pub struct WeakBoxHandle( pub(super) crate::traced::WeakSender, pub(super) CancellationToken, + pub(super) HostSlot, ); impl WeakBoxHandle { @@ -741,7 +808,11 @@ impl WeakBoxHandle { /// has already shut down (all strong senders dropped). #[must_use] pub fn upgrade(&self) -> Option { - Some(BoxHandle(self.0.upgrade()?, self.1.clone())) + Some(BoxHandle( + self.0.upgrade()?, + self.1.clone(), + Arc::clone(&self.2), + )) } /// A dangling handle whose actor is already gone (`upgrade` always yields @@ -754,7 +825,7 @@ impl WeakBoxHandle { let weak = tx.downgrade(); // Drop the only strong sender so `upgrade()` returns `None`. drop(tx); - Self(weak, CancellationToken::new()) + Self(weak, CancellationToken::new(), HostSlot::default()) } /// A handle to a stand-in "actor" that answers each expose request with @@ -792,6 +863,6 @@ impl WeakBoxHandle { } } }); - Self(weak, CancellationToken::new()) + Self(weak, CancellationToken::new(), HostSlot::default()) } } diff --git a/crates/minimald/src/box_actor/host.rs b/crates/minimald/src/box_actor/host.rs index 1a95b6a0e2..a7d35b031a 100644 --- a/crates/minimald/src/box_actor/host.rs +++ b/crates/minimald/src/box_actor/host.rs @@ -363,6 +363,10 @@ impl BoxActor { BoxInner::Active { host, sops, .. } => Some((host.take(), std::mem::take(sops))), BoxInner::Draft { .. } => None, }; + // The host is out of the actor's hands now: out of the slot too, so a + // handle that outlives a `Stop` reads no host rather than probing the + // one being stopped. + self.publish_host(); if let Some((host, mut sops)) = inner { for s in sops.drain(..) { s.shutdown().await; @@ -750,6 +754,7 @@ impl BoxActor { // Minted for an exec: a command is about to run in this sandbox, so a // later attach must not replace it. See [`HostOrigin::Exec`]. self.host_origin = HostOrigin::Exec; + self.publish_host(); Ok(host) } @@ -828,6 +833,7 @@ impl BoxActor { // Minted by an attach: its environment describes the terminal that is // here, so nothing may replace it out from under that client. self.host_origin = HostOrigin::Interactive; + self.publish_host(); outcome } @@ -983,6 +989,7 @@ impl BoxActor { // attach may replace this shell rather than inherit its blank `TERM`. // See [`HostOrigin::Hooks`]. self.host_origin = HostOrigin::Hooks; + self.publish_host(); Ok(()) } } diff --git a/crates/minimald/src/box_actor/kind.rs b/crates/minimald/src/box_actor/kind.rs index 434f6cb581..6df670f57f 100644 --- a/crates/minimald/src/box_actor/kind.rs +++ b/crates/minimald/src/box_actor/kind.rs @@ -9,7 +9,6 @@ impl BoxMessage { Self::GetPaths { .. } => "GetPaths", Self::MakeContext { .. } => "MakeContext", Self::Attach { .. } => "Attach", - Self::GetHostAttrs { .. } => "GetHostAttrs", Self::EnsureHost { .. } => "EnsureHost", Self::GetWorkspaceDelta { .. } => "GetWorkspaceDelta", Self::GetHostScreen { .. } => "GetHostScreen", @@ -42,6 +41,8 @@ impl BoxMessage { #[cfg(test)] Self::PeekPendingAsks(_) => "PeekPendingAsks", #[cfg(test)] + Self::Park(_) => "Park", + #[cfg(test)] Self::PeekComposition { .. } => "PeekComposition", #[cfg(test)] Self::PeekIngressCells(_) => "PeekIngressCells", diff --git a/crates/minimald/src/box_actor/tests.rs b/crates/minimald/src/box_actor/tests.rs index 8f86319fb6..54f28feecf 100644 --- a/crates/minimald/src/box_actor/tests.rs +++ b/crates/minimald/src/box_actor/tests.rs @@ -15,7 +15,8 @@ const GIVE_UP: Duration = Duration::from_secs(600); /// A probe of a host that never answers has to end on its own deadline. /// -/// The session actor forwards `GetHostAttrs`/`GetHostScreen` to a spawned +/// The session actor forwards `GetHostScreen` (and, through the host slot, +/// the listing's attrs probe takes the same bounded path) to a spawned /// task so the actor stays responsive, but nothing cancels that task: /// dropping the caller's receiver does not stop a spawned future. Left /// unbounded, every poll of a wedged host stranded one task forever — and @@ -1866,7 +1867,7 @@ async fn missing_sidecar_falls_back_to_baseline() { /// session sitting idle with nobody attached must still be able to launch /// a host, and a second request must reuse it rather than starting a /// second shell in the same session. -#[tokio::test] +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn ensure_host_launches_an_unattached_host_and_then_reuses_it() { let server = TestServer::new().await; let mut client = server.connect().await; @@ -1891,7 +1892,8 @@ async fn ensure_host_launches_an_unattached_host_and_then_reuses_it() { assert!(host.is_alive()); assert!( handle.get_attrs().await.is_some(), - "the session should now be holding the host it launched" + "the session should now be holding the host it launched: the slot is \ + published before `ensure_host` replies, on a multi-thread runtime too" ); let again = handle diff --git a/crates/minimald/src/box_manager/tests.rs b/crates/minimald/src/box_manager/tests.rs index 722097eb9e..ffe92dfb42 100644 --- a/crates/minimald/src/box_manager/tests.rs +++ b/crates/minimald/src/box_manager/tests.rs @@ -858,6 +858,107 @@ async fn a_listing_survives_a_cleanup_destroy_of_an_unfinished_session() { ); } +/// A listing must not wait on a box actor: its attrs come from the host the +/// actor last published, not from the actor's mailbox. With an actor parked +/// (a clone inside `ConfigureLoadout`, an inline attach), `list` answered +/// only after `HOST_PROBE_TIMEOUT` for that box: 2 s per `min ls` through +/// the whole of a stalled activation (710d). +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_listing_does_not_wait_on_a_parked_actor() { + let (_state, _cache, mngr) = manager().await; + let id = create_active_session(&mngr).await; + let handle = mngr + .get_session(BoxKeyPredicate::Id(id)) + .await + .unwrap() + .expect("session should resolve"); + + handle.park_for(std::time::Duration::from_secs(3)).await; + // Let the actor take the park before measuring. + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + let started = std::time::Instant::now(); + let listed = tokio::time::timeout(std::time::Duration::from_millis(500), mngr.list()) + .await + .expect("`list` waited on the parked actor") + .unwrap(); + assert_eq!(listed.len(), 1); + assert!(listed[0].attrs.is_none(), "no host, so no attrs"); + assert!( + started.elapsed() < std::time::Duration::from_millis(500), + "the listing took {:?}", + started.elapsed() + ); + drop(handle); +} + +/// The slot path with a live host, the 710d case itself: an actor parked in +/// a long message still has its host's attrs listed, at once, read through +/// the slot and not the actor's mailbox. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn a_parked_actors_live_host_is_still_listed() { + let (_state, _cache, mngr) = manager().await; + let id = create_active_session(&mngr).await; + let handle = mngr + .get_session(BoxKeyPredicate::Id(id)) + .await + .unwrap() + .expect("session should resolve"); + + handle.park_for(std::time::Duration::from_secs(3)).await; + tokio::time::sleep(std::time::Duration::from_millis(100)).await; + // Published while the actor is parked, as a host it had before would be. + handle.put_host_for_test(crate::box_runtime::HostHandle::answering_attrs()); + let listed = tokio::time::timeout(std::time::Duration::from_millis(500), mngr.list()) + .await + .expect("`list` waited on the parked actor") + .unwrap(); + assert_eq!(listed.len(), 1); + assert!( + listed[0].attrs.is_some(), + "the live host's attrs come through the slot while the actor is parked" + ); + drop(handle); +} + +/// The slot is emptied when the actor ends, so a handle that outlives it +/// reads no host, and holds no sender to one. The destroy's message ends the +/// actor with a `break`, after which nothing is published. +#[tokio::test(flavor = "multi_thread", worker_threads = 2)] +async fn an_ended_actors_slot_is_emptied() { + let (_state, _cache, mngr) = manager().await; + let id = create_active_session(&mngr).await; + let handle = mngr + .get_session(BoxKeyPredicate::Id(id)) + .await + .unwrap() + .expect("session should resolve"); + // The actor rewrites the slot after every message it handles, and it + // replies to a message before that rewrite, so a host put right after a + // reply can still be overwritten by the publish that follows it. Put + // until the put survives: once the mailbox is empty nothing rewrites + // the slot, and the host stays. + tokio::time::timeout(std::time::Duration::from_secs(5), async { + loop { + handle.put_host_for_test(crate::box_runtime::HostHandle::answering_attrs()); + tokio::time::sleep(std::time::Duration::from_millis(20)).await; + if handle.get_attrs().await.is_some() { + break; + } + } + }) + .await + .expect("no put of a host survived the actor's publishes"); + mngr.delete_session(id).await.unwrap(); + // The destroy replies just before the actor's loop ends. + tokio::time::timeout(std::time::Duration::from_secs(2), async { + while handle.get_attrs().await.is_some() { + tokio::time::sleep(std::time::Duration::from_millis(10)).await; + } + }) + .await + .expect("the ended actor's slot still served a host"); +} + /// Destroying an unknown ID is a `NotFound` error. #[tokio::test(flavor = "multi_thread", worker_threads = 2)] async fn destroy_unknown_id_errors() { diff --git a/crates/minimald/src/box_runtime/handle.rs b/crates/minimald/src/box_runtime/handle.rs index 3a8128c094..8b21896538 100644 --- a/crates/minimald/src/box_runtime/handle.rs +++ b/crates/minimald/src/box_runtime/handle.rs @@ -248,6 +248,25 @@ impl HostHandle { let (sender, receiver) = mpsc::channel(HOST_MAILBOX_CAPACITY); (Self { sender }, WedgedMailbox(receiver)) } + + /// A handle to a host that answers every attrs probe at once (with + /// default attrs) and ignores everything else: a live host, as far as a + /// listing can tell. + pub(crate) fn answering_attrs() -> Self { + let (sender, mut receiver) = mpsc::channel(HOST_MAILBOX_CAPACITY); + crate::traced::spawn(async move { + while let Some(message) = receiver.recv().await { + if let Message::GetAttrs(reply) = message { + #[expect( + clippy::let_underscore_must_use, + reason = "a prober that went away needs no answer" + )] + let _ = reply.send(HostAttrs::default()); + } + } + }); + Self { sender } + } } impl HostHandle { From e8a8bc21fbc6bf30cba93bd3dd02e1a85d8a68c2 Mon Sep 17 00:00:00 2001 From: bryan Date: Thu, 22 Oct 2026 13:28:11 -0700 Subject: [PATCH 2/3] fix(minimald): the teardown task's shutdown refusals use SHUTDOWN_REFUSAL, as the inline one does --- crates/minimald/src/box_manager.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/crates/minimald/src/box_manager.rs b/crates/minimald/src/box_manager.rs index c4acba5d50..2a56a7c6fa 100644 --- a/crates/minimald/src/box_manager.rs +++ b/crates/minimald/src/box_manager.rs @@ -1785,7 +1785,7 @@ impl BoxManager { if seed.is_some() && in_shutdown.is_cancelled() { return Err(BoxManagerError::new( std::io::ErrorKind::ConnectionRefused, - "in shutdown", + minimald_rpc::SHUTDOWN_REFUSAL, )); } let actor = match (claimed, seed) { @@ -1800,7 +1800,7 @@ impl BoxManager { hnd.stop().await; return Err(BoxManagerError::new( std::io::ErrorKind::ConnectionRefused, - "in shutdown", + minimald_rpc::SHUTDOWN_REFUSAL, )); } Ok(hnd) => Some(hnd), From ea819ab738fcbece63c4fc138f7b3f14a2f658c1 Mon Sep 17 00:00:00 2001 From: bryan Date: Thu, 22 Oct 2026 13:28:11 -0700 Subject: [PATCH 3/3] test(minimald): park_for returns once the actor is parked, not after a fixed sleep --- crates/minimald/src/box_actor.rs | 13 ++++++++++--- crates/minimald/src/box_actor/handle.rs | 10 ++++++++-- crates/minimald/src/box_actor/kind.rs | 2 +- crates/minimald/src/box_manager/tests.rs | 8 ++++---- 4 files changed, 23 insertions(+), 10 deletions(-) diff --git a/crates/minimald/src/box_actor.rs b/crates/minimald/src/box_actor.rs index 3a03ab2730..b23a03c321 100644 --- a/crates/minimald/src/box_actor.rs +++ b/crates/minimald/src/box_actor.rs @@ -246,9 +246,11 @@ enum BoxMessage { /// was merely created isn't busy — nothing is in flight to strand. IsBusy(oneshot::Sender), /// Test-only: park the actor for the duration, holding its mailbox the - /// way a long inline message does. + /// way a long inline message does. The sender is answered as the park + /// begins, so a test knows every earlier message, and the publish that + /// follows it, is done. #[cfg(test)] - Park(std::time::Duration), + Park(std::time::Duration, oneshot::Sender<()>), /// Shutdown-stop: kill the host (if any), withdraw the hostname, and stop /// the actor — the on-disk record is kept. Stop(oneshot::Sender<()>), @@ -1040,7 +1042,12 @@ impl BoxActor { let _ = r.send(self.rename(new_name).await); } #[cfg(test)] - BoxMessage::Park(d) => { + BoxMessage::Park(d, entered) => { + #[expect( + clippy::let_underscore_must_use, + reason = "the parker may already be gone; the park still runs" + )] + let _ = entered.send(()); tokio::time::sleep(d).await; } BoxMessage::IsBusy(r) => { diff --git a/crates/minimald/src/box_actor/handle.rs b/crates/minimald/src/box_actor/handle.rs index ece9c03df8..555492643c 100644 --- a/crates/minimald/src/box_actor/handle.rs +++ b/crates/minimald/src/box_actor/handle.rs @@ -261,13 +261,19 @@ impl BoxHandle { /// Test-only: parks the actor for `d`, the shape of a message that holds /// its mailbox (a clone inside `ConfigureLoadout`, an inline attach). + /// Returns once the actor is inside the park, so every earlier message + /// and the slot publish after it are done. #[cfg(test)] pub(crate) async fn park_for(&self, d: std::time::Duration) { + let (entered, entered_recv) = oneshot::channel(); #[expect( clippy::let_underscore_must_use, - reason = "a gone actor cannot be parked, and the test's assertion says so" + reason = "a gone actor cannot be parked, and the recv below says so" )] - let _ = self.0.send(BoxMessage::Park(d)).await; + let _ = self.0.send(BoxMessage::Park(d, entered)).await; + entered_recv + .await + .expect("the session actor should enter the park"); } /// Returns the workspace's at-risk report (what a destroy would lose), diff --git a/crates/minimald/src/box_actor/kind.rs b/crates/minimald/src/box_actor/kind.rs index 6df670f57f..24ade1f4df 100644 --- a/crates/minimald/src/box_actor/kind.rs +++ b/crates/minimald/src/box_actor/kind.rs @@ -41,7 +41,7 @@ impl BoxMessage { #[cfg(test)] Self::PeekPendingAsks(_) => "PeekPendingAsks", #[cfg(test)] - Self::Park(_) => "Park", + Self::Park(..) => "Park", #[cfg(test)] Self::PeekComposition { .. } => "PeekComposition", #[cfg(test)] diff --git a/crates/minimald/src/box_manager/tests.rs b/crates/minimald/src/box_manager/tests.rs index ffe92dfb42..9e4cd775fc 100644 --- a/crates/minimald/src/box_manager/tests.rs +++ b/crates/minimald/src/box_manager/tests.rs @@ -873,9 +873,9 @@ async fn a_listing_does_not_wait_on_a_parked_actor() { .unwrap() .expect("session should resolve"); + // Returns once the actor is inside the park: every earlier message, and + // its post-message publish, is done. handle.park_for(std::time::Duration::from_secs(3)).await; - // Let the actor take the park before measuring. - tokio::time::sleep(std::time::Duration::from_millis(100)).await; let started = std::time::Instant::now(); let listed = tokio::time::timeout(std::time::Duration::from_millis(500), mngr.list()) .await @@ -905,8 +905,8 @@ async fn a_parked_actors_live_host_is_still_listed() { .expect("session should resolve"); handle.park_for(std::time::Duration::from_secs(3)).await; - tokio::time::sleep(std::time::Duration::from_millis(100)).await; - // Published while the actor is parked, as a host it had before would be. + // Published while the actor is parked (park_for returns once it is), as + // a host it had before would be: nothing overwrites it until the park ends. handle.put_host_for_test(crate::box_runtime::HostHandle::answering_attrs()); let listed = tokio::time::timeout(std::time::Duration::from_millis(500), mngr.list()) .await