fix(tower): lock transfers history writes

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.
This commit is contained in:
L'électron rare
2026-06-21 09:31:42 +02:00
parent 58da6b63e4
commit dfc8d4762b
+10 -5
View File
@@ -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)