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:
1 parent
94178e9b04
commit
4c32469a58
4 files changed
+76
-16
No files matched your search
+1
-1
@@ -142,7 +142,7 @@ def record(s, cwd, opt, sid=None):
|
||||
log = store.data("retro.jsonl")
|
||||
store.append(log, json.dumps(entry, sort_keys=True))
|
||||
if not store.commit("retro recorded", log):
|
||||
print("sisi: retro saved, but store git commit failed.")
|
||||
print("sisi: retro saved, but store git commit failed (%s)." % store.GIT_ERROR)
|
||||
s["snapshot"], s["changed"], s["retro_at"] = store.snapshot(cwd), False, now
|
||||
for key in ("prompts_since", "direct_since", "delegations_since", "reasons_since", "workers_since", "done", "paused",
|
||||
"fails_since", "picked", "starts", "pending", "meta", "reminded", "retro_demanded", "began"):
|
||||
|
||||
+10
-11
@@ -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))
|
||||
|
||||
+14
-4
@@ -96,20 +96,30 @@ def append(path, line, once=False):
|
||||
(not once or len(raw) <= 64000 and b"\0" not in raw and line[:-1] not in raw.splitlines()) and f.write(line)
|
||||
|
||||
|
||||
GIT_ERROR = "" # why the last git call that failed did: the first line of its stderr, or the error
|
||||
|
||||
|
||||
def git(args, cwd, timeout=15):
|
||||
# A repository named by the caller's environment (a git hook sets GIT_DIR) must not decide which project this folder is.
|
||||
# No optional locks: a plain `git status` holds index.lock while it runs, and a commit started then fails on it.
|
||||
global GIT_ERROR
|
||||
env = {k: v for k, v in os.environ.items() if k not in ("GIT_DIR", "GIT_WORK_TREE", "GIT_INDEX_FILE", "GIT_COMMON_DIR")}
|
||||
env["GIT_OPTIONAL_LOCKS"] = "0"
|
||||
try:
|
||||
r = subprocess.run(["git"] + args, cwd=cwd, capture_output=True, text=True, timeout=timeout, env=env)
|
||||
return r.returncode, r.stdout
|
||||
except (OSError, subprocess.SubprocessError):
|
||||
except (OSError, subprocess.SubprocessError) as e:
|
||||
GIT_ERROR = str(e)
|
||||
return 1, ""
|
||||
GIT_ERROR = (r.stderr.strip().partition("\n")[0] or "git exited with %d" % r.returncode) if r.returncode else ""
|
||||
return r.returncode, r.stdout
|
||||
|
||||
|
||||
def commit(message, *paths):
|
||||
"""One commit in the person's store holding only these paths, so the change can be undone on its own."""
|
||||
def commit(message, *paths, put=None):
|
||||
"""One commit in the person's store holding only these paths, so the change can be undone on its own.
|
||||
`put` ({path: text}) is written first, under the same lock: `git add` in another process never reads a file as it is replaced."""
|
||||
with lock("store"):
|
||||
os.path.isdir(os.path.join(DATA, ".git")) or git(["init", "-q"], DATA)
|
||||
[write(p, text) for p, text in (put or {}).items()]
|
||||
rel = [os.path.relpath(p, DATA) for p in paths]
|
||||
return (git(["add", "-A", "--"] + rel, DATA)[0] == 0 and
|
||||
(git(["status", "--porcelain", "--"] + rel, DATA) == (0, "") or
|
||||
|
||||
Reference in new issue
Block a user