Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extended logging to RESTManager.site.start() call #7451

Merged
merged 5 commits into from
Jun 1, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading