Azure/azure-iot-cli-extension

az iot hub monitor-events fails with ImportError uamqp

herckens opened this issue · 2 comments

Describe the bug

Command Name
az iot hub monitor-events Extension Name: azure-iot. Version: 0.11.0.

Errors:

The command failed with an unexpected error. Here is the traceback:
cannot import name 'c_uamqp' from partially initialized module 'uamqp' (most likely due to a circular import) (/Users/taaerhe4/.azure/cliextensions/azure-iot/uamqp/__init__.py)
Traceback (most recent call last):
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/knack/cli.py", line 231, in invoke
    cmd_result = self.invocation.execute(args)
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 657, in execute
    raise ex
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 720, in _run_jobs_serially
    results.append(self._run_job(expanded_arg, cmd_copy))
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 691, in _run_job
    result = cmd_copy(params)
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/azure/cli/core/commands/__init__.py", line 328, in __call__
    return self.handler(*args, **kwargs)
  File "/usr/local/Cellar/azure-cli/2.30.0_1/libexec/lib/python3.10/site-packages/azure/cli/core/commands/command_operation.py", line 121, in handler
    return op(**command_args)
  File "/Users/taaerhe4/.azure/cliextensions/azure-iot/azext_iot/operations/hub.py", line 2942, in iot_hub_monitor_events
    _iot_hub_monitor_events(
  File "/Users/taaerhe4/.azure/cliextensions/azure-iot/azext_iot/operations/hub.py", line 3055, in _iot_hub_monitor_events
    from azext_iot.monitor.builders import hub_target_builder
  File "/Users/taaerhe4/.azure/cliextensions/azure-iot/azext_iot/monitor/builders/hub_target_builder.py", line 8, in <module>
    import uamqp
  File "/Users/taaerhe4/.azure/cliextensions/azure-iot/uamqp/__init__.py", line 12, in <module>
    from uamqp import c_uamqp  # pylint: disable=import-self
ImportError: cannot import name 'c_uamqp' from partially initialized module 'uamqp' (most likely due to a circular import) (/Users/taaerhe4/.azure/cliextensions/azure-iot/uamqp/__init__.py)

To Reproduce:

Steps to reproduce the behavior. Note that argument values have been redacted, as they may contain sensitive information.

  • az iot hub monitor-events --hub-name {} --output {}

Expected Behavior

Display IoT Hub events.

Environment Summary

macOS-11.6-x86_64-i386-64bit, Darwin 20.6.0
Python 3.10.0
Installer: HOMEBREW

azure-cli 2.30.0

Extensions:
azure-iot 0.11.0

Additional Context

This used to work until a few days ago.

At some point, when running the command, I got this prompt, maybe that has an influence:

Dependency update (uamqp 1.2) required for IoT extension version: 0.11.0. 
Continue? (y/n) -> y

Maybe related: Trying to locally run a .NET Azure Function with IoT Hub trigger produces a similar looking error since about the same time:

The listener for function 'IoTHub_EventHub1' was unable to start. Microsoft.Azure.EventHubs.Processor: Encountered error while fetching the list of EventHub PartitionIds. Microsoft.Azure.Amqp: Operation is not valid due to the current state of the object.

There is a similar issue posted here Azure/azure-uamqp-python#281 in the uamqp repo. The fact that it used to work is a good data point. For amqp based commands, there is a --repair flag which will try to reinstall uamqp with uamqp~=1.2 in the IoT extension directory.

Because uamqp is a cython based package that wraps a C implementation, there needs to be an already available compatible pre-compiled distribution or it needs to be built for the target environment. If you updated azure-cli, and the azure-cli Python runtime changed (hypothetically from 3.6.x to 3.10.x) then you may run into issues using a uamqp version compiled for Py 3.6.x. Note the extension does not update when azure-cli is updated.

Also we have recently soft released v0.12.1 of the azure-iot extension. You can download & install or install via az extension add --source https://github.com/Azure/azure-iot-cli-extension/releases/download/v0.12.1/azure_iot-0.12.1-py3-none-any.whl. This version should support Py 3.10 on x86/x64 machine architecture.

Closing issue. We've had multiple users being able to solve this problem by running the command with --repair on a recent extension version (0.12.1+) to force a re-install of uamqp. Let us know if you are still having trouble.