From 45a41d4e5cd1113927ed29f3ada19eda7a70cb5b Mon Sep 17 00:00:00 2001 From: Nils Pukropp Date: Wed, 25 Nov 2026 02:00:33 +0100 Subject: [PATCH] fix(core,fuse): keep the mount failure file readable after a downgrade, test the key wait Mark the wait for key recovery with a separate awaiting_keys field instead of a null next_attempt, which an older daemon cannot parse. Pin the missing-key match against a real rpgp decrypt failure, including the try-every-key path, and test that a parked daemon pings the watchdog until it is stopped. --- crates/pdfs-cli/src/main.rs | 5 +- crates/pdfs-core/src/mountfail.rs | 73 +++++++++++++++++++++++-- crates/pdfs-fuse/Cargo.toml | 1 + crates/pdfs-fuse/src/systemd.rs | 52 ++++++++++++++++-- crates/pdfs-gui/src/app/main.rs | 2 +- crates/pdfs-gui/src/app/pages/status.rs | 2 +- crates/pdfs-gui/src/main.rs | 2 +- 7 files changed, 120 insertions(+), 17 deletions(-) diff --git a/crates/pdfs-cli/src/main.rs b/crates/pdfs-cli/src/main.rs index 5a4308c..466b12c 100644 --- a/crates/pdfs-cli/src/main.rs +++ b/crates/pdfs-cli/src/main.rs @@ -2706,7 +2706,8 @@ fn cmd_daemon(mountpoint: Option) -> Result<()> { error: error.clone(), since: failure.as_ref().map_or(now, |f| f.since), attempts, - next_attempt: delay.map(|delay| now + delay.as_secs() as i64), + next_attempt: now + delay.map_or(0, |delay| delay.as_secs() as i64), + awaiting_keys: delay.is_none(), not_empty: not_empty_cause(&e), }; if let Err(e) = pdfs_core::mountfail::record(&state_dir, &record) { @@ -4657,7 +4658,7 @@ fn cmd_diagnose() -> Result<()> { "failing for {}, {} attempt(s): {}", human_secs(unix_now().saturating_sub(failure.since).max(0) as u64), failure.attempts, - if failure.next_attempt.is_none() { + if failure.awaiting_keys { format!("{} — after recovering your account keys, restart proton-drive.service", failure.error) } else { failure.error diff --git a/crates/pdfs-core/src/mountfail.rs b/crates/pdfs-core/src/mountfail.rs index 5946acc..6c5c4ff 100644 --- a/crates/pdfs-core/src/mountfail.rs +++ b/crates/pdfs-core/src/mountfail.rs @@ -22,8 +22,11 @@ impl RootLookup { pub fn needs_account_keys(&self) -> bool { use proton_sdk::{ProtonError, crypto::CryptoError}; - // SDK 0.12 exposes the underlying PGP missing-key reason as the - // payload of Decrypt. Never park for an unrelated decryption error. + // SDK 0.12 carries rpgp's `MissingKey` as the text of Decrypt. + // `a_real_missing_key_failure_waits_for_keys` pins that wording, so + // an SDK or rpgp update that changes it fails the test instead of + // silently going back to the retry loop. Never park for an unrelated + // decryption error. matches!(self.0, ProtonError::KeysLocked) || matches!(&self.0, ProtonError::Crypto(CryptoError::Decrypt(reason)) if reason == "missing key") } @@ -38,8 +41,14 @@ pub struct MountFailure { pub since: i64, /// Failed attempts in a row. pub attempts: u32, - /// Unix seconds of the next attempt, or None while waiting for key recovery. - pub next_attempt: Option, + /// Unix seconds of the next attempt. Meaningless while `awaiting_keys`. + pub next_attempt: i64, + /// The daemon stopped retrying because the account keys cannot open My + /// Files, and waits for a restart after key recovery. A separate field + /// rather than an optional `next_attempt`, so an older version reading + /// the file after a downgrade still understands it. + #[serde(default, skip_serializing_if = "std::ops::Not::not")] + pub awaiting_keys: bool, /// Set when the mount was refused because the folder already has files in /// it, so the app can explain that in the user's language. Absent in a /// file an older daemon wrote. @@ -83,7 +92,8 @@ mod tests { error: "fetch My Files root: missing key".into(), since: 1_700_000_000, attempts: 3, - next_attempt: Some(1_700_000_040), + next_attempt: 1_700_000_040, + awaiting_keys: false, not_empty: None, }; record(&dir, &failure).unwrap(); @@ -97,8 +107,59 @@ mod tests { fn a_failure_an_older_daemon_wrote_still_reads() { let old = r#"{"error":"missing key","since":1,"attempts":2,"next_attempt":3}"#; let failure: MountFailure = serde_json::from_str(old).unwrap(); - assert_eq!(failure.next_attempt, Some(3)); + assert_eq!(failure.next_attempt, 3); + assert!(!failure.awaiting_keys); assert_eq!(failure.not_empty, None); assert_eq!(failure.attempts, 2); } + + /// An older daemon, after a downgrade, must still read what this one + /// writes while it waits for key recovery. + #[test] + fn a_key_wait_reads_as_a_plain_failure_without_the_new_field() { + #[derive(Deserialize)] + #[allow(dead_code)] + struct OlderFailure { + error: String, + since: i64, + attempts: u32, + next_attempt: i64, + } + let failure = MountFailure { + error: "fetch My Files root: missing key".into(), + since: 1, + attempts: 1, + next_attempt: 1, + awaiting_keys: true, + not_empty: None, + }; + let json = serde_json::to_string(&failure).unwrap(); + let older: OlderFailure = serde_json::from_str(&json).unwrap(); + assert_eq!(older.next_attempt, 1); + let back: MountFailure = serde_json::from_str(&json).unwrap(); + assert!(back.awaiting_keys); + } + + /// The error a key that is not the message's recipient really produces, + /// not a hand-written payload. + #[test] + fn a_real_missing_key_failure_waits_for_keys() { + use proton_sdk::crypto::{PrivateKey, generate_node_key}; + + let unlock = |generated: proton_sdk::crypto::GeneratedNodeKey| { + PrivateKey::from_armored(&generated.locked_armored, &generated.passphrase).unwrap() + }; + let recipient = unlock(generate_node_key().unwrap()); + let stranger = unlock(generate_node_key().unwrap()); + let message = recipient.encrypt(b"root share key").unwrap(); + + let single = stranger.decrypt_armored_message(&message).unwrap_err(); + assert!(RootLookup(single.into()).needs_account_keys()); + let any = proton_sdk::crypto::decrypt_armored_with_keys(&message, &[stranger]).unwrap_err(); + assert!(RootLookup(any.into()).needs_account_keys()); + assert!( + !RootLookup(proton_sdk::ProtonError::invalid_operation("missing key")) + .needs_account_keys() + ); + } } diff --git a/crates/pdfs-fuse/Cargo.toml b/crates/pdfs-fuse/Cargo.toml index d6741ea..3e6ba00 100644 --- a/crates/pdfs-fuse/Cargo.toml +++ b/crates/pdfs-fuse/Cargo.toml @@ -39,3 +39,4 @@ parking_lot.workspace = true [dev-dependencies] tracing-subscriber.workspace = true +tokio = { workspace = true, features = ["test-util"] } diff --git a/crates/pdfs-fuse/src/systemd.rs b/crates/pdfs-fuse/src/systemd.rs index 2c7ea03..39567b5 100644 --- a/crates/pdfs-fuse/src/systemd.rs +++ b/crates/pdfs-fuse/src/systemd.rs @@ -112,17 +112,34 @@ pub fn wait_for_restart() -> std::io::Result<()> { .block_on(async { let mut terminate = signal(SignalKind::terminate())?; let mut interrupt = signal(SignalKind::interrupt())?; - let mut tick = tokio::time::interval(Duration::from_secs(10)); - loop { + let stop = async { tokio::select! { - _ = terminate.recv() => return Ok(()), - _ = interrupt.recv() => return Ok(()), - _ = tick.tick() => watchdog_ping(), + _ = terminate.recv() => {} + _ = interrupt.recv() => {} } - } + }; + park_until(stop, Duration::from_secs(10), watchdog_ping).await; + Ok(()) }) } +/// Call `ping` every `every` until `stop` completes. The parked daemon's whole +/// loop, apart from which signals end it. +async fn park_until( + stop: impl std::future::Future, + every: Duration, + mut ping: impl FnMut(), +) { + let mut tick = tokio::time::interval(every); + tokio::pin!(stop); + loop { + tokio::select! { + _ = &mut stop => return, + _ = tick.tick() => ping(), + } + } +} + /// How long a mount may take to start before the watchdog counts it as hung. /// /// Startup has no control socket to probe yet, and on a large account it is @@ -177,3 +194,26 @@ pub(crate) fn watchdog_period() -> Option { let usec: u64 = std::env::var("WATCHDOG_USEC").ok()?.parse().ok()?; (usec > 0).then(|| Duration::from_micros(usec / 2)) } + +#[cfg(test)] +mod tests { + use super::*; + + /// A parked daemon keeps the watchdog fed and returns once stopped, so the + /// restart after key recovery is a clean exit, not a kill. + #[test] + fn a_parked_daemon_pings_until_it_is_stopped() { + let runtime = tokio::runtime::Builder::new_current_thread() + .enable_time() + .start_paused(true) + .build() + .unwrap(); + let pings = std::cell::Cell::new(0); + runtime.block_on(async { + let stop = tokio::time::sleep(Duration::from_secs(35)); + park_until(stop, Duration::from_secs(10), || pings.set(pings.get() + 1)).await; + }); + // Ticks at 0, 10, 20 and 30 seconds, then the stop at 35. + assert_eq!(pings.get(), 4); + } +} diff --git a/crates/pdfs-gui/src/app/main.rs b/crates/pdfs-gui/src/app/main.rs index 207ac97..38c1a8a 100644 --- a/crates/pdfs-gui/src/app/main.rs +++ b/crates/pdfs-gui/src/app/main.rs @@ -1993,7 +1993,7 @@ pub(crate) fn service_unreachable( offer_not_empty(&ui, found); } } - Some(failure) if failure.next_attempt.is_none() => status( + Some(failure) if failure.awaiting_keys => status( &ui, "dialog-warning-symbolic", &gettext("Can't open your Drive"), diff --git a/crates/pdfs-gui/src/app/pages/status.rs b/crates/pdfs-gui/src/app/pages/status.rs index b3e54a2..6308f6b 100644 --- a/crates/pdfs-gui/src/app/pages/status.rs +++ b/crates/pdfs-gui/src/app/pages/status.rs @@ -1430,7 +1430,7 @@ pub(crate) fn paint_disconnected(ui: &Rc) { match failure { Some(failure) => SyncState::CannotOpen { not_empty: failure.not_empty.is_some(), - waiting_for_keys: failure.next_attempt.is_none(), + waiting_for_keys: failure.awaiting_keys, }, None => SyncState::Disconnected, }, diff --git a/crates/pdfs-gui/src/main.rs b/crates/pdfs-gui/src/main.rs index d62dd1e..be17d90 100644 --- a/crates/pdfs-gui/src/main.rs +++ b/crates/pdfs-gui/src/main.rs @@ -200,7 +200,7 @@ fn poll_state(socket: &Path, default_mountpoint: &Path) -> DriveState { if let Some(failure) = socket.parent().and_then(pdfs_core::mountfail::read) => { ( - if failure.next_attempt.is_none() { + if failure.awaiting_keys { gettext( "Your account keys could not open My Files. After recovering your keys, choose Retry.", )