Skip to content

NDC's commit mvs across filesystems, so the atomicity #142's design rests on is not the atomicity it gets #242

Closed

Description

@fdaviddpt

Code-read finding, main @ 2bb0d4f

#142 replaced NDC's blind : > now.md with a partial truncate, and its own reasoning for why the commit is safe is atomicity:

mv-over is atomic on the same filesystem

The shipped code does not put the temp file on the same filesystem. scripts/save-session.sh:602:

NDC_TAIL=$(mktemp "${TMPDIR:-/tmp}"/remember-ndc-tail-XXXXXX)
if tail -c +$(( NDC_SRC_BYTES + 1 )) "$MEMORY_FILE" > "$NDC_TAIL" 2>/dev/null; then
    NDC_KEPT=$(wc -c < "$NDC_TAIL" | tr -d ' ')
    mv "$NDC_TAIL" "$MEMORY_FILE"

$MEMORY_FILE is ${REMEMBER_DIR}/now.md. $TMPDIR is a different filesystem in ordinary configurations — /tmp is tmpfs on most Linux distributions, and external storage mode (data_dir) can put REMEMBER_DIR on a different volume from $TMPDIR on any platform. Across filesystems mv is not a rename: it is copy-then-unlink, and now.md is destroyed and rewritten in place rather than swapped.

#142's suggested fix wrote "$MEMORY_FILE.tmp" — beside the target, where the atomicity argument holds. The implementation moved it to $TMPDIR and kept the argument.

What it costs

The window is the copy, not a syscall. A machine that loses power, a SIGKILL, a full destination filesystem, or an ENOSPC partway through leaves now.md truncated mid-copy: not the old content, not the new content, a prefix of the tail. The save lock is held (#223/#224), so no concurrent writer is involved — this is single-process durability, which the lock cannot help with.

It also silently changes the file's inode, mode and — on a copy — its ownership semantics, which is why the .remember/.gitignore and permissions assumptions elsewhere are worth a look in the same pass.

Fix

mktemp "${MEMORY_FILE}.ndc-XXXXXX" (or "$(dirname "$MEMORY_FILE")"/…) so the mv is a same-directory rename and the atomicity the comment claims is the atomicity the code gets. The same applies to any other temp that is mv'd onto a memory file; the temps that are only read ($NDC_PROMPT, $HAIKU_STDERR, $TMP_PROMPT) are fine where they are and should stay there.

Worth a test that plants a destination on a distinct filesystem, or — cheaper and portable — asserts the temp path is a sibling of the target rather than asserting an mv failure mode.

Found while building the save.lock hold-time recorder for #226; not fixed there, because it deserves its own red test.

Activity

  1. fdaviddpt commented on Jul 30, 2026

    @fdaviddpt
    ContributorAuthor

    Correction to this issue's own body, recorded because it is closed and nobody will revisit it otherwise.

    The body predicts truncation — "a crash mid-copy leaves now.md truncated", a surviving prefix. That understates it. Reproduced during #245 against a real second filesystem:

    • BSD mv unlinks the partial destination, so now.md was gone entirely — total loss, not a prefix.
    • GNU mv leaves the prefix, which is the case the body describes.
    • Either way the log reported kept 5400000b appended during compression — a byte count taken off the temp file before the move, so it prints whether or not the commit landed.

    So on macOS the failure is: the memory file is destroyed, and the log says it kept 5.4 MB.

    Fixed in #245 (64cea0c) by committing through a sibling-directory rename — a real rename(2), which has no intermediate state — with the result checked and the #141 day marker moved only inside the success arm. The pattern was already in this repo three times over (lib-env-cache.sh:170-174, whose comment reads "Rename, so no reader ever parses a partial file", and post-tool-hook.sh:260); the NDC commit was the one call site that had not adopted it.

    Recording this because scoping from a closed issue's body is exactly how a fix gets sized wrong: the same mv still exists one file over in run-consolidation.sh:201 (#246), and anyone reading this issue to size that one would come away expecting a recoverable prefix.

  2. added a commit that references this issue on Jul 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions