Skip to content

Files(rootdir, atomic=True): atomic writes and the defaults that go with them (#213) - #226

Merged
thorwhalen merged 2 commits into
masterfrom
files-atomic-213
Dec 1, 2026
Merged

thorwhalen merged 2 commits into
masterfrom
files-atomic-213

Conversation

@thorwhalen

Copy link
Copy Markdown
Member

Closes #213. Implements item 2 of #210 (the atomic=True facade); items 1, 3 and 4 there stay open.

Most of what #213 asks for was already in dol, but it took knowing three seams: Files(write_func=atomic_write) (#201), read_retry=, strict_key_errors=. atomic_write's temp files were already hidden, and so skipped. Two applications hand-rolled the store anyway. This PR adds one keyword that assembles those seams:

s = Files(rootdir, atomic=True)                       # also TextFiles, JsonFiles, PickleFiles...
s = mk_dirs_if_missing(Files)(rootdir, atomic=True)   # with folders made on the way

What atomic=True does (on FileBytesPersister, so every file store inherits it):

  • write_func=atomic_write: a hidden temp file beside the target, then os.replace. A reader sees the old value or the new one, and a killed write leaves the old one whole.
  • read_retry=DFLT_RETRY: Windows refuses the rename while a reader holds the file open.
  • strict_key_errors=True: a failure other than a missing file propagates as itself instead of becoming a KeyError.
  • atomic_write's temp names are never keys, now also with include_hidden=True, through the walk filter (_walked_key_filter, so the Files iteration: ~100 µs/file and three walks per list(store); scandir + skip containment for walked keys (~68x) #219 fast path is kept).

Conflict rule (#210 asked for one). Explicit read_retry= / strict_key_errors= win over the facade's defaults. write_func= or write_open_kwargs= together with atomic=True raise a ValueError that points to write_func=functools.partial(atomic_write, fsync=True, ...) for other write options.

Defaults unchanged. atomic=False is the default, and nothing changes without it.

Not done here (decided, reversible):

  • Folder creation is not folded into atomic=True. It is a separate concern, already covered by mk_dirs_if_missing, whose failure cleanup also applies here; the composition is tested and documented.
  • The strict-key wrapper A local files store whose writes are atomic (temporary file, then rename) #213 mentions (no hidden names for keys that come from outside) is a separate follow-up issue. Files already refuses keys outside the root (.., absolute paths).

Tests (dol/tests/test_files_atomic.py), per the issue's "done when":

  • A child process writes through an atomic store and is SIGKILLed with half the value in the temp file. The old value is intact, the leftover temp is not a key, and the next write succeeds.
  • A reader thread reads while a writer alternates two 100 kB values 200 times: it only ever sees one or the other. With atomic=False the same test fails 3 out of 3.
  • The defaults, explicit values winning, both conflicts, strict errors (a non-missing error propagates where the default store says KeyError; a key outside the root is still refused), TextFiles and JsonFiles going through a rename, mk_dirs_if_missing composition, and temp skipping with include_hidden=True.

Full suite: 1090 passed, 11 skipped.

🤖 Generated with Claude Code

thorwhalen and others added 2 commits December 1, 2026 15:22
…ith them (#213)

The atomic=True facade of #210 item 2: each value goes through atomic_write
(hidden temp file beside the target, then os.replace), so a reader never sees a
partial value and a write killed half way leaves the old one whole. It also
defaults read_retry=DFLT_RETRY and strict_key_errors=True (explicit values win);
write_func and write_open_kwargs conflict with it (ValueError). Temp files are
never keys, now also with include_hidden=True. Folders stay with
mk_dirs_if_missing (composition tested). Default atomic=False: unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…ocstring says what is covered (#213 review)

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@thorwhalen

Copy link
Copy Markdown
Member Author

Adversarial review (independent Opus subagent): merge once the two tests are fixed for Windows. They failed only on the Windows job, which is continue-on-error: readline gets \r\n, and keys use os.sep. The feature itself held: it reaches every file store, wrap_kvs and mk_dirs_if_missing, it survives copy and deepcopy, there is no import cycle, the conflict rule is sound, the #219 fast path is kept, and the concurrency test fails 5 out of 5 without atomic. Fixed in 771853d, together with these minors: the repr keeps atomic=True; the docstring says temps are never listed, that deletes are not retried (delete_func=atomic_delete), and that text is written as UTF-8. Windows job on 771853d: 1064 passed.

@thorwhalen
thorwhalen merged commit 39c9a70 into master Dec 1, 2026
12 checks passed
@thorwhalen
thorwhalen deleted the files-atomic-213 branch December 1, 2026 15:27
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

A local files store whose writes are atomic (temporary file, then rename)

1 participant