diff --git a/src/azure-cli-core/azure/cli/core/_profile.py b/src/azure-cli-core/azure/cli/core/_profile.py index 307147f03eb..3cfa1f9d859 100644 --- a/src/azure-cli-core/azure/cli/core/_profile.py +++ b/src/azure-cli-core/azure/cli/core/_profile.py @@ -18,7 +18,8 @@ from azure.cli.core._environment import get_config_dir from azure.cli.core._session import ACCOUNT -from azure.cli.core.util import get_file_json, in_cloud_console, open_page_in_browser, can_launch_browser +from azure.cli.core.util import get_file_json, in_cloud_console, open_page_in_browser, can_launch_browser,\ + is_windows, is_wsl from azure.cli.core.cloud import get_active_cloud, set_cloud_subscription from knack.log import get_logger @@ -1089,6 +1090,16 @@ def _get_authorization_code_worker(authority_url, resource, results): import random reply_url = None + + # On Windows, HTTPServer by default doesn't throw error if the port is in-use + # https://github.com/Azure/azure-cli/issues/10578 + if is_windows(): + logger.debug('Windows is detected. Set HTTPServer.allow_reuse_address to False') + ClientRedirectServer.allow_reuse_address = False + elif is_wsl(): + logger.debug('WSL is detected. Set HTTPServer.allow_reuse_address to False') + ClientRedirectServer.allow_reuse_address = False + for port in range(8400, 9000): try: web_server = ClientRedirectServer(('localhost', port), ClientRedirectHandler) diff --git a/src/azure-cli-core/azure/cli/core/util.py b/src/azure-cli-core/azure/cli/core/util.py index d5f2cf1b691..6fb489cf339 100644 --- a/src/azure-cli-core/azure/cli/core/util.py +++ b/src/azure-cli-core/azure/cli/core/util.py @@ -367,9 +367,9 @@ def sdk_no_wait(no_wait, func, *args, **kwargs): def open_page_in_browser(url): import subprocess import webbrowser - platform_name, release = _get_platform_info() + platform_name, _ = _get_platform_info() - if _is_wsl(platform_name, release): # windows 10 linux subsystem + if is_wsl(): # windows 10 linux subsystem try: return subprocess.call(['cmd.exe', '/c', "start {}".format(url.replace('&', '^&'))]) except OSError: # WSL might be too old # FileNotFoundError introduced in Python 3 @@ -395,16 +395,21 @@ def _get_platform_info(): return platform_name.lower(), release.lower() -def _is_wsl(platform_name, release): +def is_wsl(): platform_name, release = _get_platform_info() return platform_name == 'linux' and release.split('-')[-1] == 'microsoft' +def is_windows(): + platform_name, _ = _get_platform_info() + return platform_name == 'windows' + + def can_launch_browser(): import os import webbrowser - platform_name, release = _get_platform_info() - if _is_wsl(platform_name, release) or platform_name != 'linux': + platform_name, _ = _get_platform_info() + if is_wsl() or platform_name != 'linux': return True # per https://unix.stackexchange.com/questions/46305/is-there-a-way-to-retrieve-the-name-of-the-desktop-environment # and https://unix.stackexchange.com/questions/193827/what-is-display-0 diff --git a/src/azure-cli/HISTORY.rst b/src/azure-cli/HISTORY.rst index 4831cd368f3..c6cda4734e3 100644 --- a/src/azure-cli/HISTORY.rst +++ b/src/azure-cli/HISTORY.rst @@ -15,6 +15,7 @@ Release History * Polish error when running `az login -u {} -p {}` with Microsoft account * Polish `SSLError` when running `az login` behind a proxy with self-signed root certificate +* Fix #10578: `az login` hangs when more than one instances are launched at the same time on Windows or WSL **RBAC**