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

TypeError: isinstance when azure-identity is not imported using Azure Service Bus transport #1947

Open
jesusgarmanz opened this issue Feb 27, 2024 · 1 comment

Comments

@jesusgarmanz
Copy link

I'm currently in process of upgrading these three packages:
kombu 5.1.0 -> 5.3.5
celery 5.1.2 -> 5.3.6
azure-servicebus 7.8.1 -> 7.11.4

But found that Celery was throwing a TypeError when it starts up:


  -------------- celery@620caf9bceb7 v5.3.6 (emerald-rush)
 --- ***** -----
 -- ******* ---- Linux-6.6.12-linuxkit-x86_64-with 2024-02-26 15: 36: 17
 - *** --- * ---
 - ** ---------- [config]
 - ** ---------- .> app:         my_app: 0x7ffffdc97370
 - ** ---------- .> transport:   azureservicebus://RootManageSharedAccessKey:my_key@my_env
 - ** ---------- .> results:     redis://redis:6379/
 - *** --- * --- .> concurrency: 8 (prefork)
 -- ******* ---- .> task events: OFF (enable -E to monitor tasks in this worker)
 --- ***** -----
  -------------- [queues]
                 ...


 [tasks]
   ...

 Traceback (most recent call last):
   File "/root/.local/lib/python3.10/site-packages/celery/worker/worker.py", line 202, in start
     self.blueprint.start(self)
   File "/root/.local/lib/python3.10/site-packages/celery/bootsteps.py", line 116, in start
     step.start(parent)
   File "/root/.local/lib/python3.10/site-packages/celery/bootsteps.py", line 365, in start
     return self.obj.start()
   File "/root/.local/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 340, in start
     blueprint.start(self)
   File "/root/.local/lib/python3.10/site-packages/celery/bootsteps.py", line 116, in start
     step.start(parent)
   File "/root/.local/lib/python3.10/site-packages/celery/worker/consumer/connection.py", line 21, in start
     c.connection = c.connect()
   File "/root/.local/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 469, in connect
     conn = self.connection_for_read(heartbeat=self.amqheartbeat)
   File "/root/.local/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 475, in connection_for_read
     return self.ensure_connected(
   File "/root/.local/lib/python3.10/site-packages/celery/worker/consumer/consumer.py", line 526, in ensure_connected
     conn = conn.ensure_connection(
   File "/root/.local/lib/python3.10/site-packages/kombu/connection.py", line 406, in ensure_connection
     self._ensure_connection(*args, **kwargs)
   File "/root/.local/lib/python3.10/site-packages/kombu/connection.py", line 459, in _ensure_connection
     return retry_over_time(
   File "/root/.local/lib/python3.10/site-packages/kombu/utils/functional.py", line 318, in retry_over_time
     return fun(*args, **kwargs)
   File "/root/.local/lib/python3.10/site-packages/kombu/connection.py", line 934, in _connection_factory
     self._connection = self._establish_connection()
   File "/root/.local/lib/python3.10/site-packages/kombu/connection.py", line 860, in _establish_connection
     conn = self.transport.establish_connection()
   File "/root/.local/lib/python3.10/site-packages/kombu/transport/virtual/base.py", line 975, in establish_connection
     self._avail_channels.append(self.create_channel(self))
   File "/root/.local/lib/python3.10/site-packages/kombu/transport/virtual/base.py", line 953, in create_channel
     channel = self.Channel(connection)
   File "/root/.local/lib/python3.10/site-packages/kombu/transport/azureservicebus.py", line 136, in __init__
     self._try_parse_connection_string()
   File "/root/.local/lib/python3.10/site-packages/kombu/transport/azureservicebus.py", line 144, in _try_parse_connection_string
     if (isinstance(self._credential, DefaultAzureCredential) or
 TypeError: isinstance() arg 2 must be a type, a tuple of types, or a union

So, digging a little bit on azureservicebus.py file, it tries to import DefaultAzureCredential and ManagedIdentityCredential. If azure-identity package is not installed, kombu sets DefaultAzureCredential and ManagedIdentityCredential as vars with None value.

[...]
# line 72 to 77
try:
    from azure.identity import (DefaultAzureCredential,
                                ManagedIdentityCredential)
except ImportError:
    DefaultAzureCredential = None
    ManagedIdentityCredential = None
[...]

Then, on _try_parse_connection_string function after parsing the URI and getting the credential, kombu checks if the credential is an instance of DefaultAzureCredential and ManagedIdentityCredential.

[...]
# line 144 to 146
if (isinstance(self._credential, DefaultAzureCredential) or
           isinstance(self._credential, ManagedIdentityCredential)):
    return None
[...]

The issue here is when DefaultAzureCredential and ManagedIdentityCredential are set to None, Python can't check if self._credential is an instance of None and throws a TypeError. I don't know if this is intended or not, but I did some changes on my computer and found a easy way to test it maintaining the same import strategy:

# I set the conditions on variables to have more clarity about what is being checked
default_azure_credential = (DefaultAzureCredential is not None and
    isinstance(self._credential, DefaultAzureCredential))
managed_identity_credential = (ManagedIdentityCredential is not None and
    isinstance(self._credential, ManagedIdentityCredential))
if default_azure_credential or managed_identity_credential:
    return None

I could open a PR if this is some unexpected behavior.

@jesusgarmanz
Copy link
Author

jesusgarmanz commented Feb 27, 2024

Clarify that I am using the same configuration on Celery, Kombu and Service Bus before and after the upgrade. I also review that the check of the credential type was created on 5.3.3, so makes sense why it wasn't happening this to me before. Thanks in advance

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant