fix(advice): a race in the shared repository, and the prompt naming the wrong reading
Two findings from the re-review of PR #14. A race that recreated the leak ------------------------------- `get_shared_repository()` did an unsynchronised check-then-set on a module global. Eight concurrent advice threads on a cold process each saw None and each built their own repository - so eight MongoClients and twenty-five threads. The fix that was meant to remove the leak reintroduced it, just bounded by a race instead of by the number of readings. Measured before: 8 repositories built, 25 threads. Measured after: 1 repository built, 4 threads. There is a lock, and the check is repeated inside it. The prompt was about the wrong reading -------------------------------------- The advice targeted the right reading, but the PROMPT said "give advice for the most recent reading" - and when the brewer corrects an OLDER reading, the most recent one in the history is a different reading. So the model was asked about the wrong number while the advice was filed against the right one. The reading is now named in the instruction (with its date and gravity), marked in the reading list as the one to advise on, and the system prompt says to advise about the marked reading because it may not be the most recent one. A corrected reading is also flagged as such, so the model knows it may differ from what it said before. 167 tests (up from 164).
This commit is contained in:
@@ -19,6 +19,7 @@ from __future__ import annotations
|
||||
|
||||
import logging
|
||||
import os
|
||||
import threading
|
||||
from typing import Any
|
||||
|
||||
from pymongo import MongoClient
|
||||
@@ -380,13 +381,30 @@ def _as_id(value: object) -> str:
|
||||
#: repository does too.
|
||||
_shared_repository: "BeerRepository | None" = None
|
||||
|
||||
#: Guards the check-then-set above.
|
||||
#:
|
||||
#: Without it, concurrent advice threads each saw ``None`` and each
|
||||
#: built their own repository - so the fix reintroduced the very leak it
|
||||
#: was meant to remove, just bounded by a race instead of by the number
|
||||
#: of readings. Eight threads on a cold process built eight clients and
|
||||
#: twenty-five threads.
|
||||
_shared_repository_lock = threading.Lock()
|
||||
|
||||
|
||||
def get_shared_repository() -> "BeerRepository":
|
||||
"""The process-wide repository, built on first use."""
|
||||
"""The process-wide repository, built on first use.
|
||||
|
||||
Thread-safe: the first caller builds it, the rest wait and get the
|
||||
same one.
|
||||
"""
|
||||
global _shared_repository
|
||||
|
||||
if _shared_repository is None:
|
||||
_shared_repository = BeerRepository()
|
||||
with _shared_repository_lock:
|
||||
# Checked again inside the lock - another thread may have
|
||||
# built it while this one was waiting.
|
||||
if _shared_repository is None:
|
||||
_shared_repository = BeerRepository()
|
||||
return _shared_repository
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user