Azure / azure-iot-sdk-c

A C99 SDK for connecting devices to Microsoft Azure IoT services
https://azure.github.io/azure-iot-sdk-c
Other
588 stars 740 forks source link

MQTT failed to reconnect in Azure-SDK-C after SAS Token expiration #747

Closed TomBrennan-Eaton closed 4 years ago

TomBrennan-Eaton commented 5 years ago

Description of the issue:

I setup our code to let Azure try to automatically reconnect when we hit the SAS Token expiration, and Azure failed to reconnect on its own. People we discussed this with at MS believed that this should work; that also seems to be what is expected in resolved issue 613. Also, looking into the Azure code, the path to the error condition doesn’t seem possible, but there it is.

I looked into the Azure code to see if there was some option we could set that would prevent this problem and assist the retry operation, but I didn't find anything. What is most curious about the first Azure message (below in the log) is, that looking at the Azure code, it can't happen: OptionHandler_FeedOptions() is called from (I'm pretty sure): https://github.com/Azure/azure-iot-sdk-c/blob/master/iothub_client/src/iothubtransport_mqtt_common.c#L1986 which is line 1938 in release_2018_10_03. The debug output says the second argument is null, but that is checked 10 lines earlier, so shouldn't ever happen.

As a comment: searching for OptionHandler_FeedOptions in the Azure code turns up a pile of hits in the AMQP areas, and only one in the MQTT area. I don't know if that means the retry mechanism is underdeveloped for MQTT.

Code sample exhibiting the issue:

I arranged our code to set a much shorter SAS Token Lifetime to speed up the test; I set "sas_token_lifetime" to 304 seconds, so it would timeout in about 243 seconds. Then I had our application code not do anything when the ConnectionStatusCallback() gave us reasonCode = IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN, where our code had been reacting to this by disconnecting and reconnecting.

Console log of the issue:

Then I ran the application, connected to Azure (through our corporate proxy) and waited for the refresh. When it occurred, our code didn't react but kept trying to send messages. However, it is clear from the Azure low-level messages that it was failing to do the reconnect on its own; here's the mixed debug output, with the Azure messages highlighted:

Debug: Published 1 IOT_DATA_CHANNEL_REALTIMES_ITEM updates for device 16 Info: handleD2CMessageConfirmationCallback: Message Confirmation Received, Seq: 51

Info: handleConnectionStatusCallback: Connection Status Received, Result: 1, Reason: 0 Info: handleConnectionStatusCallback: Letting Azure reconnect, after SAS Token epxiration *Error: Time:Fri Nov 9 15:33:25 2018 File:../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0xb44153b0, void destinationHandle=(nil) Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/adapters/tlsio_openssl.c Func:tlsio_openssl_setoption Line:1605 failed feeding options to underlying I/O instance Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/uws_client.c Func:uws_client_set_option Line:2063 OptionHandler_FeedOptions failed Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/wsio.c Func:wsio_setoption Line:676 unable to OptionHandler_FeedOptions Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption Error: Time:Fri Nov 9 15:33:25 2018 File:.../azure-iot-sdk-c-release_2018_10_03/iothub_client/src/iothubtransport_mqtt_common.c Func:GetTransportProviderIfNecessary Line:1940 Failed feeding existing options to new TLS instance.**

Info: Update to IoT Status: IOT_CONNECTING, Error: IOT_NO_ERROR, Reason:
Debug: list_create: Creating connection list. Debug: setMessageProperties: Setting data message property 'a' = Realtimes Debug: setMessageProperties: Setting message property 'e' = json Debug: setMessageProperties: Setting message property 'p' = f4f802f8-64b4-50fe-a183-713e3aa73f5a Debug: iot_send: Message Sent, Seq: 52 Serialized Message: {"realtimes":[{"c":"596","t":1541795606,"t_ms":786,"v":"64.0781479"}]} Debug: CIoTDeviceStats::update_counter(): Message type = 1, size = 1024, 4k messages = 1 Debug: Published 1 IOT_DATA_CHANNEL_REALTIMES_ITEM updates for device 16

*Error: Time:Fri Nov 9 15:33:30 2018 File:.../azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0xb4413308, void destinationHandle=(nil)** ... and so on, without recovery

jebrando commented 5 years ago

@brennantj I'm would like to make sure we are on the same page as far as the issue you're seeing. I'll describe what I did to test this:

I took my test file which is a slightly modified iothub_ll_telemetry_sample (included below) that send 1 message every 2 seconds. I then changed the sas token lifetime to be 2 mins with the line below:

int lifetime = 2 * 60;
IoTHubDeviceClient_LL_SetOption(device_ll_handle, OPTION_SAS_TOKEN_LIFETIME, &lifetime);

After ~1:24 the device sends a DISCONNECT packet to the IoTHub server and then sends a CONNECT packet to reconnect:

Confirmation callback received for message 32 with result IOTHUB_CLIENT_CONFIRMATION_OK
-> 08:17:00 PUBLISH | IS_DUP: false | RETAIN: 0 | QOS: DELIVER_AT_LEAST_ONCE | TOPIC_NAME: devices/sample_key_dev/messages/events/ | PACKET_ID: 35 | PAYLOAD_LEN: 20
<- 08:17:00 PUBACK | PACKET_ID: 35
Confirmation callback received for message 33 with result IOTHUB_CLIENT_CONFIRMATION_OK
-> 08:17:01 DISCONNECT
The device client has been disconnected
-> 08:17:01 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: iot-test-sdk.azure-devices.net/sample_key_dev/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.2.10%20(native%3b%20WindowsProduct%3a0x00000004%206.2%3b%20x32) | PWD: XXXX | CLEAN: 0
<- 08:17:01 CONNACK | SESSION_PRESENT: true | RETURN_CODE: 0x0
The device client is connected to iothub
-> 08:17:01 SUBSCRIBE | PACKET_ID: 36 | TOPIC_NAME: devices/sample_key_dev/messages/devicebound/# | QOS: 1

I'm not sure where I'm misunderstanding the issue. Can you run my sample and edit it so I can get a repro of your issue.

Thanks, Jelani

sdk_full_sample.zip

yzhong94 commented 5 years ago

@jebrando The customer is using MQTT-WS + proxy setting, can we try it with these settings?

TomBrennan-Eaton commented 5 years ago

I should have said: we are using MQTT over Websockets for transport, through a corporate HTTP proxy server.

TomBrennan-Eaton commented 5 years ago

I tried the test program that Jelani sent, after adding the proxy settings and making a few convenience edits (see attached). iothub_ll_telemetry_sample.c.txt

I added it to the Azure build, in parallel to the iothub_ll_telemetry_sample folder, and it successfully builds. But when I run it on my target (a BeagleBone Black, with ARM Cortex A8), it does not successfully send messages (log output below).

The problem appears to be regarding the proxy settings; if I don't set a proxy, I don't get these complaints right away, but after ~15 seconds, I get wait_for_connection failed messages instead. I have it forced to port 8080 right now; if this gets resolved, I can generalize that.

Perhaps related: if I move the SetOption call for the proxy settings down to after the SetOption call for the SAS Timeout, then it fails immediately with a message Func:IoTHubTransport_MQTT_Common_SetOption Line:3096 Cannot set proxy option once the underlying IO is created. This order sensitivity strikes me as odd and perhaps something else is wrong.

So I'm stuck at the moment; not sure what to do to fix this.

The debug output with successfully set Proxy Options (the error messages appear right away):

~ $ /tmp/iothub_ll_mqtt_ws_connect
Creating IoTHub Device handle from version 1.2.10
Setting proxy options for http://proxy.etn.com:8080
Set SAS Token Lifetime to 120 sec; ready to send messages.

Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0x83500, void* destinationHandle=(nil)
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/adapters/tlsio_openssl.c Func:tlsio_openssl_setoption Line:1605 failed feeding options to underlying I/O instance
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/uws_client.c Func:uws_client_set_option Line:2063 OptionHandler_FeedOptions failed
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/wsio.c Func:wsio_setoption Line:676 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:23 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/iothub_client/src/iothubtransport_mqtt_common.c Func:GetTransportProviderIfNecessary Line:1940 Failed feeding existing options to new TLS instance.
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0x831b8, void* destinationHandle=(nil)
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/adapters/tlsio_openssl.c Func:tlsio_openssl_setoption Line:1605 failed feeding options to underlying I/O instance
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/uws_client.c Func:uws_client_set_option Line:2063 OptionHandler_FeedOptions failed
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/wsio.c Func:wsio_setoption Line:676 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:238 failure while trying to _SetOption
Error: Time:Mon Dec  3 13:38:24 2018 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2018_10_03/iothub_client/src/iothubtransport_mqtt_common.c Func:GetTransportProviderIfNecessary Line:1940 Failed feeding existing options to new TLS instance.
jebrando commented 5 years ago

@brennantj can you please get the new shared utility branch to give a little more information for the error.

TomBrennan-Eaton commented 5 years ago

Jelani: I can't do it until toward the end of the week. Do I need to update to the latest Azure release first, before taking your branch for shared utility?

On Mon, Feb 11, 2019 at 3:29 PM Jelani Brandon notifications@github.com wrote:

@brennantj https://github.com/brennantj can you please get the new shared utility branch https://github.com/Azure/azure-c-shared-utility/tree/setOption_test to give a little more information for the error.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-iot-sdk-c/issues/747#issuecomment-462482131, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5XsgFexOOMzESC-FXSfw3Za2i3o7N5ks5vMdKVgaJpZM4Y6nX8 .

jebrando commented 5 years ago

@brennantj You shouldn't have to update anything but the submodule. I just changed the logging line in file optionhandler.c Func:OptionHandler_FeedOptions Line:238:

LogError("failure while trying to _SetOption");

to

LogError("failure while trying to SetOption with option %s", option->name);

You can add this yourself if you'd like and see what option is giving you grief.

TomBrennan-Eaton commented 5 years ago

OK, that's a simple enough test. It'll still be a few days until I can.

On Mon, Feb 11, 2019 at 7:23 PM Jelani Brandon notifications@github.com wrote:

@brennantj https://github.com/brennantj You shouldn't have to update anything but the submodule. I just changed the logging line in file optionhandler.c Func:OptionHandler_FeedOptions Line:238:

LogError("failure while trying to _SetOption");

to

LogError("failure while trying to SetOption with option %s", option->name);

You can add this yourself if you'd like and see what option is giving you grief.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-iot-sdk-c/issues/747#issuecomment-462552691, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5XsnAoJ_Ai30IILjmEmmse7Qb2brCnks5vMgmbgaJpZM4Y6nX8 .

TomBrennan-Eaton commented 5 years ago

@jebrando I updated to release_2019_02_11 and applied your one-line patch above, but it's not succeeding in automatically refreshing the SAS Token for MQTT connections (unless I'm missing something). Here's my debug output, where I set our options before the connect:

Info: iot_open: IoT Hub Connection: HostName=EatonTestIothub1.azure-devices.net;DeviceId=9e2724fa-5634-5771-8e09-4ff16164213e;SharedAccessKey=...
Debug: iot_open: Connecting to IoT Hub using MQTT Websockets
Info: initializeConnection: Set the OPTION_SAS_TOKEN_LIFETIME to 273 seconds, and refresh time = 218
Info: initializeConnection: Setting proxy options for proxy.etn.com
Info: initializeConnection: Low-level Connection established now.
Info: iot_open: Created connection
Info: iot_open: Created message thread
Info: Provisional success opening IoT Connection.
Debug: Entering the run() loop
Debug: virtual void px::asio::CServerCore::ready(): Server core ready for requests.
Info: handleConnectionStatusCallback: Connection Status Received, Result: 0, Reason: 6
Info: handleConnectionStatusCallback: Created new thread for connectionStatusChanged
Info: void CIotDeviceClient::iot_connection_status_callback(IOT_DEVICE_HANDLE, IOTHUB_CLIENT_CONNECTION_STATUS, IOTHUB_CLIENT_CONNECTION_STATUS_REASON): Inside iot_connection_status_callback background thread.
Info: (Tentatively) Confirmed valid connection to IoT Hub.
Info: connectionStatusChangedThread: Exiting Connection Status Thread

and here are the Azure error messages that appear when the refresh happens about 4 minutes later; the first burst is at the :07 timestamp, then at :12 we attempt to send the next message and the next burst starts (I just showed three of the latter burst):

Info: handleConnectionStatusCallback: Connection Status Received, Result: 1, Reason: 0
Debug: handleConnectionStatusCallback: Got SAS Token epxiration notice; will automatically reconnect.
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0xb441b228, void* destinationHandle=(nil)
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option concreteOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option concreteOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/adapters/tlsio_openssl.c Func:tlsio_openssl_setoption Line:1606 failed feeding options to underlying I/O instance
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option underlying_io_options
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option concreteOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/uws_client.c Func:uws_client_set_option Line:2063 OptionHandler_FeedOptions failed
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option uWSClientOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/wsio.c Func:wsio_setoption Line:676 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option WSIOOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option concreteOptions
Error: Time:Tue Feb 19 11:24:07 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/iothub_client/src/iothubtransport_mqtt_common.c Func:GetTransportProviderIfNecessary Line:2109 Failed feeding existing options to new TLS instance.
Debug: list_create: Creating connection list.
Debug: setMessageProperties: Setting data message property 'a' = Realtimes
Debug: setMessageProperties: Setting message property 'e' = json
Debug: setMessageProperties: Setting message property 'p' = f4f802f8-64b4-50fe-a183-713e3aa73f5a
Debug: iot_send: Message Sent, Seq: 169
     Serialized Message: {"realtimes":[{"c":"596","t":1550593448,"t_ms":699,"v":"62.1733818"},{"c":"888","t":1550593445,"t_ms":443,"v":"2200.80444"},{"c":"915","t":1550593445,"t_ms":443,"v":"2179.49805"},{"c":"917","t":1550593445,"t_ms":443,"v":"2219.62085"}]}
Debug: CIoTDeviceStats::update_counter(): Message type = 1, size = 1676, 4k messages = 1
Debug: Published 4 IOT_DATA_CHANNEL_REALTIMES_ITEM updates for device 16
Error: Time:Tue Feb 19 11:24:12 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:225 invalid arguments OPTIONHANDLER_HANDLE handle=0xb4413d88, void* destinationHandle=(nil)
Error: Time:Tue Feb 19 11:24:12 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/xio.c Func:xio_setoption Line:187 unable to OptionHandler_FeedOptions
Error: Time:Tue Feb 19 11:24:12 2019 File:/home/tbrennan/src/stash/bbb/px-toolkit/vendor_source/build/azure/azure-iot-sdk-c-release_2019_02_11/c-utility/src/optionhandler.c Func:OptionHandler_FeedOptions Line:239 failure while trying to SetOption with option concreteOptions
TomBrennan-Eaton commented 5 years ago

Jelani: Could you keep me in the loop when there are changes or updates to be tested w.r.t. this issue? Do y'all have a timeframe for working on this?

On Mon, Feb 11, 2019 at 9:08 PM Tom Brennan brennantj@gmail.com wrote:

OK, that's a simple enough test. It'll still be a few days until I can.

On Mon, Feb 11, 2019 at 7:23 PM Jelani Brandon notifications@github.com wrote:

@brennantj https://github.com/brennantj You shouldn't have to update anything but the submodule. I just changed the logging line in file optionhandler.c Func:OptionHandler_FeedOptions Line:238:

LogError("failure while trying to _SetOption");

to

LogError("failure while trying to SetOption with option %s", option->name);

You can add this yourself if you'd like and see what option is giving you grief.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/Azure/azure-iot-sdk-c/issues/747#issuecomment-462552691, or mute the thread https://github.com/notifications/unsubscribe-auth/AC5XsnAoJ_Ai30IILjmEmmse7Qb2brCnks5vMgmbgaJpZM4Y6nX8 .

rafilho commented 5 years ago

Moving some of the Jelani issues to Chris.

CIPop commented 4 years ago

@brennantj I gave this a try with code from master and it doesn't appear to repro with either MQTT or MQTT-WS + Proxy. Either the issue was fixed since then or this is a timing issue.

For reference, my setup was:

One thing that I did notice is that, given the current design, the disconnects will cause some of the ACKs to be lost as IoT Hub sends them on a different TCP 5-tuple that was closed. Due to how MQTT QoS1 works, hub will not resend the ACKs and so the sample won't finish.

Output:

Creating IoTHub Device handle
Sending message 1 to IoTHub
Sending message 2 to IoTHub
Sending message 3 to IoTHub
Sending message 4 to IoTHub
Sending message 5 to IoTHub
-> 15:53:26 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: <HUB>.azure-devices.net/<DEVICE>/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.3.6%20(native%3b%20Linux%3b%20x86_64) | PWD: XXXX | CLEAN: 0
Sending message 6 to IoTHub
Sending message 7 to IoTHub
Sending message 8 to IoTHub
Sending message 9 to IoTHub
<- 15:53:34 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
The device client is connected to iothub
Sending message 10 to IoTHub
-> 15:53:36 DISCONNECT
The device client has been disconnected. Reason: IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN
-> 15:53:46 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: <HUB>.azure-devices.net/<DEVICE>/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.3.6%20(native%3b%20Linux%3b%20x86_64) | PWD: XXXX | CLEAN: 0
<- 15:53:55 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
The device client is connected to iothub
-> 15:53:57 DISCONNECT
The device client has been disconnected. Reason: IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN
-> 15:54:07 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: <HUB>.azure-devices.net/<DEVICE>/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.3.6%20(native%3b%20Linux%3b%20x86_64) | PWD: XXXX | CLEAN: 0
<- 15:54:15 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
The device client is connected to iothub
-> 15:54:17 DISCONNECT
The device client has been disconnected. Reason: IOTHUB_CLIENT_CONNECTION_EXPIRED_SAS_TOKEN
-> 15:54:27 CONNECT | VER: 4 | KEEPALIVE: 240 | FLAGS: 192 | USERNAME: <HUB>.azure-devices.net/<DEVICE>/?api-version=2017-11-08-preview&DeviceClientType=iothubclient%2f1.3.6%20(native%3b%20Linux%3b%20x86_64) | PWD: XXXX | CLEAN: 0
<- 15:54:35 CONNACK | SESSION_PRESENT: false | RETURN_CODE: 0x0
The device client is connected to iothub
-> 15:54:37 DISCONNECT

Closing for now - please re-activate if you can still repro this with the latest sources in master or the last release.

az-iot-builder-01 commented 4 years ago

@jebrando, @brennantj, @rafilho, @CIPop, thank you for your contribution to our open-sourced project! Please help us improve by filling out this 2-minute customer satisfaction survey

TomBrennan-Eaton commented 4 years ago

I haven't seen anything in the commit messages that suggested that this was fixed, though I had seen that it was worked on some time back. I'll retry and see what I see; I use a much shorter SAS Token expiration time (like 5 minutes, vs 60) to make it more obvious. Something that I recently learned, as I was looking into batching of messages ("AMQP always batches, MQTT never does") is that there appears to be considerable code to manage this for AMQP (which has no counterpart in MQTT transport code); this is a plus for MQTT when used in our constrained IoT devices, and I wouldn't want to see it affected by SAS refresh. (While they're not obviously related, I'm not sure that they aren't interconnected somehow, similar to your comment on ACKing messages when closing.)

CIPop commented 4 years ago

Thanks @brennantj! I actually used 5 and 10 seconds for SAS expiration when I tried to repro. There were a lot of commits in dependencies around XIO using sockets, TLS, and a large change for DNS handling. It's hard to tell what was causing the original issue just from logs. I'm not yet excluding the possibility of this being some sort of timing issue that requires your particular setup to easily repro. You're correct about the MQTT protocol and our usage of it - it's the one we recommend for constrained devices.