Skip to content

Commit

Permalink
Merge pull request #7451 from kozlovsky/log_restapi_start
Browse files Browse the repository at this point in the history
Add extended logging to RESTManager.site.start() call
  • Loading branch information
kozlovsky committed Jun 1, 2023
2 parents 9c2a7da + 5d75982 commit 3d88f2d
Showing 1 changed file with 34 additions and 9 deletions.
43 changes: 34 additions & 9 deletions src/tribler/core/components/restapi/rest/rest_manager.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
import logging
import ssl
import traceback
Expand All @@ -20,6 +21,11 @@
from tribler.core.utilities.process_manager import get_global_process_manager
from tribler.core.version import version_id


SITE_START_TIMEOUT = 5.0 # seconds
BIND_ATTEMPTS = 10


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -137,17 +143,24 @@ async def start(self):
await self.site.start()
else:
self._logger.info(f"Searching for a free port starting from {api_port}")
bind_attempts = 0
while bind_attempts < 10:
for port in range(api_port, api_port + BIND_ATTEMPTS):
try:
port = api_port + bind_attempts
self.site = web.TCPSite(self.runner, self.http_host, port,
shutdown_timeout=self.shutdown_timeout)
await self.site.start()
self.set_api_port(port)
await self.start_http_site(port)
break
except OSError:
bind_attempts += 1

except asyncio.TimeoutError:
self._logger.warning(f"Timeout when starting HTTP REST API server on port {port}")

except OSError as e:
self._logger.warning(f"{e.__class__.__name__}: {e}")

except BaseException as e:
self._logger.error(f"{e.__class__.__name__}: {e}")
raise # an unexpected exception; propagate it

else:
raise RuntimeError("Can't start HTTP REST API on any port in range "
f"{api_port}..{api_port + BIND_ATTEMPTS}")

self._logger.info("Started HTTP REST API: %s", self.site.name)

Expand All @@ -165,6 +178,18 @@ async def start(self):
await self.site_https.start()
self._logger.info("Started HTTPS REST API: %s", self.site_https.name)

async def start_http_site(self, port):
self.site = web.TCPSite(self.runner, self.http_host, port, shutdown_timeout=self.shutdown_timeout)
self._logger.info(f"Starting HTTP REST API server on port {port}...")

# The self.site.start() is expected to start immediately. It looks like on some machines,
# it hangs. The timeout is added to prevent the hypothetical hanging.
await asyncio.wait_for(self.site.start(), timeout=SITE_START_TIMEOUT)

self._logger.info(f"HTTP REST API server started on port {port}")
self.set_api_port(port)


async def stop(self):
self._logger.info('Stopping...')
if self.runner:
Expand Down

0 comments on commit 3d88f2d

Please sign in to comment.