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
@@ -1231,6 +1231,57 @@ class Core(unittest.TestCase):
|
||||
self.assertIn("First concurrent project rule", after_revert)
|
||||
self.assertNotIn("Second concurrent project rule", after_revert)
|
||||
|
||||
def test_undo_snapshot_is_written_inside_the_store_lock(self):
|
||||
# rules/last-project.json is one file for every project, each written under its own project's lock. Another
|
||||
# project replaced it between `git add` taking its size and reading it: "short read while indexing" (run 46195092165).
|
||||
from sisi import rules, store
|
||||
self.sisi("learn", "--project", "First rule makes the snapshot")
|
||||
snap, real_lock, seen = os.path.join(self.data, "rules", "last-project.json"), store.lock, []
|
||||
before = self.read(snap)
|
||||
|
||||
def locked(key, wait=True):
|
||||
key == "store" and seen.append(self.read(snap))
|
||||
return real_lock(key, wait=wait)
|
||||
|
||||
with mock.patch.object(store, "lock", locked):
|
||||
res, _ = self.in_process(rules.add, "Rule written while the store lock is asked for", rules.PROJECT, "owner", self.proj)
|
||||
self.assertEqual(res[1], "")
|
||||
self.assertEqual(seen, [before]) # still the old snapshot when the lock is asked for: it is written under the lock
|
||||
self.assertIn("Rule written while the store lock is asked for", self.read(snap))
|
||||
committed = subprocess.run(["git", "show", "HEAD:rules/last-project.json"], cwd=self.data, capture_output=True, text=True, check=True).stdout
|
||||
self.assertEqual(json.loads(committed)["after"], self.read(os.path.join(self.proj, ".sisi", "rules.md")))
|
||||
|
||||
def test_a_git_read_leaves_the_index_alone(self):
|
||||
# A plain `git status` holds index.lock while it runs and rewrites the index when its stat info is stale; a
|
||||
# commit that starts then fails with "Unable to create index.lock: File exists". A read of sisi's takes no lock.
|
||||
from sisi import store
|
||||
self.sisi("learn", "--project", "Tracked file whose stat info goes stale")
|
||||
tracked, index, later = os.path.join(self.data, "rules", "last-project.json"), os.path.join(self.data, ".git", "index"), time.time() + 10
|
||||
os.utime(tracked, (later, later)) # same content, other mtime: plain git would write the refreshed index back
|
||||
before = os.stat(index).st_ino
|
||||
self.assertEqual(store.git(["status", "--porcelain"], self.data)[0], 0)
|
||||
self.assertEqual(os.stat(index).st_ino, before)
|
||||
|
||||
def test_organized_store_files_are_written_inside_the_store_lock(self):
|
||||
# organize replaced rules/user.md under a project's lock; a user rule's concurrent `git add` read it as it
|
||||
# shrank: "short read while indexing rules/user.md".
|
||||
from sisi import rules, store
|
||||
self.sisi("learn", "User rule before organizing")
|
||||
user, real_lock, seen = os.path.join(self.data, "rules", "user.md"), store.lock, []
|
||||
before = self.read(user)
|
||||
|
||||
def locked(key, wait=True):
|
||||
key == "store" and seen.append(self.read(user))
|
||||
return real_lock(key, wait=wait)
|
||||
|
||||
answer = self.write(os.path.join(self.tmp, "answer.md"), "## user\n- Organized user rule [retro 2026-11-24]\n## project\n- Organized project rule [retro 2026-11-24]\n")
|
||||
self.in_process(rules.organize_brief, self.proj)
|
||||
with mock.patch.object(store, "lock", locked):
|
||||
why, out = self.in_process(rules.organize_record, answer, self.proj)
|
||||
self.assertIsNone(why, out)
|
||||
self.assertEqual(seen, [before]) # still the old list when the lock is asked for: it is replaced under the lock
|
||||
self.assertEqual(self.read(user), "- Organized user rule [retro 2026-11-24]\n")
|
||||
|
||||
def test_folders_outside_git_are_each_their_own_project(self):
|
||||
notes, other, thing = (self.mkdir(self.tmp, *p) for p in (("notes",), ("code", "other"), ("code", "thing")))
|
||||
self.assertEqual(self.sisi("learn", "--project", "A rule written at the home folder", cwd=self.tmp)[0], 0)
|
||||
|
||||
Reference in new issue
Block a user