Azure EvenHub: Failed to initiate the connection due to exception: [Errno 9] Bad file descriptor

137 Views Asked by At

I have a single script that is supposed to send data to Azure Event Hub. However when I run the script I get the error message below for which I couldn't find any reference. The same script was running a couple of months ago. Thank you.

    Traceback (most recent call last):
      File "test.py", line 82, in <module>
        getAirThings()
      File "test.py", line 76, in getAirThings
        event_data_batch = producer.create_batch()
                                        ^^^^^^^^^^^^^^^^^^^^^^^
      File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer_client.py", line 740, in create_batch
        self._get_max_message_size()
      File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer_client.py", line 336, in _get_max_message_size
        )._open_with_retry()
         ^^^^^^^^^^^^^^^^^^
      File ".venv/lib/python3.11/site-packages/azure/eventhub/_producer.py", line 155, in _open_with_retry
        return self._do_retryable_operation(self._open, operation_need_param=False)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
      File ".venv/lib/python3.11/site-packages/azure/eventhub/_client_base.py", line 608, in _do_retryable_operation
        raise last_exception from None

    azure.eventhub.exceptions.ConnectError: Failed to initiate the connection due to exception: [Errno 9] Bad file descriptor
     Error condition: ErrorCondition.SocketError
     Error Description: Failed to initiate the connection due to exception: [Errno 9] Bad file descriptor

1

There are 1 best solutions below

0
datatalian On

I was able to solve this with Amqp. Here's the context of the problem. The script:

  1. Call an API

  2. Convert the returned data to JSON

  3. Create the EventHub producer with the connectionString and EventHub parameters. This is the part of the code where I added the transport_type parameter to solve the issue.

  4. Create a batch

  5. Add the API data output to the batch

  6. Send the event data to the Azure EventHub

    from azure.eventhub import EventHubProducerClient, EventData, TransportType
    
    producer = EventHubProducerClient.from_connection_string(
        conn_str = connectionString, 
        eventhub_name = eventHubName, 
        transport_type = TransportType.AmqpOverWebsocket
    )
    
    event_data = json.dumps(API_data)
    event_to_send = EventData(event_data)
    event_batch = producer.create_batch()
    event_batch.add(event_to_send)
    producer.send_batch(event_batch)