From bed3563ae64c746ff7f6282620ee8f73d26db894 Mon Sep 17 00:00:00 2001 From: Brad Stein Date: Mon, 19 Jan 2026 01:53:13 -0300 Subject: [PATCH] mailu-sync: cap wait in listener --- services/mailu/scripts/mailu_sync_listener.py | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/services/mailu/scripts/mailu_sync_listener.py b/services/mailu/scripts/mailu_sync_listener.py index 27070c0..6ac0da7 100644 --- a/services/mailu/scripts/mailu_sync_listener.py +++ b/services/mailu/scripts/mailu_sync_listener.py @@ -1,5 +1,6 @@ import http.server import json +import os import subprocess import threading @@ -7,15 +8,17 @@ from time import time # Simple debounce to avoid hammering on bursts MIN_INTERVAL_SECONDS = 10 +WAIT_TIMEOUT_SECONDS = float(os.environ.get("MAILU_SYNC_WAIT_TIMEOUT_SEC", "20")) last_run = 0.0 lock = threading.Lock() sync_done = threading.Event() sync_done.set() sync_running = False +last_rc = None def _run_sync_blocking() -> int: - global last_run, sync_running + global last_run, sync_running, last_rc with lock: if sync_running: return 0 @@ -27,6 +30,7 @@ def _run_sync_blocking() -> int: proc = subprocess.run(["python", "/app/sync.py"], check=False) rc = int(proc.returncode) print(f"mailu-sync-listener: sync completed rc={rc}", flush=True) + last_rc = rc return rc finally: with lock: @@ -66,16 +70,20 @@ class Handler(http.server.BaseHTTPRequestHandler): if wait: with lock: already_running = sync_running - if already_running: - sync_done.wait(timeout=120) - with lock: - still_running = sync_running - self.send_response(200 if not still_running else 503) - self.end_headers() - return - rc = _run_sync_blocking() - self.send_response(200 if rc == 0 else 500) + if not already_running: + _trigger_sync_async() + + sync_done.wait(timeout=WAIT_TIMEOUT_SECONDS) + with lock: + still_running = sync_running + rc = last_rc + + if still_running: + # Avoid blocking callers while a sync is in flight. + self.send_response(200) + else: + self.send_response(200 if rc == 0 else 500) self.end_headers() return