question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unable to execute direct_method calls when first connect() gets called before device is known in the IoT-Hub

See original GitHub issue

Context

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

logfile.txt

AB#7366702

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
BertKleeweincommented, May 26, 2020

fixed in azure-iot-device 2.1.2 – https://pypi.org/project/azure-iot-device/2.1.2/

1reaction
BertKleeweincommented, Apr 29, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Troubleshooting Azure IoT Hub error codes | Microsoft Learn
When trying to invoke a direct method from IoT Hub to a device, you may see that the request fails with the error...
Read more >
Azure IoT Hub — nRF Connect SDK 2.2.99 documentation
Connecting to Azure IoT Hub . After the initialization, calling the azure_iot_hub_connect() function connects the device to the configured IoT hub or DPS ......
Read more >
Azure IoT DeviceClient SDK demonstration, the basics
In this blog, we dive into what is offered by the Device SDKs. ... We start with the creation of a device registration...
Read more >
Azure IoT Hub | Dev Center - Electric Imp
Device instance's connectionString() method, get the Device Connection String and pass it into the AzureIoTHub.Client constructor. Using The Azure Portal. Use ...
Read more >
Microsoft Azure IOT Hub - SolarWinds Documentation
The total count of failed Cloud to Device method invocations by the device via PowerShell cmd-let. Unit: Count. Request size of direct method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found