From 58a60c94dbc1dfc68140577af6c75a8d9500d899 Mon Sep 17 00:00:00 2001 From: Shin'ichiro Kawasaki Date: Wed, 5 May 2021 13:06:22 +0900 Subject: [PATCH] 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 --- pyscrlink/scratch_link.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyscrlink/scratch_link.py b/pyscrlink/scratch_link.py index 631a9b1..65593c6 100755 --- a/pyscrlink/scratch_link.py +++ b/pyscrlink/scratch_link.py @@ -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__":