SNIClient: fix program not exiting if SNI does not exist nor is running

This commit is contained in:
Fabian Dill 2022-07-26 16:44:32 +02:00 committed by Fabian Dill
parent 73afab67c8
commit 489450d3fa
1 changed files with 7 additions and 6 deletions

View File

@ -188,7 +188,10 @@ class Context(CommonContext):
async def shutdown(self): async def shutdown(self):
await super(Context, self).shutdown() await super(Context, self).shutdown()
if self.snes_connect_task: if self.snes_connect_task:
await self.snes_connect_task try:
await asyncio.wait_for(self.snes_connect_task, 1)
except asyncio.TimeoutError:
self.snes_connect_task.cancel()
def on_package(self, cmd: str, args: dict): def on_package(self, cmd: str, args: dict):
if cmd in {"Connected", "RoomUpdate"}: if cmd in {"Connected", "RoomUpdate"}:
@ -598,7 +601,7 @@ class SNESState(enum.IntEnum):
SNES_ATTACHED = 3 SNES_ATTACHED = 3
def launch_sni(ctx: Context): def launch_sni():
sni_path = Utils.get_options()["lttp_options"]["sni"] sni_path = Utils.get_options()["lttp_options"]["sni"]
if not os.path.isdir(sni_path): if not os.path.isdir(sni_path):
@ -636,11 +639,9 @@ async def _snes_connect(ctx: Context, address: str):
address = f"ws://{address}" if "://" not in address else address address = f"ws://{address}" if "://" not in address else address
snes_logger.info("Connecting to SNI at %s ..." % address) snes_logger.info("Connecting to SNI at %s ..." % address)
seen_problems = set() seen_problems = set()
succesful = False while 1:
while not succesful:
try: try:
snes_socket = await websockets.connect(address, ping_timeout=None, ping_interval=None) snes_socket = await websockets.connect(address, ping_timeout=None, ping_interval=None)
succesful = True
except Exception as e: except Exception as e:
problem = "%s" % e problem = "%s" % e
# only tell the user about new problems, otherwise silently lay in wait for a working connection # only tell the user about new problems, otherwise silently lay in wait for a working connection
@ -650,7 +651,7 @@ async def _snes_connect(ctx: Context, address: str):
if len(seen_problems) == 1: if len(seen_problems) == 1:
# this is the first problem. Let's try launching SNI if it isn't already running # this is the first problem. Let's try launching SNI if it isn't already running
launch_sni(ctx) launch_sni()
await asyncio.sleep(1) await asyncio.sleep(1)
else: else: