-
Notifications
You must be signed in to change notification settings - Fork 493
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Bug Report] NullReferenceException in AuthenticationWithTokenRefresh #1911
Comments
Thanks for reporting this @its-phil . The debug assert referenced asserts that the lock obtained during token generation is valid. You hitting a Could you explain a bit about this operation that you're carrying out:
Do you mean that you call Also, do you hit this |
Thanks. So here's the normal sequence of operation I'd like to achieve: Preconditions:
So far, this procedure works as expected. All instances can be connected correctly. Now the preconditions change:
After steps 1 through 3 ran successfully at least once the device can communicate with the Edge even if both are not connected to the internet any longer. This is where the example I posted comes in: with the example I simulated that the device has an error in its stored IoT Hub address. Note that in the example I omitted the DPS connection attempt because it wouldn't work in the offline case anyway. I admit, this scenario it's a bit artificial, but I wanted to see what happens anyway. I am expecting an error because of the invalid IoT Hub address, so the behavior I observed is not a real issue for me. But the |
Thank you for the detailed explanation @its-phil . You are correct, connecting to an invalid IoT Hub endpoint should have returned an exception of type |
@its-phil I have not been able to reproduce the above scenario. If I connect to an invalid IoT Hub endpoint, I get a socket exception saying that the host couldn't be resolved. On connecting to a different IoT hub endpoint returns an exception that says that the device registration couldn't be found (which isn't the scenario that you mentioned, but I just wanted to point it out). Would you be able to share the complete device logs for your scenario: https://github.com/Azure/azure-iot-sdk-csharp/tree/master/tools/CaptureLogs |
Also, in my tests I've been connecting directly to hub, without any gateway in between (even though the sample uses localhost as your local gateway): https://github.com/its-phil/GatewayTest/blob/master/Program.cs#L36. |
@abhipsaMisra Thanks for testing.
That's what I would have expected, too. I attached a trace log. Does that help you? Also, just FYI: I run the gateway on my local machine using iotedgehubdev. That's why my gateway hostname is localhost.
I ran the test program without the gateway hostname parameter in https://github.com/its-phil/GatewayTest/blob/master/Program.cs#L36 (i.e. trying to connect to the IoT Hub directly once more after trying to do so in L24). The program doesn't crash with a NullReferenceException. Instead, I get the following (understandable) exception:
|
Thanks for the confirmation @its-phil ; it looks like the gateway hostname route is the one that is broken here. I'll try to repro this with a gateway in between and investigate it. |
I believe I was able to reproduce the issue that you're hitting: The In addition, there is another issue wherein reusing a token refresh enabled authentication method can result in the client not refreshing the sas tokens properly, I'll put a fix out for this as well.
|
@abhipsaMisra Thanks for investigating this issue. I haven't seen the token-refresh issue you mentioned yet. If the time to live is one hour, there's a good chance I haven't tested my artificial scenario long enough. Could you clear up one thing, please? I understood that the DeviceClient tries to refresh its token after a while (depending on the time-to-live). I assume the DeviceClient negotiates this token refresh with the gateway when it is connected to the gateway. Does the gateway have to have an online connection to the IoT Hub for token refresh to succeed? |
@its-phil For a regular device client - IoT hub connection, yes, as you've said that the client will negotiate the token with the hub service and there needs to be an active connection. As for the scenario when you have a gateway in between, I believe that will depend on how the gateway has been designed. If the gateway that you're using is IoT Edge, I believe that it does support offline capabilities for a connected client. |
Thanks, yes, the gateway is an Azure IoT Edge. |
The Edge team has confirmed that reauthentication can happen offline. |
Thank you very much for your support! |
@abhipsaMisra Thanks for the hint. As a matter of fact, that's what I already do to work around this. |
This fix has been released in our latest release: https://github.com/Azure/azure-iot-sdk-csharp/releases/tag/2021-8-11 |
Context
Description of the issue
I was testing the DeviceClient connection using TPM for attestation when I stumbled across a
NullReferenceException
.The desired sequence is:
DeviceClient
instance for direct to the IoT Hub (no gateway).DeviceClient
and connect again, this time via an Edge gateway.If the IoT hub address is valid it's all good. However, if for some reason the IoT Hub address is invalid (e.g. not reachable), i get said
NullReferenceException
in the second step.Code sample exhibiting the issue
See this repo for a demo: https://github.com/its-phil/GatewayTest
I debugged into the SDK code and found that the exception occurs in
AuthenticationWithTokenRefresh.cs
in line 78 (Debug.Assert(_lock != null);
).Am I using the SDK wrong or might this be an issue in the SDK?
Console log of the issue
The text was updated successfully, but these errors were encountered: