Skip to main content

Module write_lock

Module write_lock 

Source
Expand description

Cross-process write serialisation per mem.

Every mutation runs reload-before-operation, compares the caller’s expected_hash, then commits. Those three steps must be one atomic unit across processes: if a sibling process commits between this process’s reload and its commit, the compare runs against a snapshot that is already behind, and a stale write lands over the sibling’s (a lost update reported as success). The guard below holds an exclusive OS file lock on the mem’s MemBackend::write_lock_path for the whole unit, so a second writer blocks until the first has committed and then reloads, sees the new content, and gets its honest HASH_MISMATCH.

The lock is reentrant per thread ([crate::storage::path_lock]): a mutation that calls another mutation entry point (a batch over single-item paths, a merge over creates) takes the same lock again without blocking on itself. Two threads, or two engines on two threads, still exclude each other.

The guard is also the one exit every mutation shares for its staged writes. A mutation stages bytes into the backend and commits them later; an error between the two (a refused sidecar write, a failed commit, a ? in any verb) used to leave the staged bytes in the backend, where the next read served them as content that never landed. When the outermost guard of a thread drops, it discards whatever is still staged on the mems it locked. After a commit nothing is staged and the discard is a no-op, so a call site has no failure path to remember: holding the lock is the cleanup.

Structs§

MemWriteGuard
Held for the duration of one mutation; dropping it discards the writes still staged on the mems it covers and releases every lock it took (both when the last nested holder drops).