Skip to main content

Module atomic_write

Module atomic_write 

Source
Expand description

atomic_write submodule — the shared temp-file-safe shard writer used by both rkyv caches. Crash-safe atomic file replacement for the rkyv shard caches.

zshrs-original — no C counterpart. zsh has no persistent bytecode store, so nothing here mirrors a Src/ function.

Both shard caches replace their file the same way: serialize into a sibling temp file, fsync, rename over the target. The temp name embeds the writing pid (<file>.tmp.<pid>.<nanos>), which is what lets a later writer decide whether an abandoned temp is safe to delete.

Abandonment was not hypothetical. One ~/.zshrs accumulated 18 orphaned autoloads.rkyv.tmp.<pid>.<nanos> files totalling 517 MB. Two holes produced them, and both are closed here:

  • an error return between File::create and rename left the temp on disk — [TempFileGuard] now unlinks it on every exit path, including the ? returns and a panic;
  • a process killed inside that window never reached the rename at all — [reap_orphan_temps] unlinks temps whose recorded pid no longer exists.

A temp owned by a live pid is never touched: it belongs to a sibling shell that is still mid-write, and deleting it would corrupt that write. Same reasoning as the .git/index.lock rule — a lock (or a temp) you did not create is not yours to remove.

Functions§

reap_orphan_temps
Delete <file>.tmp.<pid>.<nanos> siblings of path whose <pid> is no longer running. Returns how many were removed.
write_bytes_atomic
Replace path with bytes atomically, leaving no temp file behind on any exit path, and reap temps abandoned by processes that are gone.