Unable to execute direct_method calls when first connect() gets called before device is known in the IoT-Hub
See original GitHub issueContext
- OS and version used: <VERSION> (Ubuntu 18)
- Python version: <VERSION> (3.7.5)
- pip version: <VERSION> (pip 9.0.1)
- cloned repo: <VERSION> (azure-iot-device==2.1.0)
Description of the issue:
Unable to execute direct_method calls when first connect() gets called before device is known in the IoT-Hub. Steps to reproduce:
- make sure the device it not added yet to the IoT-Hub
- create the IoTDeviceClient object
- call connect() This will fail with a: CredentialError(‘Credentials invalid, could not connect’) caused by UnauthorizedError(‘Connection Refused: not authorised.’)
- Add the device to the IoT-Hub
- call connect() for the second time ON THE SAME INSTANCE of the IoTDeviceClient.
- connection succeeds --> async_clients(110): connect: Successfully connected to Hub
- Try to execute a direct method call -> This doens’t work (not in the web portal and not via the azure cli tool)
- Waited for the sas_token_update, but even after that, still not possible to send direct_method calls to the device.
Response when executing a direct mothod call directly from the IoTHub web portal: {“message”:“Device {"Message":"{\"errorCode\":404103,\"trackingId\":\"7af44c59b01e49539bb0dc91cc717798-G:21-TimeStamp:03/29/2020 10:47:23-G:6-TimeStamp:03/29/2020 10:47:23\",\"message\":\"Timed out waiting for device to subscribe.\",\"info\":{},\"timestampUtc\":\"2020-03-29T10:47:23.0351741Z\"}","ExceptionMessage":""} not registered”}
I have a task monitoring the IoTHubDeviceClient to recover from cases where the device is not known in the IoTHub for whatever reason. If the device gets again added to the IoT-Hub, after been removed by accident, we should be able to recover without rebooting. This isn’t the case for now I think. Connect indeed succeeds when the device is added again, but direct method calls are not working anymore. I know the SDK tries to reconnect on its own when the connection drops. But this is a monitoring task to try to cover all other cases where for instance a mistake is made in the cloud and the IoT-Devices get removed by accident. In that case, you want the device to recover on its own.
Code sample exhibiting the issue:
while True:
if not iot_hub_device_client.connected:
LOG.debug("iot_hub_device_client is not connected, call connect()")
# Connect IoTHubDeviceClient
try:
await iot_hub_device_client.connect()
except Exception as exception:
message = "%s: %s" % (type(exception).__name__, exception)
LOG.error(message)
else:
LOG.debug("iot_hub_device_client is still connected")
await asyncio.sleep(60)
Console log of the issue:
AB#7366702
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Top GitHub Comments
fixed in azure-iot-device 2.1.2 – https://pypi.org/project/azure-iot-device/2.1.2/
I don’t know what the version number will be, but it will be in the next release. I’m trying to figure out when that will be. Hopefully soon.