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)