Reconnect after transient socket disconnects
This commit is contained in:
1 parent
62ed14e334
commit
7d6fc8aa81
2 files changed
+65
-1
No files matched your search
@@ -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:
|
||||
|
||||
@@ -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):
|
||||
|
||||
Reference in new issue
Block a user