From 8b7d1afd19c9dbae492262b72642276c029724e5 Mon Sep 17 00:00:00 2001 From: Jonathan Moody <103143855+moodyjon@users.noreply.github.com> Date: Wed, 21 Sep 2022 11:38:07 -0500 Subject: [PATCH] Retry on EADDRINUSE. --- hub/herald/session.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/hub/herald/session.py b/hub/herald/session.py index a314518..72662f3 100644 --- a/hub/herald/session.py +++ b/hub/herald/session.py @@ -2,6 +2,7 @@ import os import sys import math import time +import errno import codecs import typing import asyncio @@ -273,6 +274,7 @@ class SessionManager: except OSError as e: # don't suppress CancelledError self.logger.error(f'{kind} server failed to listen on {host}:' f'{port:d} :{e!r}') + raise else: self.logger.info(f'{kind} server listening on {host}:{port:d}') @@ -282,8 +284,19 @@ class SessionManager: """ env = self.env host = env.cs_host() - if env.tcp_port is not None: - await self._start_server('TCP', host, env.tcp_port) + if env.tcp_port is None: + return + started = False + while not started: + try: + await self._start_server('TCP', host, env.tcp_port) + started = True + except OSError as e: + if e.errno is errno.EADDRINUSE: + await asyncio.sleep(3) + continue + raise + async def _close_servers(self, kinds): """Close the servers of the given kinds (TCP etc.)."""