The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): hosted-room attachment reads no longer redo store startup or block on other state.db writers
Tekniumd3652dc68688
diff --git a/gateway/hosted_room_attachments.py b/gateway/hosted_room_attachments.pyindex 9f77e51df2e..c6922d37182 100644--- a/gateway/hosted_room_attachments.py+++ b/gateway/hosted_room_attachments.py@@ -289,6 +289,13 @@ def validate_task_manifest(value: Any) -> list[dict[str, Any]]:return normalized+# (state.db, blob root) pairs whose schema, event reconciliation and prune already ran in this+# process. Callers build a store per operation (one per attachment chunk); startup work and+# its write locks must not repeat on every read.+_STARTED: set[tuple[str, str, int, int] | None] = set()+_STARTED_LOCK = threading.Lock()++class HostedRoomAttachmentStore:"""SQLite-owned metadata and private, content-deduplicated blob bytes."""@@ -313,10 +320,27 @@ class HostedRoomAttachmentStore:self.gateway_quota_count = max(1, int(gateway_quota_count))self._lock = threading.RLock()self._prepare_private_root()- conn = self._connect()- conn.close()- self.reconcile_room_events()- self.prune()+ with _STARTED_LOCK:+ if self._startup_key() in _STARTED:+ return+ conn = self._connect()+ try:+ conn.execute("BEGIN IMMEDIATE")+ self._initialize(conn)+ conn.commit()+ finally:+ conn.close()+ self.reconcile_room_events()+ self.prune()+ _STARTED.add(self._startup_key())++ def _startup_key(self) -> tuple[str, str, int, int] | None:+ """Identity of the database FILE, so a recreated state.db is set up again."""+ try:+ info = self.db_path.stat()+ except OSError:+ return None+ return str(self.db_path.resolve()), str(self.root.resolve()), info.st_dev, info.st_inodef _prepare_private_root(self) -> None:self.root.mkdir(parents=True, exist_ok=True, mode=0o700)@@ -398,11 +422,7 @@ class HostedRoomAttachmentStore:try:apply_wal_with_fallback(conn, db_label="state.db (hosted room attachments)")conn.execute("PRAGMA foreign_keys=ON")- conn.execute("BEGIN IMMEDIATE")- self._initialize(conn)- conn.commit()except Exception:- conn.rollback()conn.close()raisereturn conndiff --git a/tests/gateway/test_hosted_room_attachments.py b/tests/gateway/test_hosted_room_attachments.pyindex 40c66d9a338..61462bea644 100644--- a/tests/gateway/test_hosted_room_attachments.py+++ b/tests/gateway/test_hosted_room_attachments.py@@ -519,3 +519,28 @@ def test_restart_reconciles_event_commit_and_expires_abandoned_commit(tmp_path):attachment_id=abandoned["attachment_id"],recipient_member_id="research",)+++def test_attachment_reads_take_no_write_lock_and_repeat_no_startup(tmp_path):+ """Hosted transfer builds a store per chunk. Schema setup, event reconciliation and the+ orphan prune belong to startup; a read must not queue behind another state.db writer+ (rollback-journal fallback: RESERVED still admits readers) nor fail as 'locked'."""+ import time++ db = tmp_path / "state.db"+ store = HostedRoomAttachmentStore(db)+ item = _put(store)+ store.commit_message(room_id="room-1", event_id="event-1", manifest=_manifest(item),+ recipient_member_ids=("research",))+ writer = sqlite3.connect(db, isolation_level=None, timeout=0)+ writer.execute("BEGIN IMMEDIATE")+ try:+ started = time.monotonic()+ saved = HostedRoomAttachmentStore(db).read_range(+ room_id="room-1", attachment_id=item["attachment_id"], event_id="event-1",+ recipient_member_id="research", offset=0, length=4)+ assert saved.data == PNG[:4]+ assert time.monotonic() - started < 2+ finally:+ writer.rollback()+ writer.close()