Skip to main content

Module atomic_write

Module atomic_write 

Source
Expand description

Crash-safe file replacement and the workspace index lock.

Index binaries (content.bin, trigrams.bin) used to be opened at their final path with truncate(true) and streamed into. A crash, a killed process, or a second indexer starting mid-write left a short file behind, and the next reader failed with content.bin is too small - appears to be corrupted. Every writer now goes through this module:

  1. write to <final>.tmp in the same directory,
  2. sync_all,
  3. atomic_replace renames the temp file over the final path.

Readers therefore only ever see the previous complete file or the new complete file. Because the rename lands in the same directory, it is atomic on every platform we ship to.

IndexLock is an OS advisory lock on .reflex/index.lock held for the whole Indexer::index run. The OS releases it when the process dies, so there is no stale-PID bookkeeping.

Structs§

IndexLock
RAII guard for the workspace index lock.

Constants§

INDEX_LOCK_FILE
File name of the workspace index lock inside the cache directory.
TMP_SUFFIX
Suffix appended to a final path to build its in-progress temp path.

Functions§

atomic_replace
Rename tmp over final_path.
remove_stale_tmp
Remove leftover *.tmp files in dir (a crashed indexer leaves them).
tmp_path_for
Temp path for final_path: same directory, same file name plus .tmp.