No exception thrown when sending message to IOT Hub when there is no network connection

AJ 0 Reputation points
2024-04-18T06:39:52.7833333+00:00

I am using python sdk version 2.13.0 and I am using VS Code as editor on my ubuntu laptop.

Based from a similar problem raised here https://github.com/Azure/azure-iot-sdk-python/issues/441, when sending a message to IoT Hub when there is no internet connection, an exception must be raised. However, I do not receive an exception and similar to the link above, I am stuck in await iot_hub_device_client.send_message() until my internet connection is restored.

Basically, I am performing two tasks asynchronously. First task creates a message and adds the message to a message queue. While the other task publishes the message queue to the IoT Hub. But since there is no exception thrown when there is no internet connection, the first task cannot resume because the program gets stuck in await iot_hub_device_client.send_message(). I tried to simplify the code and just send a message directly to the IoT Hub every 1 second in a loop and the same issue occurs. It waits in the await iot_hub_device_client.send_message().

Azure IoT Hub
Azure IoT Hub
An Azure service that enables bidirectional communication between internet of things (IoT) devices and applications.
1,205 questions
{count} votes

2 answers

Sort by: Most helpful
  1. LeelaRajeshSayana-MSFT 16,281 Reputation points
    2024-04-18T21:29:09.6133333+00:00

    Hi @AJ Greetings! Welcome to Microsoft Q&A forum. Thank you for posting this question here.

    retry policy is not supported in Python

    Even though the Python SDK does not support Exponential back-off with jitter or Custom Retry, there is a retry policy configuration that can be passed through connection_retry_interval parameter of the IoTHubDeviceClient class. The default value for this is 10 seconds. We can change this to modify the attempts to re-establish a dropped connection.

    Please note that this setting does not effect the time out of the iot_hub_device_client.send_message() method. I do not see an option to override the time out setting for this method. It would not be advised to change this setting to 2 seconds as this would cause false exceptions in the cases where the device might still be connected but it takes longer time to process the message delivery due to poor network.

    One possible approach to consider for overcoming the issue is by creating an async method definition where you call the await iot_hub_device_client.send_message() and calling this method from a different line of code without using the await keyword. See if this helps. If you don't see a change in behavior of the code execution, consider using threads and have a thread work on queuing the messages while the other thread handles message delivery.

    Hope this helps. Please let us know if you need any additional assistance with this.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.