Merge pull request #122 from narrrl/feat/protect-queued-changes

feat: protect queued changes at shutdown and sign-out (B66)
This commit is contained in:
nvrl authored and GitHub committed 2026-11-21 23:46:37 +01:00
commit 7be4c7db1e
18 files changed
+3644 -3252

No files matched your search

+102 -5
View File
@@ -158,8 +158,12 @@ enum Command {
#[arg(long)]
forget: bool,
},
/// Forget the stored session.
Logout,
/// Forget the stored session. Asks first while changes are still queued.
Logout {
/// Do not ask first.
#[arg(long)]
yes: bool,
},
/// Show account login state, and mount status if a daemon is running.
Status,
/// List every local Proton Drive location.
@@ -857,6 +861,13 @@ enum SyncCmd {
},
/// List uploads and changes not yet on Proton Drive.
Queue,
/// Wait until every queued change is on Proton Drive. Exits 0 once nothing
/// is left, 3 when what is left needs you (see `sync issues`), 4 on timeout.
Flush {
/// Give up after this many seconds.
#[arg(long)]
timeout: Option<u64>,
},
/// List queued changes Proton Drive refused or that keep failing, and what to do.
Issues,
/// Save the content of a queued upload (by id from `sync queue`) to a file.
@@ -1047,7 +1058,7 @@ fn main() -> Result<()> {
cli.command,
Command::Login { .. }
| Command::Unlock { .. }
| Command::Logout
| Command::Logout { .. }
| Command::Mount { .. }
| Command::Daemon { .. }
) {
@@ -1057,7 +1068,7 @@ fn main() -> Result<()> {
Command::Login { username, remember } => cmd_login(username, remember),
Command::Unlock { remember } => cmd_unlock(remember),
Command::Lock { forget } => cmd_lock(forget),
Command::Logout => cmd_logout(),
Command::Logout { yes } => cmd_logout(yes),
Command::Status => cmd_status(),
Command::Locations => cmd_locations(),
Command::Mount { mountpoint } => cmd_mount(mountpoint),
@@ -1362,6 +1373,7 @@ fn cmd_sync(action: SyncCmd) -> Result<()> {
})?)?
}
SyncCmd::Queue => return cmd_sync_queue(),
SyncCmd::Flush { timeout } => return cmd_sync_flush(timeout),
SyncCmd::Issues => return cmd_sync_issues(),
SyncCmd::Export { id, dest } => return cmd_sync_export(id, &dest),
SyncCmd::Limit { up, down } => return cmd_sync_limit(up, down),
@@ -1465,6 +1477,71 @@ fn cmd_sync_queue() -> Result<()> {
Ok(())
}
/// `pdfs sync flush`: wait for the queue to drain.
///
/// Failed changes are retried at once rather than after their backoff. The
/// wait ends early, with exit code 3, when everything left is waiting on the
/// user: refused by Drive, failing again and again, or a temporary file
/// waiting for its final name. Paused or offline, it keeps waiting and says
/// why.
fn cmd_sync_flush(timeout: Option<u64>) -> Result<()> {
const POLL: std::time::Duration = std::time::Duration::from_secs(1);
let deadline =
timeout.map(|secs| std::time::Instant::now() + std::time::Duration::from_secs(secs));
ok_or_bail(control_request(CtlRequest::RetryPendingOp { id: None })?)?;
let mut last_line = String::new();
loop {
let items = match control_request(CtlRequest::ListPendingOps)? {
CtlResponse::PendingOps { items } => items,
CtlResponse::Error { message, kind } => bail!("{}", cli_error(kind, &message)),
other => bail!("unexpected response: {other:?}"),
};
let waiting = items
.iter()
.filter(|op| !op.parked && !op.failing && op.issue.is_none())
.count();
if items.is_empty() {
finish_line(&last_line);
println!("Everything is on Proton Drive.");
return Ok(());
}
if waiting == 0 {
finish_line(&last_line);
eprintln!(
"{} queued change(s) need you before they can be sent. See `pdfs sync issues`.",
items.len()
);
std::process::exit(3);
}
let reason = match control_request(CtlRequest::Status)? {
CtlResponse::Status { paused: true, .. } => " (sync is paused; `pdfs sync resume`)",
CtlResponse::Status { online: false, .. } => " (offline; waiting for Proton Drive)",
_ => "",
};
let line = format!("{waiting} change(s) left to send{reason}");
if line != last_line {
if !last_line.is_empty() {
print!("\r\x1b[2K");
}
print!("{line}");
last_line = line;
}
if deadline.is_some_and(|deadline| std::time::Instant::now() >= deadline) {
finish_line(&last_line);
eprintln!("Timed out with {waiting} change(s) still queued.");
std::process::exit(4);
}
std::thread::sleep(POLL);
}
}
/// End a progress line so the next message starts on its own.
fn finish_line(last_line: &str) {
if !last_line.is_empty() {
println!();
}
}
/// `pdfs sync issues`: the queued changes that need the user, and what to do.
fn cmd_sync_issues() -> Result<()> {
let response = control_request(CtlRequest::ListPendingOps)?;
@@ -2298,7 +2375,27 @@ fn key_state_name() -> &'static str {
}
}
fn cmd_logout() -> Result<()> {
fn cmd_logout(yes: bool) -> Result<()> {
// Queued changes stay staged on this computer and are sent after the next
// sign-in, but until then they exist nowhere else. Without a running
// daemon there is no count to show.
if !yes
&& let Ok(CtlResponse::Status {
pending_uploads,
pending_changes,
parked_uploads,
..
}) = control_request(CtlRequest::Status)
{
let queued = pending_uploads + pending_changes + parked_uploads;
if queued > 0 {
println!(
"{queued} change(s) are not on Proton Drive yet. They stay on this computer and are \
sent after you sign in again. Run `pdfs sync flush` first to send them now."
);
confirm("Sign out anyway?")?;
}
}
if auth::logout()? {
println!("Signed out: the session is ended on Proton and cleared here.");
} else {
+129
View File
@@ -0,0 +1,129 @@
//! Hold off a shutdown while queued changes exist only on this computer.
//!
//! A change made through the mount is staged here and sent by the drain. Until
//! it lands, this computer has the only copy, so powering off then risks it
//! more than the user expects (`docs/BUGS.md` B66). While the queue holds work
//! the drain can still send, the daemon takes a logind shutdown inhibitor
//! through `systemd-inhibit`, which desktops show in their power-off dialog
//! with the reason. It is let go as soon as the queue empties.
//!
//! Work that cannot drain on its own — a temporary file waiting for its final
//! name, or a change Drive refused or that keeps failing — never holds the
//! lock, or a shutdown would be held up for something that is not coming.
//! Without `systemd-inhibit` nothing happens.
use std::process::{Child, Command, Stdio};
use std::time::Duration;
use pdfs_core::db::PendingCounts;
use tracing::{debug, warn};
use super::Core;
/// How often the queue is looked at.
const CHECK_EVERY: Duration = Duration::from_secs(5);
/// Changes the drain can still send by itself.
fn sendable(counts: &PendingCounts) -> i64 {
(counts.uploads + counts.changes - counts.parked - counts.failing).max(0)
}
/// Run until the daemon stops, holding the inhibitor while changes wait.
pub(crate) fn run(core: Core) {
let mut held: Option<Child> = None;
loop {
let waiting = core
.db
.pending_op_counts()
.map(|counts| sendable(&counts))
.unwrap_or(0);
// The helper can go away (logind restarted, or it was killed); the
// lock is then taken again on the next round.
if let Some(child) = &mut held
&& !matches!(child.try_wait(), Ok(None))
{
held = None;
}
match (&mut held, waiting > 0) {
(None, true) => held = take(waiting),
(Some(child), false) => {
release(child);
held = None;
}
_ => {}
}
if !core.shutdown.sleep(CHECK_EVERY) {
break;
}
}
if let Some(mut child) = held {
release(&mut child);
}
}
fn take(waiting: i64) -> Option<Child> {
let why = if waiting == 1 {
"Sending 1 change to Proton Drive".to_string()
} else {
format!("Sending {waiting} changes to Proton Drive")
};
match Command::new("systemd-inhibit")
.args([
"--what=shutdown",
"--who=Proton Drive",
&format!("--why={why}"),
"--mode=block",
"sleep",
"infinity",
])
.stdin(Stdio::null())
.stdout(Stdio::null())
.stderr(Stdio::null())
.spawn()
{
Ok(child) => {
debug!(waiting, "holding off shutdown while changes are queued");
Some(child)
}
Err(error) if error.kind() == std::io::ErrorKind::NotFound => None,
Err(error) => {
warn!(%error, "taking the shutdown inhibitor failed");
None
}
}
}
fn release(child: &mut Child) {
let _ = child.kill();
let _ = child.wait();
debug!("queue drained; shutdown no longer held off");
}
#[cfg(test)]
mod tests {
use super::*;
fn counts(uploads: i64, changes: i64, parked: i64, failing: i64) -> PendingCounts {
PendingCounts {
uploads,
changes,
parked,
failing,
last_error: None,
}
}
#[test]
fn only_work_the_drain_can_send_holds_off_a_shutdown() {
assert_eq!(sendable(&counts(0, 0, 0, 0)), 0);
assert_eq!(sendable(&counts(2, 1, 0, 0)), 3);
assert_eq!(sendable(&counts(1, 0, 1, 0)), 0);
assert_eq!(sendable(&counts(2, 0, 0, 2)), 0);
assert_eq!(sendable(&counts(3, 2, 1, 1)), 3);
}
#[test]
fn counts_that_overlap_never_go_negative() {
assert_eq!(sendable(&counts(1, 0, 1, 1)), 0);
}
}
+1
View File
@@ -92,6 +92,7 @@ use drive::{DriveApi, RevisionRead};
mod events;
mod filesystem;
mod gallery;
mod inhibit;
mod keeper;
mod link;
pub use filesystem::ProtonFs;
+11
View File
@@ -793,6 +793,17 @@ pub(crate) fn mount_with(
warn!(%error, "start My Files share-id repair failed");
}
}
// Hold off a shutdown while changes exist only here (B66). Only the real
// daemon: a simulation's queue is not the user's data.
if matches!(host, Host::Daemon) {
let inhibit_core = core.clone();
if let Err(error) = std::thread::Builder::new()
.name("pdfs-inhibit".into())
.spawn(move || crate::inhibit::run(inhibit_core))
{
warn!(%error, "start the shutdown inhibitor failed");
}
}
// Stop signals (SIGTERM from `systemctl --user stop`, SIGINT from Ctrl-C)
// are delivered onto the async runtime; bridge them onto a sync channel so
+33 -3
View File
@@ -675,15 +675,45 @@ fn verify_in_browser(
/// Sign out, after a confirmation: disable+stop the mount service (so the
/// daemon isn't left running without credentials), forget the stored session,
/// and drop back to the login page.
///
/// Changes still queued are named first: they stay staged here and are sent
/// after the next sign-in, but until then they exist nowhere else.
pub(crate) fn sign_out(ui: &Rc<Ui>) {
let rx = spawn_request(ui.dirs.control_socket(), Request::Status);
let ui = ui.clone();
glib::spawn_future_local(async move {
let queued = match rx.recv().await {
Ok(Ok(Response::Status {
pending_uploads,
pending_changes,
parked_uploads,
..
})) => pending_uploads + pending_changes + parked_uploads,
_ => 0,
};
confirm_sign_out(&ui, queued);
});
}
fn confirm_sign_out(ui: &Rc<Ui>, queued: u64) {
let Some(window) = ui_window(ui) else { return };
let ui = ui.clone();
let mut body = gettext(
"Proton Drive will disconnect and stop syncing until you sign in again. Files already on this computer stay where they are.",
);
if queued > 0 {
let warning = ngettext_f(
"{n} change is not on Proton Drive yet. It stays on this computer and is sent after you sign in again.",
"{n} changes are not on Proton Drive yet. They stay on this computer and are sent after you sign in again.",
queued,
&[],
);
body = format!("{warning}\n\n{body}");
}
confirm_destructive(
&window,
&gettext("Sign Out?"),
&gettext(
"Proton Drive will disconnect and stop syncing until you sign in again. Files already on this computer stay where they are.",
),
&body,
&gettext("Sign Out"),
move || {
let galleries = ui.drive_galleries.borrow().upgrade();
+15 -2
View File
@@ -74,7 +74,6 @@ original wording.
| [B134](#b134--a-file-deleted-online-while-the-drain-checks-its-write-is-kept-as-a-conflict-copy) | A file deleted online while the drain checks its write is kept as a conflict copy | Only with `local_first` off |
| [B91](#b91--the-daemon-hung-overnight-and-nothing-could-say-what-it-was-waiting-on) | The daemon hung overnight, and nothing could say what it was waiting on | The cause is not diagnosed; the daemon now reports where it is stuck and is restarted |
| [B67](#b67--fresh-state-restore-can-silently-select-a-new-device-high-22) | Fresh-state restore can silently select a new device (HIGH-22) | An ambiguous device after a loss of local state is not asked about |
| [B66](#b66--local-only-pending-data-is-not-protected-at-shutdown-high-21) | Local-only pending data is not protected at shutdown (HIGH-21) | No shutdown protection and no `pdfs sync flush` |
| [B65](#b65--release-artifacts-lack-supply-chain-verification-med-11) | Release artifacts lack supply-chain verification (MED-11) | No SBOM, provenance or checksums |
| [B15](#b15--an-empty-but-listed-folder-and-duplicated-folder-uids) | An empty-but-`listed` folder, and duplicated folder uids | Cause not attributed |
| [B10](#b10--glib-critical-when-the-launcher-closes-over-an-in-flight-open) | GLib critical when the launcher closes over an in-flight open | |
@@ -109,6 +108,7 @@ a live account since.
| Entry | Title |
|---|---|
| [B66](#b66--local-only-pending-data-is-not-protected-at-shutdown-high-21) | Local-only pending data is not protected at shutdown (HIGH-21) |
| [B291](#b291--on-fedora-45s-oo7-every-saved-sign-in-is-lost-at-the-next-reboot) | On Fedora 45's oo7, every saved sign-in is lost at the next reboot |
| [B289](#b289--the-takeout-import-mixes-in-other-services-and-drops-location-and-live-photos) | The Takeout import mixes in other services and drops location and live photos |
| [B288](#b288--an-interrupted-takeout-import-reads-as-finished-and-one-error-ends-it) | An interrupted Takeout import reads as finished, and one error ends it |
@@ -7475,7 +7475,7 @@ duplicates, renamed folders, multiple devices, and byte-identical restoration.
### B66 — Local-only pending data is not protected at shutdown (HIGH-21)
**Status:** Open — 1.0 data-safety/UX blocker
**Status:** Fixed, unverified live
**Found:** 2026-07-22, 1.0 recovery audit
**Where:** shutdown, CLI/GUI status, logout/unregister workflows
@@ -7488,6 +7488,19 @@ and last errors, and protect destructive lifecycle actions. Test offline stop,
logout, unregister, reboot, retry exhaustion, and drain. Credential removal must
never delete staged bytes.
**Fix:** `crates/pdfs-fuse/src/inhibit.rs` takes a logind shutdown inhibitor
(`systemd-inhibit --what=shutdown --mode=block`) while the queue holds changes
the drain can send, and lets it go once they land. Parked transient files and
changes that need the user are left out, so they never hold a power-off up for
good. `pdfs sync flush` retries failed changes and waits for the queue, with
its own exit codes for "needs you" and timeout. `pdfs logout` and the app's
Sign Out dialog say how many changes are still queued and that they stay
staged. Credential removal never touched `staging/`.
**Verified:** unit tests for which work holds the lock; `systemd-inhibit` in
block mode taken by a user process on 2026-11-21. A power-off with a queued
upload is still owed.
---
### B65 — Release artifacts lack supply-chain verification (MED-11)
+10
View File
@@ -11,6 +11,16 @@ scratch (user data in `staging/` and `recovery/` is never touched by this).
## [Unreleased]
### Added
- `pdfs sync flush` waits until every queued change is on Proton Drive, retrying
failed ones at once. It exits 3 when what is left needs you and 4 on `--timeout`.
- While changes are queued that only this computer has, the daemon holds off a
shutdown through logind, and desktops show "Sending N changes to Proton Drive" in
their power-off dialog. Changes waiting on you never hold it up (B66).
- Signing out, in the app or with `pdfs logout`, says how many changes are not on
Proton Drive yet. They stay on this computer and are sent after the next sign-in.
### Changed
- A Google Photos import uploads two photos at once instead of one, while the next
+2 -1
View File
@@ -70,7 +70,7 @@ The script runs `pdfs` from `PATH` on every Tab, so it keeps working after an up
| `pdfs login [-u EMAIL] [--remember]` | Sign in (password, then a 2FA code if the account has one) and store the session in the system keyring. Restarts the service if it is running. `--remember` keeps a two-password account unlocked across logins. |
| `pdfs unlock [--remember]` | Enter the mailbox password of a two-password account, so the service can open your files. Needed once per login session, or once with `--remember`. |
| `pdfs lock` | Forget the unlock held in memory; the account is locked again. |
| `pdfs logout` | End the session on Proton and remove the stored session. |
| `pdfs logout [--yes]` | End the session on Proton and remove the stored session. Asks first while changes are still queued; they stay on this computer and are sent after the next sign-in. |
| `pdfs status` | Account, mount, queue and cache state. |
| `pdfs quota` | Storage used across all Proton products. |
| `pdfs locations` | Every local place the service occupies: the Proton Drive folder and each synced folder, with mode and state. |
@@ -125,6 +125,7 @@ commands are for scripts and for cases where the mount is not available.
| `pdfs sync ignore check PATH` | Say whether a file in a synced folder is synced, and which pattern decides. |
| `pdfs sync limit [--up RATE] [--down RATE]` | Cap bandwidth, for example `500K` or `2M` per second. `0` removes a cap. Without options, prints the current caps. |
| `pdfs sync queue` | List changes that have not reached Proton Drive yet. |
| `pdfs sync flush [--timeout SECS]` | Retry failed changes now and wait until the queue is empty. Exits 0 when everything is on Proton Drive, 3 when what is left needs you (see `sync issues`), 4 on timeout. Says so while sync is paused or offline. |
| `pdfs sync issues` | List queued changes Proton Drive refused or that keep failing, and what to do. |
| `pdfs sync export ID DEST` | Save a copy of a queued upload's content. `DEST` may be a folder; an existing file is never overwritten. The upload stays queued. |
| `pdfs sync discard ID [--yes]` | Drop a queued change and undo it here: a new file or folder goes, anything else goes back to what Proton Drive has. Content not exported first is lost. |
+1 -1
View File
@@ -43,7 +43,7 @@ them along.
| Item | State | Notes |
|---|---|---|
| Protect queued changes at shutdown | Not started | A systemd inhibitor lock while `staging/` holds unsent data, a warning when signing out, and a `pdfs sync flush` that waits for the queue. Today the queue is visible in `pdfs sync queue`, the tray and **Sync → Overview**, but nothing stops a shutdown (B66) |
| Protect queued changes at shutdown | Done, live check owed | The daemon holds a logind shutdown inhibitor while the queue holds changes it can send, signing out warns with the count, and `pdfs sync flush` waits for the queue (B66) |
| Restore pins from the machine profile | Not started | `profile.json` records pins; `pdfs sync restore` does not apply them yet |
| Profile backup health | Not started | A failed profile upload is a `WARN` in the log and nothing in the app (B68) |
| Rotated credentials survive a keyring failure | Open | Report and retry keyring write failures, so a rotated single-use refresh token is not lost (B61) |
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff
+372 -360
View File
File diff suppressed because it is too large. Load diff
+371 -360
View File
File diff suppressed because it is too large. Load diff