From 7d6fc8aa81679c2e2b3763f9053f48538cce82d7 Mon Sep 17 00:00:00 2001 From: Tanner Date: Sat, 14 Nov 2026 00:21:19 +0000 Subject: [PATCH] Reconnect after transient socket disconnects --- mosfet.py | 44 ++++++++++++++++++++++++++++++++- tests/test_web_inventory_api.py | 22 +++++++++++++++++ 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/mosfet.py b/mosfet.py index ad78049..37cbd9d 100644 --- a/mosfet.py +++ b/mosfet.py @@ -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: diff --git a/tests/test_web_inventory_api.py b/tests/test_web_inventory_api.py index 1d2991b..ee568c2 100644 --- a/tests/test_web_inventory_api.py +++ b/tests/test_web_inventory_api.py @@ -30,6 +30,28 @@ class FakeGame: class TestWebInventoryAPI(unittest.TestCase): + def test_connection_loop_retries_socket_disconnects_without_crashing(self): + state = Munch(running=True) + attempts = [] + delays = [] + + def connector(global_state): + attempts.append(True) + if len(attempts) == 1: + raise BrokenPipeError(32, 'Broken pipe') + if len(attempts) == 2: + raise ValueError('I/O operation on closed file') + global_state.running = False + + mosfet_web._run_connection_loop( + state, connector, sleep=lambda delay: delays.append(delay)) + + self.assertEqual(len(attempts), 3) + self.assertEqual(delays, [1.0, 2.0]) + self.assertFalse(state.connected) + self.assertIs(state.connection, False) + self.assertEqual(state.connection_status, 'reconnecting') + def test_web_port_uses_bot_name_suffix(self): for suffix in range(1, 10): with self.subTest(suffix=suffix):