Skip to content

: clock: bind undeliverable port in sim clock's mailbox #517

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 34 additions & 3 deletions hyperactor/src/clock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,25 @@ use std::error::Error;
use std::fmt;
use std::sync::LazyLock;
use std::sync::Mutex;
use std::sync::OnceLock;
use std::time::SystemTime;

use futures::pin_mut;
use hyperactor_telemetry::TelemetryClock;
use serde::Deserialize;
use serde::Serialize;

use crate::ActorId;
use crate::Mailbox;
use crate::channel::ChannelAddr;
use crate::data::Named;
use crate::id;
use crate::mailbox::DeliveryError;
use crate::mailbox::MailboxSender;
use crate::mailbox::MessageEnvelope;
use crate::mailbox::Undeliverable;
use crate::mailbox::UndeliverableMailboxSender;
use crate::mailbox::monitored_return_handle;
use crate::simnet::SleepEvent;
use crate::simnet::simnet_handle;

Expand Down Expand Up @@ -175,6 +184,28 @@ impl ClockKind {
}
}

// TODO (SF, 2025-07-11): Remove this global, thread through a mailbox
// from upstack and handle undeliverable messages properly.
fn simclock_mailbox() -> &'static Mailbox {
static SIMCLOCK_MAILBOX: OnceLock<Mailbox> = OnceLock::new();
SIMCLOCK_MAILBOX.get_or_init(|| {
let mailbox = Mailbox::new_detached(id!(proc[0].proc).clone());
let (undeliverable_messages, mut rx) =
mailbox.open_port::<Undeliverable<MessageEnvelope>>();
undeliverable_messages.bind_to(Undeliverable::<MessageEnvelope>::port());
tokio::spawn(async move {
while let Ok(Undeliverable(mut envelope)) = rx.recv().await {
envelope.try_set_error(DeliveryError::BrokenLink(
"message returned to undeliverable port".to_string(),
));
UndeliverableMailboxSender
.post(envelope, /*unused */ monitored_return_handle())
}
});
mailbox
})
}

/// Clock to be used in simulator runs that allows the simnet to create a scheduled event for.
/// When the wakeup event becomes the next earliest scheduled event, the simnet will advance it's
/// time to the wakeup time and use the transmitter to wake up this green thread
Expand All @@ -184,7 +215,7 @@ pub struct SimClock;
impl Clock for SimClock {
/// Tell the simnet to wake up this green thread after the specified duration has pass on the simnet
async fn sleep(&self, duration: tokio::time::Duration) {
let mailbox = Mailbox::new_detached(id!(proc[0].proc).clone());
let mailbox = simclock_mailbox().clone();
let (tx, rx) = mailbox.open_once_port::<()>();

simnet_handle()
Expand All @@ -199,7 +230,7 @@ impl Clock for SimClock {
}

async fn non_advancing_sleep(&self, duration: tokio::time::Duration) {
let mailbox = Mailbox::new_detached(id!(proc[0].proc).clone());
let mailbox = simclock_mailbox().clone();
let (tx, rx) = mailbox.open_once_port::<()>();

simnet_handle()
Expand Down Expand Up @@ -234,7 +265,7 @@ impl Clock for SimClock {
where
F: std::future::Future<Output = T>,
{
let mailbox = Mailbox::new_detached(id!(proc[0].proc).clone());
let mailbox = simclock_mailbox().clone();
let (tx, deadline_rx) = mailbox.open_once_port::<()>();

simnet_handle()
Expand Down
4 changes: 2 additions & 2 deletions hyperactor/src/mailbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1310,7 +1310,7 @@ impl cap::sealed::CanSend for Mailbox {
.get_or_init(DashSet::new)
.insert(actor_id.clone())
{
let bt = std::backtrace::Backtrace::capture();
let bt = std::backtrace::Backtrace::force_capture();
tracing::warn!(
actor_id = ?actor_id,
backtrace = ?bt,
Expand Down Expand Up @@ -2834,7 +2834,7 @@ mod tests {
"returned in unit test".to_string(),
));
UndeliverableMailboxSender
.post(envelope, /*unused */ monitored_return_handle().clone());
.post(envelope, /*unused */ monitored_return_handle());
}
});
drop(return_handle);
Expand Down