If extract_all() returns None (Dashboard init failure), appending it to store["records"] would cause _accept_loop to crash with AttributeError on the next client connection. Since only OSError was caught, the thread died permanently — no further clients could join push_clients, so live broadcasts reached nobody. - collect.py: skip None snapshots instead of appending them - network.py: filter None records in _accept_loop; catch all exceptions during history send (not just OSError) so the thread survives - storage_helpers.py: filter None records when loading from disk Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
86 lines
2.9 KiB
Python
86 lines
2.9 KiB
Python
import json
|
|
import logging
|
|
import socket
|
|
import threading
|
|
|
|
from jaydiff.diff import diff_full as jay_diff_full
|
|
|
|
log = logging.getLogger(__name__)
|
|
|
|
|
|
def _accept_loop(
|
|
server_sock: socket.socket,
|
|
clients: list,
|
|
lock: threading.Lock,
|
|
store_ref: dict,
|
|
) -> None:
|
|
while True:
|
|
try:
|
|
conn, addr = server_sock.accept()
|
|
log.info("Push client connected from %s", addr)
|
|
with lock:
|
|
store = store_ref["current"]
|
|
in_mem = store["records"] if store else []
|
|
preloaded = store_ref.get("preloaded", [])
|
|
|
|
if preloaded:
|
|
seen_ts = {r.get("ts") for r in preloaded if r is not None}
|
|
extra = [r for r in in_mem if r is not None and r.get("ts") not in seen_ts]
|
|
records = sorted(preloaded + extra, key=lambda r: r.get("ts", "") if r is not None else "")
|
|
else:
|
|
records = [r for r in in_mem if r is not None]
|
|
|
|
try:
|
|
prev: dict = {}
|
|
for record in records:
|
|
if record is None:
|
|
continue
|
|
diff = jay_diff_full(prev, record, combine_upd_add=True)
|
|
prev = record
|
|
conn.sendall((json.dumps(diff, default=str) + "\n").encode())
|
|
if records:
|
|
log.info("Sent %d record(s) to %s", len(records), addr)
|
|
except OSError:
|
|
log.debug("Failed to send history to %s", addr)
|
|
conn.close()
|
|
continue
|
|
except Exception:
|
|
log.exception("Error sending history to %s — dropping client", addr)
|
|
conn.close()
|
|
continue
|
|
clients.append(conn)
|
|
except OSError:
|
|
break
|
|
|
|
|
|
def broadcast(snapshot: dict, clients: list, lock: threading.Lock) -> None:
|
|
line = (json.dumps(snapshot, default=str) + "\n").encode()
|
|
with lock:
|
|
dead = []
|
|
for conn in clients:
|
|
try:
|
|
conn.sendall(line)
|
|
except OSError:
|
|
dead.append(conn)
|
|
for conn in dead:
|
|
clients.remove(conn)
|
|
conn.close()
|
|
log.info("Push client disconnected")
|
|
|
|
|
|
def start_push_server(host: str, port: int) -> tuple:
|
|
server_sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
server_sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
|
|
server_sock.bind((host, port))
|
|
server_sock.listen()
|
|
clients: list = []
|
|
lock = threading.Lock()
|
|
store_ref: dict = {"current": None, "preloaded": []}
|
|
t = threading.Thread(
|
|
target=_accept_loop,
|
|
args=(server_sock, clients, lock, store_ref),
|
|
daemon=True,
|
|
)
|
|
t.start()
|
|
log.info("Push server listening on %s:%d", host, port)
|
|
return clients, lock, store_ref |