Reconnect after transient socket disconnects

This commit is contained in:
Tanner committed 2026-11-14 00:21:19 +00:00
1 parent 62ed14e334
commit 7d6fc8aa81
2 files changed
+65 -1

No files matched your search

+43 -1
View File
@@ -350,6 +350,48 @@ def api_item_icon(name):
def root():
return app.send_static_file('index.html')
def _run_connection_loop(global_state, connector=None, sleep=time.sleep):
"""Retry the client after expected TCP disconnects without killing the API."""
connector = connector or connect.main
retry_delay = 1.0
while global_state.get('running', True):
attempt_started = time.monotonic()
try:
connector(global_state)
except KeyboardInterrupt:
global_state.running = False
return
except (OSError, EOFError, ValueError) as exc:
if (isinstance(exc, ValueError) and
str(exc) != 'I/O operation on closed file'):
raise
print('[connection] socket closed ({}); will reconnect'.format(exc),
flush=True)
else:
if not global_state.get('running', True):
return
print('[connection] server disconnected; will reconnect', flush=True)
if not global_state.get('running', True):
return
if time.monotonic() - attempt_started >= 60.0:
retry_delay = 1.0
connection = global_state.get('connection')
if connection and hasattr(connection, 'disconnect'):
try:
connection.disconnect(immediate=True)
except OSError:
pass
global_state.connection = False
global_state.connected = False
global_state.connection_status = 'reconnecting'
print('[connection] retrying in {:.1f}s'.format(retry_delay),
flush=True)
sleep(retry_delay)
retry_delay = min(retry_delay * 2.0, 30.0)
def main():
"""Run the 26.3 client with in-place development hot reload.
@@ -511,7 +553,7 @@ def main():
worker.start()
try:
connect.main(global_state)
_run_connection_loop(global_state, connect.main)
except KeyboardInterrupt:
pass
except BaseException: