Skip to content

Upload an edit once the file has gone quiet, not the moment it closes - #53

Merged
franzjeger merged 3 commits into
mainfrom
claude/upload-debounce
Aug 8, 2026
Merged

franzjeger merged 3 commits into
mainfrom
claude/upload-debounce

Conversation

@franzjeger

Copy link
Copy Markdown
Owner

Stacked on #52.

Uploading on close is what made the mount race itself:

#51 and #52 fix those by recovering afterwards: create the file remotely, then delete it; upload under the wrong name, then rename it. Waiting for the file to go quiet removes them at the source. By the time the upload starts, the temp file has become the real one, the deleted file is gone, and ten saves have become one.

Default 900s, configurable as upload_debounce_secs. 0 keeps the old upload-on-close behaviour.

Rewriting a file pushes its deadline out rather than queueing a second upload. unlink() drops a waiting edit instead of racing it.

The cost, and what pays for it

What the wait costs is exactly its length: an edit lives only on this machine until then. Fifteen minutes of unsent work is a real exposure, and it is the reason this PR is more than a sleep.

Shutdown flushes the queue and waits up to 60s for it to drain before unmounting. Without that, a restart would take the whole window's work with it — a worse bug than the ones being removed. Anything still unsent after the timeout is in the cache and the retry queue, and says so in the log rather than disappearing quietly.

pending_uploads over D-Bus now counts waiting edits too, not just ones queued for retry. With a 15-minute window the waiting kind is the common one, and leaving it out would show "everything synced" over work that has not left the machine.

Testing

Five tests for the queue on a paused clock — so they assert the timing without spending 15 minutes:

  • an edit waits out its delay, and is not uploaded at 899s
  • a rewrite extends the deadline instead of adding a second waiter
  • a delete cancels the wait
  • a flush releases everything at once
  • the count covers both waiting and in-flight

Plus a mount round trip proving a file written and deleted inside the quiet period never reaches OneDrive at all — no PUT, nothing queued.

One existing test changed meaning and was rewritten rather than patched: the delete-during-upload round trip now waits for the upload to genuinely start before deleting. With a debounce, that race is one the client is designed to win, so exercising the recovery path needs the window opened deliberately.

cargo fmt, cargo clippy --workspace --all-targets -- -D warnings, and ONEDRIVE_REQUIRE_FUSE=1 cargo test --workspace pass.


Generated by Claude Code

Uploading on close is what made the mount race itself. An atomic save
closes a temp file it is about to rename away, so the upload went out
under a name the file was about to stop having. A scratch file written
and deleted seconds later raced its own upload. An editor saving
repeatedly started an upload per save, and they collided with each other
on the way out — the 409 nameAlreadyExists churn.

Waiting for a file to go quiet removes all three at the source rather
than recovering from them: by the time the upload starts, the temp file
has become the real one, the deleted file is gone, and ten saves have
become one. Default 900s, configurable as upload_debounce_secs; 0 keeps
the old upload-on-close behaviour.

Rewriting a file pushes its deadline out rather than queueing a second
upload, and unlink() drops a waiting edit instead of racing it.

What the wait costs is exactly its length: an edit lives only on this
machine until then. So shutdown flushes the queue and waits up to 60s
for it to drain before unmounting — otherwise a restart would take the
window's work with it, which would be a worse bug than the ones this
removes. Anything still unsent after that is in the cache and the retry
queue, and says so in the log rather than disappearing quietly.

pending_uploads over D-Bus now counts waiting edits as well as ones
queued for retry. With a 15-minute window the waiting kind is the common
one, and leaving it out would show "everything synced" over work that has
not left the machine.

Tests: five for the queue itself on a paused clock (waits out its delay,
a rewrite extends it, a delete cancels it, a flush releases everything,
the count covers both waiting and in-flight), and a mount round trip
proving a file written and deleted inside the quiet period never reaches
OneDrive at all. The delete-during-upload round trip now waits for the
upload to actually start before deleting — with a debounce that race is
one the client is designed to win, so testing it needs the window opened
deliberately.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XfrYQTqjBaP4gDF77Ux1wZ
@franzjeger
franzjeger force-pushed the claude/upload-debounce branch from 346df7e to cd42449 Compare August 8, 2026 10:23
@franzjeger
franzjeger changed the base branch from claude/fix-rename to main August 8, 2026 10:23
claude added 2 commits August 8, 2026 10:31
CI caught what 20 local runs did not: a file created and deleted in one
breath left an entry queued for the whole debounce window.

release() is asynchronous with respect to close(), so an unlink can land
between release()'s check that the row exists and its scheduling of the
upload. The unlink cancels an entry that is not in yet; release then adds
one that nothing will remove. It never uploads — it re-reads the row
first and gives up — but it counts as an unsent edit for the whole
window, and shutdown waits for it. The one number the user is meant to
trust becomes wrong.

release() now re-checks the row after scheduling, and unlink deletes the
row before cancelling rather than after. Whichever of the two runs last
finds the state the other left, so both orderings clean up.

Confirmed with 20 consecutive runs of the test that failed in CI.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XfrYQTqjBaP4gDF77Ux1wZ
…tity

CI failed twice on this branch, each time on a different test, and both
failures were real bugs — two of them in code already released.

**A file could vanish moments after being written.** An upload adopts the
real OneDrive ID, moving the row off the temporary `_local_*` one.
`local_path` is unique so the two rows cannot both exist, which means
there is always an instant where the inode names an ID the database no
longer has — whichever order the swap is done in. A read landing there
found no row, and for a `_local_*` item with no cache file that is read
as "no content anywhere", so it dropped the entry. Reading a file you had
just created could delete it.

Reads now hold the inode lock across the lookup and adoption takes it for
writing across the whole swap, so a reader sees either the entire before
or the entire after. This one was shipped in v0.13.1.

**An upload could overwrite a rename that beat it to the row.** The task
re-read the row for the current name, then made a network call, then
wrote — and a rename landing in between was overwritten with the name the
upload had started with, leaving the saved file apparently missing until
the next delta. The re-read now happens inside the lock, immediately
before the write.

**A cache file being moved read as a cache file that was gone.** Adopting
an ID renames the cached copy, so "missing" during an upload means
"moving". Reads in that window get EAGAIN rather than having the entry
dropped underneath them.

The atomic-save test also asserted on the wrong thing. It required the
remote rename that corrects a temp-name upload — but with the upload
re-reading the row after its wait, it usually addresses the final name
directly and no correction is needed. That is the debounce working, not a
failure. It now asserts where the file ends up rather than which route it
took.

Verified with 60 consecutive runs of the full mount suite, from 8
failures in 40 before.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01XfrYQTqjBaP4gDF77Ux1wZ
@franzjeger
franzjeger merged commit f1f090c into main Aug 8, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants