Skip to content
Python

fix(mentions): survive non-object JSON and write mention_threads.json atomically (#709)

Devika Vermadcb30488e290
diff --git a/coworker/mentions.py b/coworker/mentions.py
index 2d5b840f..5b6f65e0 100644
--- a/coworker/mentions.py
+++ b/coworker/mentions.py
@@ -16,11 +16,14 @@ clears its records (same contract as subscriptions).
from __future__ import annotations
import json
+import logging
import threading
from dataclasses import asdict, dataclass
from pathlib import Path
from typing import Optional
+logger = logging.getLogger(__name__)
+
@dataclass
class MentionThread:
@@ -43,17 +46,34 @@ class MentionSessionStore:
self._threads = [
MentionThread(**raw) for raw in data.get("threads", [])
]
- except (OSError, ValueError, TypeError):
- self._threads = [] # a corrupt file must never block startup
+ except (OSError, ValueError, TypeError, AttributeError):
+ # A corrupt file must never block startup. AttributeError covers a
+ # file that is valid JSON but not an object (`[]`, `null`, a string).
+ logger.warning(
+ "mention_threads.json at %s is unreadable; starting empty", self.path
+ )
+ self._threads = []
def _save(self) -> None:
if not self.path:
return
- self.path.parent.mkdir(parents=True, exist_ok=True)
- self.path.write_text(
- json.dumps({"threads": [asdict(t) for t in self._threads]}, indent=2),
- encoding="utf-8",
- )
+ # Write a sibling .tmp and rename it over the real file, so a crash mid-write
+ # leaves the previous complete file instead of a truncated one (#709).
+ temporary = self.path.with_suffix(self.path.suffix + ".tmp")
+ try:
+ self.path.parent.mkdir(parents=True, exist_ok=True)
+ temporary.write_text(
+ json.dumps({"threads": [asdict(t) for t in self._threads]}, indent=2),
+ encoding="utf-8",
+ )
+ temporary.replace(self.path)
+ except OSError as exc:
+ # Persistence is best-effort; memory stays authoritative for this process.
+ logger.warning("could not save %s: %s", self.path, exc)
+ try:
+ temporary.unlink()
+ except OSError:
+ pass
# -- mutations --------------------------------------------------------------
def set(self, thread_target: str, session_id: str, channel: str) -> MentionThread:
diff --git a/tests/test_mention_store_robustness.py b/tests/test_mention_store_robustness.py
index 870efee2..10616c2d 100644
--- a/tests/test_mention_store_robustness.py
+++ b/tests/test_mention_store_robustness.py
@@ -1,12 +1,15 @@
"""MentionSessionStore: a corrupt mention_threads.json must not brick server startup.
-An append interrupted mid-write (crash, SIGKILL, full disk) leaves a truncated JSON
+A save interrupted mid-write (crash, SIGKILL, full disk) leaves a truncated JSON
document, and a field added to ``MentionThread`` makes every older record fail to
-construct. Both raise straight out of ``__init__``, and the store is built eagerly by
-``SessionManager.__init__`` — so ``openworker-server`` dies on every start until the user
-finds and deletes the file by hand. Every sibling store in that constructor already treats
-a corrupt file as "start empty" (``ParkedStore``, ``ChannelBuffer``, the people
-directory); this one was the outlier.
+construct. Both used to raise straight out of ``__init__``, and the store is built
+eagerly by ``SessionManager.__init__`` — so ``openworker-server`` died on every start
+until the user found and deleted the file by hand. ``ParkedStore`` in the same
+constructor already treats a corrupt file as "start empty"; the other JSON stores
+listed in #204 still do not.
+
+#709: the guard also has to cover a file that is valid JSON but not an object, and
+the save has to be atomic so it stops producing the torn file in the first place.
"""
from __future__ import annotations
@@ -47,3 +50,35 @@ def test_load_survives_a_record_with_a_missing_field(tmp_path):
older = {k: v for k, v in _RECORD.items() if k != "channel"}
path = _write(tmp_path, json.dumps({"threads": [older]}))
assert MentionSessionStore(path).all() == []
+
+
+def test_load_survives_valid_json_that_is_not_an_object(tmp_path):
+ # `[]`, `null` and a bare string all parse, then fail on `.get` (#709).
+ for body in ("[]", "null", '"hello"'):
+ path = _write(tmp_path, body)
+ assert MentionSessionStore(path).all() == []
+
+
+def test_save_leaves_no_temp_file_behind(tmp_path):
+ path = tmp_path / "mention_threads.json"
+ store = MentionSessionStore(path)
+ store.set(_RECORD["thread_target"], _RECORD["session_id"], _RECORD["channel"])
+ assert json.loads(path.read_text(encoding="utf-8")) == {"threads": [_RECORD]}
+ assert not path.with_suffix(".json.tmp").exists()
+
+
+def test_save_failure_keeps_the_previous_file_and_the_in_memory_record(tmp_path, monkeypatch):
+ path = _write(tmp_path, json.dumps({"threads": [_RECORD]}))
+ store = MentionSessionStore(path)
+
+ def refuse(self, target):
+ raise OSError("disk full")
+
+ monkeypatch.setattr(type(path), "replace", refuse)
+ # The failed save must not raise into the caller (a Slack message handler)...
+ store.set("slack:C0123:1700.000200", "sid2", "slack:C0123")
+ # ...memory still has both records, the file on disk is the old complete one,
+ # and no .tmp is left beside it.
+ assert len(store.all()) == 2
+ assert json.loads(path.read_text(encoding="utf-8")) == {"threads": [_RECORD]}
+ assert not path.with_suffix(".json.tmp").exists()