cultmesh-rs: close waits briefly for its unsubscribes to be acknowledged before the goodbye

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-11-28 08:50:25 +01:00
commit 6a86f49532

View file

@ -331,6 +331,8 @@ fn open(options: &CultMeshRudpSubscriptionOptions) -> Result<CultNetRudpSocketTr
const FRAMES_PER_PASS: usize = 64;
/// The longest peer-supplied goodbye text an event carries.
const REASON_CHARS: usize = 200;
/// The longest `close` waits for its unsubscribes to be acknowledged.
const CLOSE_ACK_WAIT: Duration = Duration::from_millis(500);
struct Pump {
options: CultMeshRudpSubscriptionOptions,
@ -746,6 +748,16 @@ impl Pump {
let message = CultNetMessage::DatabaseUnsubscribe { message_id: self.next_message_id(), subscription_id: id };
self.transmit(&message);
}
// The unsubscribes are reliable sends: give them a moment to be acknowledged (resending a lost one) before the
// goodbye ends the session, since nothing resends after it.
let deadline = Instant::now() + CLOSE_ACK_WAIT;
while self.session.is_some() && !self.receipts.is_empty() && Instant::now() < deadline {
self.service();
if self.invalidated().is_some() {
break;
}
thread::sleep(self.options.poll_interval);
}
if let Some(session) = self.session.as_mut() {
let _ = session.disconnect(b"subscription-closed".to_vec());
}