cultnet-rs presence publisher: wait_for_write_lease never overruns the caller's timeout
Some checks failed
Rust crates / cargo test, fmt, clippy (pull_request) Has been cancelled
CultMesh portability and performance / ubuntu-latest reactive semantics and leases (pull_request) Has been cancelled

Heartbeat delivery clips its connect and ack waits to the time remaining.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 14:32:06 +01:00
commit 17c1d6e758
2 changed files with 50 additions and 8 deletions

View file

@ -164,7 +164,7 @@ impl IdunnRuntimePresencePublisher {
signer: &mut IdunnRuntimePresenceSigner,
detail: &str,
) -> Result<SignedRuntimePresence> {
let (signed, delivery) = self.sign_and_deliver(signer, detail)?;
let (signed, delivery) = self.sign_and_deliver(signer, detail, None)?;
delivery?;
Ok(signed)
}
@ -200,8 +200,11 @@ impl IdunnRuntimePresencePublisher {
loop {
let now = Instant::now();
if now >= next_heartbeat {
let (_, delivery) =
self.sign_and_deliver(signer, "waiting-for-process-write-lease")?;
let (_, delivery) = self.sign_and_deliver(
signer,
"waiting-for-process-write-lease",
Some(started + timeout),
)?;
if let Err(failure) = delivery {
failed_deliveries += 1;
last_failure = Some(failure);
@ -227,10 +230,11 @@ impl IdunnRuntimePresencePublisher {
&self,
signer: &mut IdunnRuntimePresenceSigner,
detail: &str,
deadline: Option<Instant>,
) -> Result<(SignedRuntimePresence, Result<(), DeliveryFailure>)> {
self.require_own_signer(signer)?;
let signed = signer.sign(detail)?;
let delivery = self.deliver(&signed);
let delivery = self.deliver(&signed, deadline);
Ok((signed, delivery))
}
@ -246,8 +250,17 @@ impl IdunnRuntimePresencePublisher {
/// One fresh session: connect, one reliable `DocumentPutRaw` on `schema`,
/// wait for the ack. The message id comes from the signed record, so the
/// publisher keeps no counter.
fn deliver(&self, signed: &SignedRuntimePresence) -> Result<(), DeliveryFailure> {
/// publisher keeps no counter. A `deadline` clips the connect and ack waits
/// so a caller's timeout is never overrun by more than one receive slice.
fn deliver(
&self,
signed: &SignedRuntimePresence,
deadline: Option<Instant>,
) -> Result<(), DeliveryFailure> {
let bounded = |wait: Duration| {
let due = Instant::now() + wait;
deadline.map_or(due, |deadline| due.min(deadline))
};
let remote = (self.host.as_str(), self.port)
.to_socket_addrs()
.ok()
@ -274,7 +287,7 @@ impl IdunnRuntimePresencePublisher {
transport
.connect(Vec::new())
.map_err(|_| DeliveryFailure::SendFailed)?;
let connect_deadline = Instant::now() + self.cadence.connect;
let connect_deadline = bounded(self.cadence.connect);
loop {
pump(&mut transport)?;
if transport.connected() {
@ -298,7 +311,7 @@ impl IdunnRuntimePresencePublisher {
let receipt = transport
.send_reliable("schema", payload)
.map_err(|_| DeliveryFailure::SendFailed)?;
let ack_deadline = Instant::now() + self.cadence.ack;
let ack_deadline = bounded(self.cadence.ack);
loop {
pump(&mut transport)?;
match transport.reliable_send_status(&receipt) {

View file

@ -642,6 +642,35 @@ mod lease_wait {
Ok(())
}
/// An unreachable Odin with connect and ack waits far longer than the
/// caller's timeout: the wait must return at the timeout, over by at most
/// one receive slice plus scheduling. The slack is 400 ms; unbounded, the
/// overrun is the 2 s connect wait, so the bound is not a race.
#[test]
fn lease_wait_never_overruns_the_timeout() -> Result<()> {
let dead = dead_address();
let rig = odin_rig(true, &format!("rudp://{dead}"))?;
let mut signer = rig.signer()?;
let cadence = Cadence {
heartbeat: Duration::from_secs(5),
connect: Duration::from_secs(2),
ack: Duration::from_secs(2),
..TEST_CADENCE
};
let publisher = publisher_for(&signer, cadence)?;
let timeout = Duration::from_millis(300);
let slack = Duration::from_millis(400);
let started = Instant::now();
let refused = refusal(publisher.wait_for_write_lease(&mut signer, timeout));
let elapsed = started.elapsed();
assert!(elapsed >= timeout, "{elapsed:?}");
assert!(elapsed < timeout + slack, "{elapsed:?}");
assert!(refused.contains("1 failed deliveries"), "{refused}");
Ok(())
}
#[test]
fn lease_wait_timeout_counts_only_failed_deliveries() -> Result<()> {
let odin = Odin::start(Sink::Accept);