Non-negative error code broker_init in mqtt_helper.c

Hi,

While debugging an issue with reconnecting to our Azure server after the network is lost for some time, I found that azure_iot_hub_connect() returned a non-negative error code that I didn't catch correctly since I had the condition if (err < 0) in my code. Through the logs, I found that the error was happening in mqtt_helper_connect() and more specifically in broker_init() which is called from client_connect(). I tried reconnecting before I was connected to the network, which resulted in an error in getaddrinfo(). This error, however, is returned as -err, which results in a positive error code, which invalidates the documentation of mqtt_helper_connect.

I updated my code to catch all errors (if (err != 0)), but still I wanted to raise this to see if this is indeed a bug or intentional.

Parents
  • Hi,

    Can you provide more information about your application?

    I updated my code to catch all errors (if (err != 0)), but still I wanted to raise this to see if this is indeed a bug or intentional.

    Are all errors fixed when you added (if(err!=0)) instead of (if(err<0))? Where exactly did you add this change to?

    Best regards,
    Dejan

  • Hi Dejan,

    Sorry if I was not clear. In my application I have a function that is called to connect to our Azure IoT Hub with err = azure_iot_hub_connect(cfg) from the azure_iot_hub library included in the sdk (I'm currently at ncs 2.5.2, but I see the specific piece of code I'm referring to still exists in the main branch). I initially checked if err < 0, in which case I rescheduled connecting to the network to a later moment. But I noticed that even though azure_iot_hub_connect failed, the rescheduling didn't happen. That's when I found the above that broker_init() returns a positive error code (since getaddrinfo already returns a negative error code that is reversed) that is propagated back to my err. Since I specifically checked for a negative error code, the lines responsible for rescheduling the task of connecting to the server never triggered, so changing my application code to check for any non-zero error code instead of only a negative error code fixed it for me. 

    But that doesn't take away that the documentation of mqtt_helper_connect indicates that it should return a negative error code, while this specific use case shows that it doesn't always do that.

    Best,

    Wout

Reply
  • Hi Dejan,

    Sorry if I was not clear. In my application I have a function that is called to connect to our Azure IoT Hub with err = azure_iot_hub_connect(cfg) from the azure_iot_hub library included in the sdk (I'm currently at ncs 2.5.2, but I see the specific piece of code I'm referring to still exists in the main branch). I initially checked if err < 0, in which case I rescheduled connecting to the network to a later moment. But I noticed that even though azure_iot_hub_connect failed, the rescheduling didn't happen. That's when I found the above that broker_init() returns a positive error code (since getaddrinfo already returns a negative error code that is reversed) that is propagated back to my err. Since I specifically checked for a negative error code, the lines responsible for rescheduling the task of connecting to the server never triggered, so changing my application code to check for any non-zero error code instead of only a negative error code fixed it for me. 

    But that doesn't take away that the documentation of mqtt_helper_connect indicates that it should return a negative error code, while this specific use case shows that it doesn't always do that.

    Best,

    Wout

Children
Related