Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions crates/daemon/src/dbus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ pub struct OneDriveInterface {
pub needs_auth: Arc<std::sync::atomic::AtomicBool>,
pub progress: ProgressText,
pub quota: crate::quota::QuotaCache,
/// Edits waiting out their quiet period in the FUSE layer. `None` when not
/// running on-demand, where there is no such queue.
pub pending_uploads: Option<Arc<vfs::PendingUploads>>,
}

#[interface(name = "com.onedrive.linux1")]
Expand Down Expand Up @@ -92,10 +95,19 @@ impl OneDriveInterface {
Ok(self.progress.lock().clone())
}

/// How many uploads are queued for retry. Non-zero means edits exist that
/// have not reached OneDrive yet.
/// How many edits have not reached OneDrive yet.
///
/// Both kinds count: uploads queued for retry after a failure, and edits
/// still waiting out their quiet period. With a debounce of any length the
/// second kind is the common one, and leaving it out would show "everything
/// synced" over work that is still only on this machine.
async fn pending_uploads(&self) -> zbus::fdo::Result<u32> {
Ok(self.engine.pending_uploads().await as u32)
let queued = self.engine.pending_uploads().await as u32;
let waiting = match &self.pending_uploads {
Some(pending) => pending.count().await as u32,
None => 0,
};
Ok(queued + waiting)
}

/// Bytes the on-demand cache is currently using.
Expand Down Expand Up @@ -200,6 +212,7 @@ pub async fn export_dbus(
recent: RecentBuffer,
needs_auth: Arc<std::sync::atomic::AtomicBool>,
progress: ProgressText,
pending_uploads: Option<Arc<vfs::PendingUploads>>,
) -> anyhow::Result<Connection> {
// The interface reaches Graph only through this cache.
let quota = crate::quota::QuotaCache::new(graph);
Expand All @@ -213,6 +226,7 @@ pub async fn export_dbus(
needs_auth,
progress,
quota,
pending_uploads,
},
)
.await?;
Expand Down
39 changes: 39 additions & 0 deletions crates/daemon/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ use sync_engine::{Config, Database, SyncEngine};
use tracing::{error, info};
use tracing_subscriber::EnvFilter;

/// How long shutdown waits for queued edits to reach OneDrive. Long enough for
/// a slow link to finish a reasonable file, short enough not to hang a reboot.
const FLUSH_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(60);

/// The release this binary was built from — see the same constant in odctl.
const VERSION: &str = match option_env!("ONEDRIVE_RELEASE_VERSION") {
Some(v) => v,
Expand Down Expand Up @@ -129,6 +133,9 @@ async fn main() -> Result<()> {
.context("start sync engine")?;

// ── FUSE VFS ───────────────────────────────────────────────────────────────
// Taken before the session consumes the filesystem, so shutdown can flush
// whatever is still waiting out its debounce.
let mut pending_uploads = None;
let mount_handle = if config.on_demand {
let mountpoint = config.sync_dir.clone();
vfs::prepare_mountpoint(&mountpoint).context("prepare mountpoint")?;
Expand All @@ -139,9 +146,17 @@ async fn main() -> Result<()> {
mountpoint.clone(),
cache_dir(),
config.excluded_patterns.clone(),
std::time::Duration::from_secs(config.upload_debounce_secs),
)
.await
.context("create FUSE filesystem")?;
pending_uploads = Some(fs.pending_uploads());
if config.upload_debounce_secs > 0 {
info!(
"Edits upload after {}s of quiet — flushed on shutdown",
config.upload_debounce_secs
);
}

let mount_options = MountOptions::default();
let handle = Session::new(mount_options)
Expand Down Expand Up @@ -216,6 +231,7 @@ async fn main() -> Result<()> {
Arc::clone(&recent),
Arc::clone(&needs_auth),
Arc::clone(&progress),
pending_uploads.clone(),
)
.await
{
Expand Down Expand Up @@ -247,6 +263,29 @@ async fn main() -> Result<()> {
}

// ── Graceful shutdown ──────────────────────────────────────────────────────
// Before anything else: an edit still waiting out its debounce exists only
// on this machine. Shutting down without sending it would make the wait a
// way to lose work rather than a way to avoid races.
if let Some(pending) = &pending_uploads {
let waiting = pending.count().await;
if waiting > 0 {
info!("Sending {waiting} edit(s) still waiting to upload…");
pending.flush_now().await;
if pending.wait_idle(FLUSH_TIMEOUT).await {
info!("All edits uploaded");
} else {
// Not lost: they are in the cache and the queue, and the next
// start picks them up. Worth saying out loud all the same.
error!(
"Gave up waiting after {}s with {} edit(s) unsent — they are \
still cached and will upload on next start",
FLUSH_TIMEOUT.as_secs(),
pending.count().await
);
}
}
}

if let Some(handle) = mount_handle {
info!("Unmounting FUSE...");
if let Err(e) = handle.unmount().await {
Expand Down
19 changes: 19 additions & 0 deletions crates/sync-engine/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,18 @@ fn default_max_cache_size_gb() -> f64 {
10.0
}

/// How long a file must go untouched before its edit is uploaded.
///
/// Uploading the instant a file closes made the mount race itself: an atomic
/// save uploads a temp file it is about to rename away, a scratch file races
/// its own delete, and an editor saving repeatedly starts an upload per save.
/// Waiting for quiet removes all of that — at the cost of the edit living only
/// on this machine until the wait is up, which is why the daemon flushes the
/// queue before it shuts down.
fn default_upload_debounce_secs() -> u64 {
900
}

fn default_auth_method() -> String {
"auto".into()
}
Expand Down Expand Up @@ -102,6 +114,12 @@ pub struct Config {
#[serde(default = "default_max_cache_size_gb")]
pub max_cache_size_gb: f64,

/// Seconds a file must go untouched before its edit is uploaded. `0`
/// uploads as soon as the file is closed, which is what the client used to
/// do — see [`default_upload_debounce_secs`] for why that was a problem.
#[serde(default = "default_upload_debounce_secs")]
pub upload_debounce_secs: u64,

/// How to sign in: "auto" (browser when a desktop session is present),
/// "browser" (authorization code + PKCE — required when Conditional
/// Access blocks the device code flow), or "device_code".
Expand Down Expand Up @@ -159,6 +177,7 @@ impl Config {
max_download_threads: 4,
delta_poll_interval_secs: 30,
max_cache_size_gb: default_max_cache_size_gb(),
upload_debounce_secs: default_upload_debounce_secs(),
auth_method: default_auth_method(),
};
cfg.save()
Expand Down
1 change: 1 addition & 0 deletions crates/sync-engine/src/engine/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,7 @@ mod delta_tests {
max_download_threads: 1,
delta_poll_interval_secs: 30,
max_cache_size_gb: 10.0,
upload_debounce_secs: 0,
auth_method: "device_code".into(),
});
let dir = tempfile::tempdir().unwrap();
Expand Down
2 changes: 2 additions & 0 deletions crates/vfs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ bytes = "1"
chrono.workspace = true

[dev-dependencies]
# start_paused: the debounce tests must not actually wait 15 minutes.
tokio = { workspace = true, features = ["test-util"] }
graph-client = { path = "../graph-client", features = ["test-util"] }
wiremock = "0.6"
tempfile = "3"
Expand Down
Loading
Loading