Skip to content

Commit d02b59f

Browse files
feature: address threading race conditions concerns
1 parent 25d5d6a commit d02b59f

File tree

2 files changed

+15
-13
lines changed

2 files changed

+15
-13
lines changed

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

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -202,13 +202,14 @@ def _configure_style(self):
202202

203203

204204
class ModuleLoadResult: # pylint: disable=too-few-public-methods
205-
def __init__(self, module_name, command_table, group_table, elapsed_time, error=None, traceback_str=None):
205+
def __init__(self, module_name, command_table, group_table, elapsed_time, error=None, traceback_str=None, command_loader=None):
206206
self.module_name = module_name
207207
self.command_table = command_table
208208
self.group_table = group_table
209209
self.elapsed_time = elapsed_time
210210
self.error = error
211211
self.traceback_str = traceback_str
212+
self.command_loader = command_loader
212213

213214

214215
class MainCommandsLoader(CLICommandsLoader):
@@ -343,7 +344,7 @@ def _filter_modname(extensions):
343344
# from an extension requires this map to be up-to-date.
344345
# self._mod_to_ext_map[ext_mod] = ext_name
345346
start_time = timeit.default_timer()
346-
extension_command_table, extension_group_table = \
347+
extension_command_table, extension_group_table, _ = \
347348
_load_extension_command_loader(self, args, ext_mod)
348349
import_extension_breaking_changes(ext_mod)
349350

@@ -581,10 +582,10 @@ def _load_single_module(self, mod, args):
581582
import traceback
582583
try:
583584
start_time = timeit.default_timer()
584-
module_command_table, module_group_table = _load_module_command_loader(self, args, mod)
585+
module_command_table, module_group_table, command_loader = _load_module_command_loader(self, args, mod)
585586
import_module_breaking_changes(mod)
586587
elapsed_time = timeit.default_timer() - start_time
587-
return ModuleLoadResult(mod, module_command_table, module_group_table, elapsed_time)
588+
return ModuleLoadResult(mod, module_command_table, module_group_table, elapsed_time, command_loader=command_loader)
588589
except Exception as ex: # pylint: disable=broad-except
589590
tb_str = traceback.format_exc()
590591
return ModuleLoadResult(mod, {}, {}, 0, ex, tb_str)
@@ -602,6 +603,12 @@ def _handle_module_load_error(self, result):
602603

603604
def _process_successful_load(self, result):
604605
"""Process successfully loaded module results."""
606+
if result.command_loader:
607+
self.loaders.append(result.command_loader)
608+
609+
for cmd in result.command_table:
610+
self.cmd_to_loader_map[cmd] = [result.command_loader]
611+
605612
for cmd in result.command_table.values():
606613
cmd.command_source = result.module_name
607614

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,22 +1131,17 @@ def _load_command_loader(loader, args, name, prefix):
11311131
logger.debug("Module '%s' is missing `get_command_loader` entry.", name)
11321132

11331133
command_table = {}
1134+
command_loader = None
11341135

11351136
if loader_cls:
11361137
command_loader = loader_cls(cli_ctx=loader.cli_ctx)
1137-
loader.loaders.append(command_loader) # This will be used by interactive
11381138
if command_loader.supported_resource_type():
11391139
command_table = command_loader.load_command_table(args)
1140-
if command_table:
1141-
for cmd in list(command_table.keys()):
1142-
# TODO: If desired to for extension to patch module, this can be uncommented
1143-
# if loader.cmd_to_loader_map.get(cmd):
1144-
# loader.cmd_to_loader_map[cmd].append(command_loader)
1145-
# else:
1146-
loader.cmd_to_loader_map[cmd] = [command_loader]
11471140
else:
11481141
logger.debug("Module '%s' is missing `COMMAND_LOADER_CLS` entry.", name)
1149-
return command_table, command_loader.command_group_table
1142+
1143+
group_table = command_loader.command_group_table if command_loader else {}
1144+
return command_table, group_table, command_loader
11501145

11511146

11521147
def _load_extension_command_loader(loader, args, ext):

0 commit comments

Comments
 (0)