From dfc8d4762bbecc421c175bf8dfadc7316818cf42 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=27=C3=A9lectron=20rare?= <108685187+electron-rare@users.noreply.github.com> Date: Sun, 21 Jun 2026 09:31:42 +0200 Subject: [PATCH] fix(tower): lock transfers history writes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Add _history_lock to serialize history load→extend→write in transfers_end(), preventing lost-update races on transfers.json if a second writer is added later. Fix file handle leak in _transfers_history_load() with context manager. --- tower/lisael_content.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/tower/lisael_content.py b/tower/lisael_content.py index 703a0b9..e0d781b 100644 --- a/tower/lisael_content.py +++ b/tower/lisael_content.py @@ -412,6 +412,9 @@ def push_file(ip, name, subdir="podcasts"): _busy = set() _busy_lock = threading.Lock() +# ---- transfers journal lock ---- +_history_lock = threading.Lock() + def push_batch(ip, delta): ok = 0 for f in delta: @@ -494,7 +497,8 @@ def transfers_active(): def _transfers_history_load(): if os.path.exists(TRANSFERS_JSON): try: - return json.load(open(TRANSFERS_JSON, encoding="utf-8")) + with open(TRANSFERS_JSON, encoding="utf-8") as fh: + return json.load(fh) except Exception: pass return [] @@ -510,7 +514,8 @@ def transfers_end(): done = [{k: v for k, v in f.items() if k != "_t0"} for f in _active["files"] if f["status"] in ("done", "failed")] _active = None - hist = _transfers_history_load() - hist.extend(done) - hist = hist[-TRANSFERS_MAX:] - _atomic_write_json(TRANSFERS_JSON, hist) + with _history_lock: + hist = _transfers_history_load() + hist.extend(done) + hist = hist[-TRANSFERS_MAX:] + _atomic_write_json(TRANSFERS_JSON, hist)