Blog / The save that looked like a delete

The save that looked like a delete

TextEdit's safe save is a small rename dance. On a versioned drive, two of those renames could coalesce into a delete, and the document would disappear. This is the interleaving, the four guards that fix it, and what the review rounds found that the first fix missed.

The release gate caught it - twice - before any macOS build shipped, so no customer ever lost a document. It is exactly the kind of bug that gate exists for.

What a safe save actually does#

A plain write(2) over a file is not safe: a crash halfway through leaves a truncated document. Text editors avoid that by writing the new version somewhere else and renaming it into place. On macOS, TextEdit's sequence looks roughly like this:

  1. mkdir notes.txt.sb-XXXX/
  2. write the new version at notes.txt.sb-XXXX/notes.txt
  3. rename(notes.txt, notes.txt.sb-XXXX-aside)
  4. rename(notes.txt.sb-XXXX/notes.txt, notes.txt)
  5. unlink(notes.txt.sb-XXXX-aside)
  6. rmdir(notes.txt.sb-XXXX/)

At every point the user has either the old document or the new one. That is the whole point of the dance, and it is why every editor does some version of it.

Swarmfile makes saves asynchronous: a write is acknowledged locally and the engine's durable queue publishes it. Namespace operations are deferred and coalesced per entry - a rename followed milliseconds later by a delete becomes one delete row, not two. That optimisation is what turned a safe save into a lost document.

The two interleavings#

The original file is still named notes.txt on the hub. Locally, the engine has already re-anchored the coalesced delete to the aside name, because that is where the entry was when the delete was enqueued. The new version's create is in flight. The hub sees a create for a name it still holds.

There are two ways that race resolves, and the release gate hit both:

  • Collision at create. The create reaches the hub after the replace. The hub answers with the original's entry, and the name-collision path conflict-renames the new version to notes (conflict-…).txt.
  • Collision at the corrective rename. The create lands first, inside the safe-save folder. The corrective rename onto notes.txt gets a name-taken 409, is treated as final, and "puts the save back where the hub has it" - inside the folder the app is about to remove.

In both cases the not-yet-sent delete of the original eventually removes the name the new version should have had. The user saved; the document was gone.

The fix is four holds, not one#

The engine's pending_creates module now carries four guards. Each one reads as removable in isolation; each one closes an observed loss:

  1. Hold a create whose occupant this engine is still deleting. The collision handler takes the occupant id the hub answered with and holds while this engine has a queued delete or rename-away for that id. Keying on the id, rather than the name, survives coalescing.
  2. Remember where the hub last had the entry. Deferred rows carry hub_parent_id/hub_name, set at first enqueue and kept through the coalesce. The name-free lookup uses that, not the local aside name.
  3. Hold the corrective rename's name-taken 409 when the name is taken by this engine's own in-flight delete/rename. Coded 409s such as conflict_detected are not held; only a genuine name conflict is.
  4. Hold a deferred folder delete while a create has been moved out of it. Otherwise the folder's hub delete overtakes the held save and takes the new version with it.

There is also a final fallback: past the hold's bound, or once the original's delete is refused, a save whose hub place is a folder this engine is deleting is kept as a conflict copy where the user saved it - not "restored" into a folder that is about to disappear.

What the review rounds changed#

The first fix passed the release gate. An independent read-only review then found that the folder delete could still overtake the held create in deferred mode: the synchronous path waited on moved_out_in_flight, but the deferred gate only read has_pending_child. The gate script waited three seconds; the window was longer. That became guard 4.

A second review found the bound-expiry path: if the hold expired because the original's delete was stuck behind a lock, a CAS retry loop or a 429 storm, the final 409 fell through to "restore the hub place", putting the save back inside the already-deleted safe-save folder. The row then cleared, releasing the folder delete, which removed the save on the hub. The conflict-copy fallback is the fix, and it has its own test that fails when the fallback is disabled.

The honest residual: these guards are scoped to one engine. They stop this engine's own coalesced operations from losing its own save. They are not a distributed rename barrier, and a different machine racing the same name is still a conflict (refused, not silently clobbered). Under hard conflict mode the rmdir can also return EAGAIN with a .sb- folder left behind; nothing is lost, and soft mode - the default - is unaffected.

How it is pinned#

The regression tests replay the save through the real drive code and worker, not a mocked filesystem:

  • a_save_over_an_aside_then_deleted_original_waits_instead_of_conflict_renaming
  • a_corrective_rename_onto_an_own_deleted_originals_name_waits
  • a_coalesced_delete_still_frees_the_name_the_hub_has
  • a_deferred_folder_delete_waits_for_a_create_sent_into_it_and_moved_out
  • only_a_name_taken_409_is_held
  • a_name_still_taken_past_the_hold_keeps_a_conflict_copy_not_the_doomed_folder
  • a_deferred_mode_textedit_safe_save_lands_the_new_version_on_the_hub

The TextEdit sequence has its own real-mount gate (tests/integration/run-macos-write-semantics.sh) in the release preflight. The agent-write matrix has a separate real-mount lane (run-macos-conflict-mode.sh) in the conformance runner. Both are real kernel mounts, and neither substitutes for the other. That is the standard we want for a filesystem: not "the unit tests pass", but "the editor's real save sequence survives on a mounted drive".

The broader rules for saves on the drive - whole-file versions, the ESTALE refusal, and what happens when two people edit the same file - are in working with files.