File replacement and open readers / 文件替换与旧句柄 / ファイル置換と開いた読み手
Recorded: 2026-11-01

Run / 运行 / 実行
  python3 file-replace-reader-lab.py > rerun.json
  Run without arguments. --reader is an internal child-process mode.
  Python standard library only; POSIX environment required.
  Recorded environment: macOS 27.0.1, arm64, CPython 3.12.14.
  The script creates and cleans its own temporary tree, starts three reader
  processes and prints a JSON report. It does not connect to a network, change
  project/server business files, or signal a service. The shell redirect writes
  rerun.json. No fsync, crash, power-loss or storage-failure injection is used.

Method / 方法
  Writer and independent reader use pipe acknowledgements to choose observation
  points. No sleeps or timing races select a result. Each reader holds one old
  unbuffered binary handle, seeks to offset zero at every observation, and also
  opens the current pathname anew. Identity comparisons use (st_dev, st_ino).
  The report records equality, not volatile inode numbers, process IDs or paths.

Fixtures
  Old: b'old-complete\n', 13 bytes.
  New: b'new-complete\n', 13 bytes.
  Prefix: b'new-', 4 bytes.
  Retained old-writer edit: b'old-modified\n', 13 bytes.

Recorded observations (13 across three reader processes)
  In-place (4): old -> empty after truncate -> prefix -> complete new.
    Both held and fresh reads follow the same object; identity stays unchanged.
  Replace (6): old -> candidate prefix -> candidate complete -> replace ->
    write through retained old writer -> reader reopens.
    The public name returns old content while the candidate is prepared.
    Immediately after replace: held=old, fresh=new; identities differ.
    Old handle link count is 0. Candidate identity becomes target identity.
    Candidate default mode 0600 becomes target mode, replacing old mode 0644.
    Editing old object after replace changes held content to old-modified,
    while fresh still reads new. A held handle is not an immutable snapshot.
    After reopening, held and fresh both read new and identities match.
  Delete then rename (3): old -> name missing -> name restored.
    During gap, fresh open raises FileNotFoundError while held still reads old.

Additional boundaries
  publish_once success: full new bytes, explicit 0644, candidate name gone.
  Missing replace source: FileNotFoundError; old bytes and identity retained.
  Deliberate RuntimeError before replace: partial candidate removed; old retained.
    This is a controlled Python exception, not a crash or a killed process.
  publish_once file-to-nonempty-directory error: IsADirectoryError; marker
    inside directory preserved, helper candidate cleaned.
  All assertions passed in the recorded run.

Scope
  Same local temporary filesystem; controlled directory; one writer process.
  No competing publication, Windows, network filesystem, mmap, application
  configuration cache, service reload, ownership, ACL, or extended-attribute test.
  No free-disk-space measurement or claim of physical block reclamation.
  publish_once illustrates single-file visibility, with explicit mode selection;
  it is not a multi-file transaction or a power-loss-safe storage implementation.

First-party documentation read for the article
  https://docs.python.org/3.12/library/functions.html#open
  https://docs.python.org/3.12/library/os.html#os.replace
  https://docs.python.org/3.12/library/os.html#os.stat_result
  https://docs.python.org/3.12/library/tempfile.html#tempfile.mkstemp
  https://man7.org/linux/man-pages/man2/rename.2.html
  https://man7.org/linux/man-pages/man2/open.2.html
  https://man7.org/linux/man-pages/man2/fsync.2.html
  https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/unlink.2.html
  Apple URL is explicitly archived documentation; Linux manuals are explanatory
  sources, not evidence that this script was run on Linux.

ZIP contains this README, the original script, and the recorded JSON report.
No external documentation text or third-party code is bundled.
