Store commits: write the files they stage under the store lock; reads take no optional lock

A commit under the store lock read a store file as another process replaced it
outside that lock. git: "short read while indexing rules/last-project.json"
(CI run 46195092165), then "rules/user.md" from rules organize. The undo
snapshot and organize's two store files are now written inside store.commit,
under the lock. sisi's own unlocked `git status` also held index.lock against
those commits ("Unable to create index.lock: File exists"): git calls take no
optional lock. The first line of git's stderr stays in the message.
This commit is contained in:
swaits committed 2026-11-28 15:53:39 -07:00
1 parent 94178e9b04
commit 4c32469a58
4 files changed
+76 -16

No files matched your search

+10 -11
View File
@@ -179,13 +179,15 @@ def scope_lines(scope, cwd=None, migrate=True):
return list(dict.fromkeys((_parse(raw.decode("utf-8", "replace")) if raw is not None else []) + lines(old)))
def _commit(msg, *paths, snap=None):
def _commit(msg, *paths, snap=None, put=None):
"""Commit `paths` to the store. `put` ({path: text}) and the undo snapshot `snap` are written inside the store lock, with the commit."""
if snap:
p = store.data("rules", "last-project.json")
store.write(p, json.dumps(dict(zip(("path", "before", "after"), snap))))
paths = (p,) + paths
if not store.commit(msg, *paths):
raise OSError("store git commit failed")
paths, put = (p,) + paths, {**(put or {}), p: json.dumps(dict(zip(("path", "before", "after"), snap)))}
# written inside the store lock, never under a project's or the user's: another process's `git add` read these
# files as they were replaced ("short read while indexing")
if not store.commit(msg, *paths, put=put):
raise OSError("store git commit failed (%s)" % store.GIT_ERROR)
def add(text, scope, source, cwd=None, log=None, means=""):
@@ -390,21 +392,18 @@ def organize_record(answer_file, cwd):
except OSError as e:
return "could not write %s (%s)" % (pp, e)
after is not None and note(pp)
wrote_u = False
utext = "".join(l + "\n" for l in got[USER])
try:
store.write(up, "".join(l + "\n" for l in got[USER]))
wrote_u = True
seen = _organized()
seen.update({_mark_key(USER): len(_retro(got[USER])), _mark_key(PROJECT, cwd): len(_retro(got[PROJECT]))})
seen.pop("rules/projects/%s.md" % store.project_key(cwd), None)
marks = store.data("rules", "organized.json")
store.write(marks, json.dumps(seen))
dp, ts = store.data(DROPS), time.strftime("%Y-%m-%dT%H:%M:%S")
for r, pn, why in recs:
store.append(dp, json.dumps({"ts": ts, "rule": r, "problem": pn, "reason": why}, sort_keys=True))
_commit("rules organized", marks, up, *([dp] if recs else []), snap=(pp, before, after))
_commit("rules organized", marks, up, *([dp] if recs else []), snap=(pp, before, after), put={up: utext, marks: json.dumps(seen)})
except OSError as e:
wrote_u and build_rulebook()
(wrote_u := store.regular(up, True)[1] == utext.encode()) and build_rulebook()
if not wrote and not wrote_u:
return "could not write %s (%s)" % (up, e)
print("the organized rules are in %s; they could not be recorded for undo: %s" % (pp if wrote else up, e))