From 28a978a49b928b611836c4e5c0c1e451ee53e73e Mon Sep 17 00:00:00 2001 From: "Nuem.dev" <84929587+Manuel1234477@users.noreply.github.com> Date: Fri, 23 Oct 2026 13:55:20 +0000 Subject: [PATCH 1/2] Handle clock stepping back in mailbox prune next_prune() called created.elapsed().expect(...), which panics when a mailbox timestamp is later than the current time. That happens if the system clock is stepped back after a mailbox was written, e.g. NTP correcting the time on a host without an RTC. Timestamps loaded from file metadata at startup can be affected the same way. prune() runs on every post and read through has_capacity(), so while the clock is behind, every mailbox request panics. The background prune task panics too and is never restarted. Treat a future timestamp as zero elapsed time instead. The entry still expires once the clock passes created + ttl. --- payjoin-mailroom/CHANGELOG.md | 1 + payjoin-mailroom/src/db/files.rs | 40 +++++++++++++++++++++++++++++++- 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/payjoin-mailroom/CHANGELOG.md b/payjoin-mailroom/CHANGELOG.md index 9a5c0ccfc..0fc688730 100644 --- a/payjoin-mailroom/CHANGELOG.md +++ b/payjoin-mailroom/CHANGELOG.md @@ -4,6 +4,7 @@ - Let relay operators accept gateway purposes besides BIP77 with `[ohttp_relay] extra_allowed_purposes` (#2024) - Reject mailbox payloads too large to return through the OHTTP gateway with 413, and V1 requests too large to relay with `original-psbt-rejected`, instead of truncating the response (#1983) +- Stop mailbox requests and the background prune task from panicking when the system clock steps back ## 0.1.3 diff --git a/payjoin-mailroom/src/db/files.rs b/payjoin-mailroom/src/db/files.rs index 1f6a77ece..b2e734953 100644 --- a/payjoin-mailroom/src/db/files.rs +++ b/payjoin-mailroom/src/db/files.rs @@ -485,8 +485,10 @@ impl Mailboxes { self.insert_order .front() .map(|(created, _id)| { + // A timestamp in the future means the clock was stepped back + // since the mailbox was written. Treat it as just created. self.ttl - .checked_sub(created.elapsed().expect("system clock moved back")) + .checked_sub(created.elapsed().unwrap_or(Duration::ZERO)) .unwrap_or(self.ttl) }) .unwrap_or(self.ttl) @@ -971,6 +973,42 @@ mod tests { Ok(()) } + #[tokio::test] + async fn test_prune_with_future_timestamp() -> std::io::Result<()> { + let dir = tempfile::tempdir()?; + let ttl = Duration::from_secs(600); + + let db = FilesDb::init(Duration::from_millis(2), dir.path().to_owned(), ttl) + .await + .expect("initializing mailbox database should succeed"); + + let id = ShortId([0u8; 8]); + db.post_v2_payload(&id, b"fooo".to_vec()) + .await + .expect("posting payload should succeed") + .expect("contents should be accepted"); + + // Simulate the system clock being stepped back after the write + { + let mut guard = db.mailboxes.lock().await; + for (ts, _) in guard.insert_order.iter_mut() { + *ts += Duration::from_secs(60); + } + } + + assert_eq!(db.prune().await.expect("pruning should not fail"), ttl); + assert_eq!(db.mailboxes.lock().await.len(), 1); + + // Other requests must keep working while the clock catches up + db.post_v2_payload(&ShortId([1u8; 8]), b"barr".to_vec()) + .await + .expect("posting payload should succeed") + .expect("contents should be accepted"); + assert_eq!(db.mailboxes.lock().await.len(), 2); + + Ok(()) + } + async fn assert_all_paths_over_capacity(db: &FilesDb) { let id = ShortId([0; 8]); From 98eb9bd43b488406d54f85b4bdad670cb7322c85 Mon Sep 17 00:00:00 2001 From: "Nuem.dev" <84929587+Manuel1234477@users.noreply.github.com> Date: Tue, 3 Nov 2026 13:21:13 +0000 Subject: [PATCH 2/2] Log when clock stepping back is detected next_prune treats a mailbox timestamp in the future as just created so a backward clock step no longer panics. That fallback was silent, leaving operators no way to notice the step happened. Emit a debug message with the size of the step when elapsed() fails. Debug level keeps the request-path prune from flooding warn logs while the clock catches up. --- payjoin-mailroom/src/db/files.rs | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/payjoin-mailroom/src/db/files.rs b/payjoin-mailroom/src/db/files.rs index b2e734953..f2dc13659 100644 --- a/payjoin-mailroom/src/db/files.rs +++ b/payjoin-mailroom/src/db/files.rs @@ -11,7 +11,7 @@ use rand::RngCore; use tokio::fs::{self, File}; use tokio::io::{self, AsyncReadExt, AsyncWriteExt}; use tokio::sync::{oneshot, Mutex}; -use tracing::{trace, warn}; +use tracing::{debug, trace, warn}; use crate::db::{Db as DbTrait, Error as DbError}; @@ -487,9 +487,14 @@ impl Mailboxes { .map(|(created, _id)| { // A timestamp in the future means the clock was stepped back // since the mailbox was written. Treat it as just created. - self.ttl - .checked_sub(created.elapsed().unwrap_or(Duration::ZERO)) - .unwrap_or(self.ttl) + let elapsed = created.elapsed().unwrap_or_else(|e| { + debug!( + "System clock moved back by {:?} since mailbox was written", + e.duration() + ); + Duration::ZERO + }); + self.ttl.checked_sub(elapsed).unwrap_or(self.ttl) }) .unwrap_or(self.ttl) }