Skip to content

locked_update(store, key, change, *, lock): update_json's step for any store (#186) - #227

Merged
thorwhalen merged 2 commits into
masterfrom
locked-update-186
Dec 1, 2026
Merged

thorwhalen merged 2 commits into
masterfrom
locked-update-186

Conversation

@thorwhalen

Copy link
Copy Markdown
Member

Closes #186.

Why now. #186 waited for a second store type whose callers repeat the locked read-modify-write. One exists: a JSON-per-key MutableMapping with atomic writes and a per-key locked(key) context, whose callers repeat "lock, read strictly, change, write" at four sites, two of which fit this function exactly. Another application repeats update_json's single-file case by hand, once per user file.

What. locked_update(store, key, change, *, lock, default=dict, snapshot=pickle.dumps, warn_unlocked=True), exported from dol. It is update_json's step over one key of any read-write mapping:

  • it holds lock around the read and the write; whether the lock is held is judged exactly as update_json judges it (shared _is_held), with the same warning when it is not;
  • the read is strict: only a KeyError starts from default(), and any other error propagates, so nothing is written over a value that could not be read. The docstring points to Files(atomic=True) / strict_key_errors=True, because a plain Files reports a permission error as a KeyError;
  • default=None means the key must exist: a missing key raises KeyError, and change is not called. The issue's comment left this choice open: store-level code speaks KeyError, while update_json keeps its FileNotFoundError;
  • the value is written back only when snapshot of it changed. pickle.dumps is the default because == would miss 1 -> True; pass the store's own encoder to compare exactly what is written. A no-op creates nothing;
  • change's return value is handed back and never written.

Decided here (reversible): lock is required. No default fits every store: a dol file store does not expose its key → path mapping through wrapper layers, and a lock only this process sees would lose updates silently across processes. It is a context manager, as in update_json (lock=file_lock(path), lock=store.locked(key), lock=threading.Lock()).

Not done: rewriting update_json on top of locked_update. Its file-specific checks (folder exists, an early exit before the sidecar, the JSON type check, InvalidJSONError) would stay anyway; the shared part is the held test.

Tests (dol/tests/test_locked_update.py):

  • the read and the write both happen inside the lock;
  • a missing key starts from the default, and a no-op writes nothing;
  • default=None refuses a missing key without calling change;
  • a failing change writes nothing;
  • 1 -> True counts as a change;
  • a non-KeyError read error propagates, with nothing written;
  • the unlocked warning, and its opt-out;
  • 8 threads × 25 increments through file_lock on a JsonFiles store end at 200;
  • control: the same race without a lock loses updates, so the test above can fail.

Full suite: 1090 passed, 11 skipped.

🤖 Generated with Claude Code

thorwhalen and others added 2 commits December 1, 2026 15:27
…y store (#186)

The locked read-modify-write of update_json, over one key of any read-write
mapping: hold `lock`, read strictly (only a KeyError starts from `default()`;
default=None means the key must exist), let `change` mutate the value in place,
write it back only when `snapshot` of it changed (pickle.dumps by default, so
1 -> True is a change). `lock` is required: no lock fits every store, and an
in-process default would lose updates silently. update_json and locked_update
share the held test (_is_held).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…e that failed to decode, honest docstring (#186 review)

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

Copy link
Copy Markdown
Member Author

Adversarial review (independent Opus subagent) found 3 majors, all fixed in bc88eaf:

  1. The docstring advised Files(rootdir, atomic=True), which was not on master yet. Files(rootdir, atomic=True): atomic writes and the defaults that go with them (#213) #226 has since landed it, and this branch is rebased onto it; the advice is now accurate, atomic=True includes strict_key_errors=True, and a smoke test passes.
  2. An immutable value (str, bytes, number, tuple) made change a silent no-op. It now raises TypeError, as update_json does.
  3. A KeyError raised by a value decoder on a stored record was read as "missing", so the record was overwritten with default(). Now a KeyError starts from the default only when key in store is false; otherwise it propagates.

Minors fixed:

  • The docstring now says that a store handing back its own object keeps a failing change's mutation, and notes pickle's limits.
  • The 1 -> True test now goes through a file store.
  • The control test's Barrier has a timeout.
  • from e instead of from None.

Left as is (minor): an invalid key with a no-op change returns without error. A change that mutates still raises the store's KeyValidationError on the write. Suite: 1107 passed.

@thorwhalen
thorwhalen merged commit ddad3ba into master Dec 1, 2026
12 checks passed
@thorwhalen
thorwhalen deleted the locked-update-186 branch December 1, 2026 15:31
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.

locked_update(store, key, change): update_json generalised to any read-write store

1 participant