Skip to content

TypeError: Received message 257:None is not bytes #36280

Closed

Description

  • 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.

Activity

  1. added
    customer-reported
    needs-triage
    question
    on Jun 28, 2024
  2. kashifkhan commented on Jun 28, 2024

    @kashifkhan
    Member

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

  3. added
    Messaging
    and removed
    needs-triage
    on Jun 28, 2024
  4. added
    needs-team-attention
    on Jun 28, 2024
  5. fwanghe commented on Oct 6, 2024

    @fwanghe

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

  6. liamjobrien commented on Oct 9, 2024

    @liamjobrien

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

  7. kashifkhan commented on Jan 15, 2025

    @kashifkhan
    Member

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

  8. added
    issue-addressed
    on Jan 15, 2025
  9. removed
    needs-team-attention
    on Jan 15, 2025
  10. github-actions commented on Jan 15, 2025

    @github-actions
    Contributor

    Hi Peter Szilvasi (@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.

  11. github-actions commented on Jan 22, 2025

    @github-actions
    Contributor

    Hi Peter Szilvasi (@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.

  12. locked and limited conversation to collaborators on Apr 22, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

MessagingService Buscustomer-reportedissue-addressedquestion

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions