scratch_link.py: Avoid eternal loop by hostname resolve failure

When the address of device-manager.scratch.mit.edu can not be resolved,
scratch_link.py catches the exception for the resolve failure and
restarts itself. This results in eternal loop.

To avoid the eternal loop, catch the resolve failure, print error
message and break the loop. Also improve the error message for the other
exceptions caught in the loop.

Signed-off-by: Shin'ichiro Kawasaki <kawasaki@juno.dti.ne.jp>
This commit is contained in:
Shin'ichiro Kawasaki
2021-05-05 13:06:22 +09:00
parent ea1109cee2
commit 58a60c94db

View File

@@ -8,6 +8,7 @@ import asyncio
import pathlib
import ssl
import websockets
import socket
import json
import base64
import logging
@@ -42,6 +43,8 @@ logger.setLevel(logLevel)
logger.addHandler(handler)
logger.propagate = False
HOSTNAME="device-manager.scratch.mit.edu"
class Session():
"""Base class for BTSession and BLESession"""
def __init__(self, websocket, loop):
@@ -738,7 +741,7 @@ def main():
ssl_context.load_cert_chain(localhost_cer, localhost_key)
start_server = websockets.serve(
ws_handler, "device-manager.scratch.mit.edu", 20110, ssl=ssl_context
ws_handler, HOSTNAME, 20110, ssl=ssl_context
)
while True:
@@ -749,7 +752,13 @@ def main():
except KeyboardInterrupt as e:
stack_trace()
break
except socket.gaierror as e:
logger.error(f"{type(e).__name__}: {e}")
logger.info(f"Check internet connection to {HOSTNAME}. If not "
f"available, add '127.0.0.1 {HOSTNAME}' to /etc/hosts.")
break
except Exception as e:
logger.error(f"{type(e).__name__}: {e}")
logger.info("Restarting scratch-link...")
if __name__ == "__main__":