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 1 commit
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
Prev Previous commit
Next Next commit
Raise RuntimeError if HTTP REST API can't start on any port in specif…
…ied range
  • Loading branch information
kozlovsky committed Jun 1, 2023
commit 380e95e996b74b68e53b40c94ce19a0720b6e754
14 changes: 10 additions & 4 deletions src/tribler/core/components/restapi/rest/rest_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
from tribler.core.utilities.process_manager import get_global_process_manager
from tribler.core.version import version_id


BIND_ATTEMPTS = 10


logger = logging.getLogger(__name__)


Expand Down Expand Up @@ -137,10 +141,9 @@ 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)
self._logger.info(f"Starting HTTP REST API server on port {port}...")
Expand All @@ -151,12 +154,15 @@ async def start(self):

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

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)

if self.config.https_enabled:
Expand Down