azure-sdk-for-python: Blocking SSLContext call when using azure-servicebus

  • Package Name: azure-servicebus
  • Package Version: 7.11.1 and main
  • Operating System: Linux
  • Python Version: 3.11.5

Describe the bug When using azure.servicebus.aio.ServiceBusClient() blocking calls to SSLContext .load_verify_locations() are made. Async libraries should not contain any blocking operations.

The call is made here: https://github.com/Azure/azure-sdk-for-python/blob/5ae05fc0a86e4b1cce5708791ba2ce9f7f31bac2/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/aio/_transport_async.py#L198 and here: https://github.com/Azure/azure-sdk-for-python/blob/5ae05fc0a86e4b1cce5708791ba2ce9f7f31bac2/sdk/servicebus/azure-servicebus/azure/servicebus/_pyamqp/aio/_transport_async.py#L232

To Reproduce

from azure.servicebus.aio import ServiceBusClient
client = ServiceBusClient.from_connection_string(...)
async with client:
    receiver = client.get_subscription_receiver(,,,)
    async with receiver:
        ...   # ^^ Observe that load_verify_locations() have been called

Expected behavior Async libraries should avoid making blocking calls. The blocking operations should be called using executors or threads.

Additional context For reference, this issue was discovered here: https://github.com/custom-components/zaptec/issues/116 . Home Assistant have a detector which warns about blocking operations.

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Comments: 19 (10 by maintainers)

Most upvoted comments

@sveinse we will allow folks to pass in their own ssl context so things will look similar to how asyncio handles it.

I confirm that the the fix for this issue resolves the issue when providing a custom ssl_context. Thank you very much.

…which is a part of PR #37702 . Thanks @swathipil, looking forward to the release (which allows a release in our end as well).

Hi @sveinse - We’ve merged the fix to main and it will be going out in our upcoming release. We’ll update this thread once it’s released. Thanks!

@kashifkhan I agree with @graingert, it would be better if the user may provide a SSLContext themselves.

@sveinse this change was inspired from sslproto in asyncio https://github.com/python/cpython/blob/7e7223e18f58ec48fb36a68fb75b5c5b7a45042a/Lib/asyncio/sslproto.py#L295.

Wouldn’t that cause the same warning to bubble up as well?

It would, but this code allows for supplying a user-provided SSLContext. By having that capability, the user may setup a SSLContext with ThreadPoolExcutor themselves. This is not supported in the eventhub implementation, unfortunately. If that were the case, the use can opt-out of the blocking IO.

I think it would be better to allow users to pass in a configured SSLContext then they can choose to build it in a thread if required

Hi @kashifkhan Thank you for looking at the issue.

Which of the two solutions will move forward? It seems this https://github.com/Azure/azure-sdk-for-python/pull/37655 is the preferred solution.

Please be advised that moving the blocking call to the constructor does not resolve the issue. It is completely ordinary to dynamically create a ServiceBusClient while async is running. If that’s the case, then the same issue of doing blocking calls within async will prevail.

/unresolve