The agent that grows with you
Python aiai-agentai-agentsanthropicchatgptclaudeclaude-codecodexhermeshermes-agentllmnous-researchopenai
fix(gateway): ThreadParticipationTracker no longer holds the set lock across os.replace
kshitijk4poor0c417ece25e3
diff --git a/gateway/platforms/helpers.py b/gateway/platforms/helpers.pyindex 6d439098902..3ae4b9ba2cc 100644--- a/gateway/platforms/helpers.py+++ b/gateway/platforms/helpers.py@@ -113,12 +113,19 @@ class ThreadParticipationTracker:def __init__(self, platform_name: str, max_tracked: int = 500):self._platform = platform_nameself._max_tracked = max_tracked- # Serializes the in-memory set against ``_save``'s trim. ``mark_async``- # runs the persist on a worker thread, which removes the accidental- # serialization the event loop used to provide, so the lock has to be- # added in the SAME change that introduces the concurrency. Re-entrant- # because ``mark`` takes it and then calls ``_save``, which takes it too.- self._lock = threading.RLock()+ # ``mark_async`` runs the persist on a worker thread, which removes the+ # accidental serialization the event loop used to provide. Two locks,+ # never nested the other way round:+ # ``_lock`` guards ONLY the in-memory set (``_remember``,+ # ``__contains__``, ``clear`` and the snapshot in+ # ``_save``); held for microseconds, safe on the loop.+ # ``_io_lock`` worker-only; serializes snapshot+write in ``_save`` so+ # two persists cannot interleave and lose an entry.+ # ``_save`` must NOT hold ``_lock`` across ``os.replace``: the adapters'+ # ``thread_id in tracker`` gate runs on the loop thread and would stall+ # for the whole rename.+ self._lock = threading.Lock()+ self._io_lock = threading.Lock()self._threads: dict[str, None] = dict.fromkeys(str(t) for t in self._load())def _state_path(self) -> Path:@@ -133,11 +140,12 @@ class ThreadParticipationTracker:return [str(thread_id) for thread_id in data] if isinstance(data, list) else []def _save(self) -> None:- with self._lock:- thread_list = list(self._threads)- if len(thread_list) > self._max_tracked:- thread_list = thread_list[-self._max_tracked:]- self._threads = dict.fromkeys(thread_list)+ with self._io_lock:+ with self._lock:+ thread_list = list(self._threads)+ if len(thread_list) > self._max_tracked:+ thread_list = thread_list[-self._max_tracked:]+ self._threads = dict.fromkeys(thread_list)atomic_json_write(self._state_path(), thread_list, indent=None)def _remember(self, thread_id: str) -> bool:diff --git a/tests/gateway/test_thread_tracker_mark_off_loop.py b/tests/gateway/test_thread_tracker_mark_off_loop.pyindex f2ed1306d3b..379e7e33f2d 100644--- a/tests/gateway/test_thread_tracker_mark_off_loop.py+++ b/tests/gateway/test_thread_tracker_mark_off_loop.py@@ -36,6 +36,7 @@ import asyncioimport contextlibimport jsonimport threading+import timeimport pytest@@ -78,10 +79,22 @@ def test_two_concurrent_marks_do_not_lose_an_entry(tracker, monkeypatch):monkeypatch.setattr(helpers, "atomic_json_write", _synchronised_write, raising=True)async def scenario():- await asyncio.gather(+ marks = asyncio.gather(tracker.mark_async("!a:example.org"),tracker.mark_async("!b:example.org"),)+ # A worker is parked inside the rename. Neither the second mark's+ # in-memory insert nor a membership check may wait for it: both take+ # the set lock ON THE LOOP THREAD, so holding that lock across+ # ``os.replace`` stalls the loop for the whole rename.+ started = time.monotonic()+ await asyncio.sleep(0.05)+ assert "!a:example.org" in tracker+ assert time.monotonic() - started < 0.2, (+ "the loop stalled on the tracker lock while a worker held it "+ "across os.replace"+ )+ await marksasyncio.run(scenario())