azure-sdk-for-python icon indicating copy to clipboard operation
azure-sdk-for-python copied to clipboard

TypeError: Received message 257:None is not bytes

Open SzilvasiPeter opened this issue 2 years ago • 6 comments

  • Package Name: azure-servicebus
  • Package Version: 7.12.2
  • Operating System: Linux
  • Python Version: 3.10

Describe the bug We have deployed a service on an OpenShift cluster that ingests data from an Azure Service Bus resource. However, it throws the following error three times and then stops throwing it:

2024-06-28 11:29:32,258.258 ERROR _pyamqp_transport - create_servicebus_exception: Unexpected error occurred (TypeError('Received message 257:None is not bytes')). Handler shutting down.
Traceback (most recent call last):
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/aio/_base_handler_async.py", line 260, in _do_retryable_operation
    return await operation(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/aio/_servicebus_receiver_async.py", line 451, in _receive
    receiving = await amqp_receive_client.do_work_async()
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_client_async.py", line 352, in do_work_async
    return await self._client_run_async(**kwargs)
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_client_async.py", line 760, in _client_run_async
    await self._connection.listen(wait=self._socket_timeout, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_connection_async.py", line 794, in listen
    if await self._read_frame(wait=wait, **kwargs):
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_connection_async.py", line 276, in _read_frame
    new_frame = await self._transport.receive_frame(timeout=timeout, **kwargs)
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_transport_async.py", line 76, in receive_frame
    header, channel, payload = await asyncio.wait_for(
  File "/usr/local/lib/python3.10/asyncio/tasks.py", line 445, in wait_for
    return fut.result()
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_transport_async.py", line 98, in read
    await self._read(8, buffer=frame_header, initial=True)
  File "/usr/local/lib/python3.10/site-packages/azure/servicebus/_pyamqp/aio/_transport_async.py", line 532, in _read
    data = await self.sock.receive_bytes()
  File "/usr/local/lib/python3.10/site-packages/aiohttp/client_ws.py", line 296, in receive_bytes
    raise TypeError(f"Received message {msg.type}:{msg.data!r} is not bytes")
TypeError: Received message 257:None is not bytes

There is also another kind of error in addition to the previous error:

2024-06-28 11:25:58,245.245 ERROR client - __del__: Unclosed client session client_session: <aiohttp.client.ClientSession object at 0x7f6d906d4a60>

We were catching the errors and kept reinitializing the ServiceBusReceiver and it is an okay workaround.

To Reproduce Steps to reproduce the behavior:

  1. Initializing the Service Bus with connection string and HTTP proxy and starting listening on a specific topic and subscription.
# main.py
    http_proxy = {
        "proxy_hostname": http_settings.proxy_hostname,
        "proxy_port": http_settings.proxy_port,
    }

    async with ServiceBusClient.from_connection_string(
        conn_str=message_bus_settings.connection_string, http_proxy=http_proxy
    ) as service_bus_client:
        client = AzureServiceBus(service_bus_client, handle_storage_event)
        asyncio.create_task(
            client.listen(
                message_bus_settings.topic,
                message_bus_settings.subscription,
                message_bus_settings.max_message_count, # The max message count is 400
                message_bus_settings.max_wait_time, # The max wait time is 60
            )
        )
# azure_servicebus.py
class AzureServiceBus(Servicebus):
    def __init__(
        self, client: ServiceBusClient, handler: Callable[[StorageEventDto], Responses]
    ) -> None:
        self._client = client
        self._handler = handler

    async def listen(
        self, topic: str, subscription: str, max_msg_cnt: int, max_wait_time: float
    ) -> None:
        while True:
            await self.receive_events(topic, subscription, max_msg_cnt, max_wait_time)

    async def receive_events(
        self, topic: str, subs: str, max_msg_cnt: int, max_wait_time: float
    ) -> None:
        try:
            async with self._client.get_subscription_receiver(topic, subs) as recv:
                logger.info("The AzureServiceBus receiver is initialized.")
                messages = await recv.receive_messages(max_msg_cnt, max_wait_time)
                for msg in messages:
                    message: dict[str, Any] = json.loads(str(msg))
                    self._handler(StorageEventDto(**message))
                    await recv.complete_message(msg)
        except Exception as ex:
            logger.error(f"Error during receiving: {ex}")
  1. Deploying a FastAPI service (that receives messages from the Service Bus) to an OpenShift cluster

Expected behavior I expect to initialize the ServiceBusReceiver without encountering an error. So that the workaround can be omitted.

Additional context Locally, I haven't encountered this error.

SzilvasiPeter avatar Jun 28 '24 12:06 SzilvasiPeter

Thank you for the feedback @SzilvasiPeter . We will investigate and get back to you asap.

kashifkhan avatar Jun 28 '24 13:06 kashifkhan

We are having the same error recently, with pretty much the same implementation in this issue. Any update on this please? Thanks very much

fwanghe avatar Oct 06 '24 20:10 fwanghe

I'm also seeing this issue, an update would be great. Thank you.

liamjobrien avatar Oct 09 '24 10:10 liamjobrien

We have merged the fix for this into main and will have it available in our next release cadence.

kashifkhan avatar Jan 15 '25 21:01 kashifkhan

Hi @SzilvasiPeter. Thank you for opening this issue and giving us the opportunity to assist. We believe that this has been addressed. If you feel that further discussion is needed, please add a comment with the text "/unresolve" to remove the "issue-addressed" label and continue the conversation.

github-actions[bot] avatar Jan 15 '25 21:01 github-actions[bot]

Hi @SzilvasiPeter, since you haven’t asked that we /unresolve the issue, we’ll close this out. If you believe further discussion is needed, please add a comment /unresolve to reopen the issue.

github-actions[bot] avatar Jan 22 '25 22:01 github-actions[bot]