Skip to content

Commit 0f57d7a

Browse files
fix: address co-pilot suggestions
1 parent f6e769b commit 0f57d7a

1 file changed

Lines changed: 14 additions & 6 deletions

File tree

src/azure-cli-core/azure/cli/core/__init__.py

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@
3434
ALWAYS_LOADED_MODULES = []
3535
# Extensions that will always be loaded if installed. They don't expose commands but hook into CLI core.
3636
ALWAYS_LOADED_EXTENSIONS = ['azext_ai_examples', 'azext_next']
37+
# Timeout (in seconds) for loading a single module. Acts as a safety valve to prevent indefinite hangs
3738
MODULE_LOAD_TIMEOUT_SECONDS = 30
39+
# Maximum number of worker threads for parallel module loading.
3840
MAX_WORKER_THREAD_COUNT = 4
3941

4042

@@ -200,12 +202,13 @@ def _configure_style(self):
200202

201203

202204
class ModuleLoadResult: # pylint: disable=too-few-public-methods
203-
def __init__(self, module_name, command_table, group_table, elapsed_time, error=None):
205+
def __init__(self, module_name, command_table, group_table, elapsed_time, error=None, traceback_str=None):
204206
self.module_name = module_name
205207
self.command_table = command_table
206208
self.group_table = group_table
207209
self.elapsed_time = elapsed_time
208210
self.error = error
211+
self.traceback_str = traceback_str
209212

210213

211214
class MainCommandsLoader(CLICommandsLoader):
@@ -552,45 +555,50 @@ def _load_modules(self, args, command_modules):
552555
future_to_module = {executor.submit(self._load_single_module, mod, args): mod
553556
for mod in command_modules if mod not in BLOCKED_MODS}
554557

555-
for future in future_to_module:
558+
for future in concurrent.futures.as_completed(future_to_module):
556559
try:
557560
result = future.result(timeout=MODULE_LOAD_TIMEOUT_SECONDS)
558561
results.append(result)
559562
except concurrent.futures.TimeoutError:
560563
mod = future_to_module[future]
561564
logger.warning("Module '%s' load timeout after %s seconds", mod, MODULE_LOAD_TIMEOUT_SECONDS)
562-
future.cancel()
563565
results.append(ModuleLoadResult(mod, {}, {}, 0,
564566
Exception(f"Module '{mod}' load timeout")))
565567
except (ImportError, AttributeError, TypeError, ValueError) as ex:
566568
mod = future_to_module[future]
567569
logger.warning("Module '%s' load failed: %s", mod, ex)
568570
results.append(ModuleLoadResult(mod, {}, {}, 0, ex))
571+
except Exception as ex:
572+
mod = future_to_module[future]
573+
logger.warning("Module '%s' load failed with unexpected exception: %s", mod, ex)
574+
results.append(ModuleLoadResult(mod, {}, {}, 0, ex))
569575

570576
return results
571577

572578
def _load_single_module(self, mod, args):
573579
from azure.cli.core.breaking_change import import_module_breaking_changes
574580
from azure.cli.core.commands import _load_module_command_loader
581+
import traceback
575582
try:
576583
start_time = timeit.default_timer()
577584
module_command_table, module_group_table = _load_module_command_loader(self, args, mod)
578585
import_module_breaking_changes(mod)
579586
elapsed_time = timeit.default_timer() - start_time
580587
return ModuleLoadResult(mod, module_command_table, module_group_table, elapsed_time)
581588
except Exception as ex: # pylint: disable=broad-except
582-
return ModuleLoadResult(mod, {}, {}, 0, ex)
589+
tb_str = traceback.format_exc()
590+
return ModuleLoadResult(mod, {}, {}, 0, ex, tb_str)
583591

584592
def _handle_module_load_error(self, result):
585593
"""Handle errors that occurred during module loading."""
586-
import traceback
587594
from azure.cli.core import telemetry
588595

589596
logger.error("Error loading command module '%s': %s", result.module_name, result.error)
590597
telemetry.set_exception(exception=result.error,
591598
fault_type='module-load-error-' + result.module_name,
592599
summary='Error loading module: {}'.format(result.module_name))
593-
logger.debug(traceback.format_exc())
600+
if result.traceback_str:
601+
logger.debug(result.traceback_str)
594602

595603
def _process_successful_load(self, result):
596604
"""Process successfully loaded module results."""

0 commit comments

Comments
 (0)