azure-sdk-for-python: Unexpected program hang after exception is raised while iterating a generator

  • Package Name: azure-servicebus
  • Package Version: 7.4.0
  • Operating System: GNU/Linux
  • Python Version: 3.9.9

Strangely, the following program does not terminate:

from azure.identity import DefaultAzureCredential
from azure.servicebus import (
    ServiceBusClient,
    ServiceBusReceiveMode,
)


def receive():
    namespace = 'xxx'
    topic = 'yyy'
    subscription = 'zzz'

    credential = DefaultAzureCredential()
    client = ServiceBusClient(
        fully_qualified_namespace=f'{namespace}.servicebus.windows.net',
        credential=credential,
    )

    with client as client:
        receiver = client.get_subscription_receiver(
            topic_name=topic,
            subscription_name=subscription,
            receive_mode=ServiceBusReceiveMode.PEEK_LOCK,
        )
        with receiver as receiver:
            for message in receiver:
                yield message


def main():
    messages = receive()
    for message in messages:
        raise Exception('Terminate program.')


if __name__ == '__main__':
    main()

It prints the exception and hangs:

Traceback (most recent call last):
  File "/home/indy/dm/sb-experiment/./sbexperiment/app.py", line 40, in <module>
    main()
  File "/home/indy/dm/sb-experiment/./sbexperiment/app.py", line 36, in main
    raise Exception('Terminate program.')
Exception: Terminate program.

But it works as expected when I modify main() to this:

def main():
    for message in receive():
        raise Exception('Terminate program.')

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 1
  • Comments: 17 (11 by maintainers)

Most upvoted comments

Hi @Indy2222 and @pavelm10,

I apologize for the extreme delay in providing an update to this matter. We are currently working on moving azure-servicebus away from uamqp and on to a pure python based stack, which will address this issue. The preview is expected to be released in February. I’ll leave this issue open and update yall when its available on pypy.

Hey @Indy2222 - apologies for the late update! I’ll make sure to discuss this with the team this upcoming week and keep you posted on when we can address this. Thanks!

Hey @Indy2222 - Appreciate your feedback on this! We have a few other issues taking priority at the moment, but we plan to address this as soon as possible. Thank you!

hey @Indy2222 , I am able to reproduce the issue. this is an interesting issue – from the code itself I didn’t see a major difference. I tried to debug and execute one line by one line, and they are of the same execution order.

I honestly am not clear on what happens when error is raised and how python exits the program. I have the feeling that it is related to the keep_alive_thread in uamqp which blocks the program. I will do more investigation into this and try different approaches to see if this could be resolved (e.g. keep_alive_thread should be a daemon thread)

Are you blocked by this issue? I think adding a try except catch should help you bypass this hang issue.