Repository navigation
locked_update(store, key, change, *, lock): update_json's step for any store (#186) - #227
Merged
Merged
Conversation
…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
force-pushed
the
locked-update-186
branch
from
December 1, 2026 15:27
14ca369 to
bc88eaf
Compare
Member
Author
|
Adversarial review (independent Opus subagent) found 3 majors, all fixed in bc88eaf:
Minors fixed:
Left as is (minor): an invalid key with a no-op |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
MutableMappingwith atomic writes and a per-keylocked(key)context, whose callers repeat "lock, read strictly, change, write" at four sites, two of which fit this function exactly. Another application repeatsupdate_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 fromdol. It isupdate_json's step over one key of any read-write mapping:lockaround the read and the write; whether the lock is held is judged exactly asupdate_jsonjudges it (shared_is_held), with the same warning when it is not;KeyErrorstarts fromdefault(), and any other error propagates, so nothing is written over a value that could not be read. The docstring points toFiles(atomic=True)/strict_key_errors=True, because a plainFilesreports a permission error as aKeyError;default=Nonemeans the key must exist: a missing key raisesKeyError, andchangeis not called. The issue's comment left this choice open: store-level code speaksKeyError, whileupdate_jsonkeeps itsFileNotFoundError;snapshotof it changed.pickle.dumpsis the default because==would miss1 -> 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):
lockis 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 inupdate_json(lock=file_lock(path),lock=store.locked(key),lock=threading.Lock()).Not done: rewriting
update_jsonon top oflocked_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):default=Nonerefuses a missing key without callingchange;changewrites nothing;1 -> Truecounts as a change;KeyErrorread error propagates, with nothing written;file_lockon aJsonFilesstore end at 200;Full suite: 1090 passed, 11 skipped.
🤖 Generated with Claude Code