This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

CC3220SF: SDK 5.1 Prob With MQTT_IF_Publish,Contiuesly blocking if Wifi Disconnected.

Part Number: CC3220SF

We are developing MQTT Client App using Example PRovided in SDK 5.1.

"MQTT_IF_Publish" function blocks infinitely if function is called and then Wifi or Internet is Disconnected.

We are receiving Event of WIFI Disconnection as well as MQTT_EVENT_SERVER_DISCONNECT indicating Server Disconnection but Function doesn't return and keeps blocking thread.

We want to reconnect With Wifi and MQTT Broker but as Thread is blocked our app gets stuck.

Similar issue like below question,

https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/945109/cc3235sf-device-gets-stuck-when-mqttclient_publish-is-called-in-defined-scenario/3492606?tisearch=e2e-sitesearch&keymatch=MQTT_IF_Publish#3492606

  • After Further Investigation into Problem I found problem in below mentioned code,

    //*****************************************************************************
    //
    //! \brief Wait for the response (callback) from the server (network)
    //
    //*****************************************************************************
    int32_t _sl_ExtLib_ServerAckLockedWait(MQTTClient_Ctx_t *clientCtx)
    {
        int32_t rc = 0;
    
        MQTTClient_mutexLock();
        if (true == clientCtx->blockingSend)
        {
            MQTTClient_mutexUnlock();
            MQTTClient_ackRxSignalWait(clientCtx->ackSyncObj);
            MQTTClient_mutexLock();
            rc = (MQTT_DISCONNECT == clientCtx->awaitedAck) ? MQTT_PACKET_ERR_NOTCONN : 0;
        }
    
        MQTTClient_mutexUnlock();
    
        return rc;
    }

    Here Code stucks at Line

    It cant Lock into Mutex.

  • MQTT Library Calls MQTTClient_delete Function which also Destroys Mutex object and above mentioned function is still waiting for Mutex to get lock.

  • Hi,

    It might be the case that there is a corner case within the MQTT library such that if you attempted to perform an MQTT_IF_Publish operation but lost internet connectivity during that operation you will end up getting stuck.

    Like Kobi suggests in the other thread you linked, you should modify the MQTT library to implement a check to ensure that the the device is still connected to the network before attempting to lock that mutex.

    Regards,
    Michael

  • I have modified mqtt_if.c as suggested now function returns with error and doesn't block but now i am facing new problem.

    Original Code,

    int MQTT_IF_Publish(MQTTClient_Handle mqttClientParams, char* topic, char* payload, unsigned short payloadLen, int flags)
    {
        pthread_mutex_lock(mMQTTContext.moduleMutex);
        if(mMQTTContext.moduleState != MQTT_STATE_CONNECTED){
            LOG_ERROR("not connected to broker\r\n");
            pthread_mutex_unlock(mMQTTContext.moduleMutex);
            return -1;
        }
        pthread_mutex_unlock(mMQTTContext.moduleMutex);
    
        return MQTTClient_publish(mqttClientParams,
                                  (char*)topic,
                                  strlen((char*)topic),
                                  (char*)payload,
                                  payloadLen,
                                  flags);
    }

    Modified,

    int MQTT_IF_Publish(MQTTClient_Handle mqttClientParams, char* topic, char* payload, unsigned short payloadLen, int flags)
    {
        pthread_mutex_lock(mMQTTContext.moduleMutex);
        if(mMQTTContext.moduleState != MQTT_STATE_CONNECTED){
            LOG_ERROR("not connected to broker\r\n");
            pthread_mutex_unlock(mMQTTContext.moduleMutex);
            return -1;
        }
    
        int ret = MQTTClient_publish(mqttClientParams,
                                  (char*)topic,
                                  strlen((char*)topic),
                                  (char*)payload,
                                  payloadLen,
                                  flags);
    
        pthread_mutex_unlock(mMQTTContext.moduleMutex);
    
        return ret;
    }

    When MQTT_IF_Publish fails and returns with error I have called LOG_ERROR to print Error on UART which now crashes program.

    After debugging i found LOG_ERROR function uses vsnprintf function which uses __TI_printfi function. __TI_printfi causes problem, if i remove format specified variables passed to LOG_ERROR function and pass just String then problem doesn't occur.

    Problem When,

    LOG_ERROR("Publishing Config Message Failed,Topic:%s %d\r\n",ret,Topic_Pub);

    No Problem When,

    LOG_ERROR("Publishing Config Message Failed\r\n");

  • Hi,

    It is a bit strange that the LOG_ERROR function seems to interact poorly with your code edits. The vsnprintf function should be fine to use in the thread context of the MQTT_IF_Publish function.

    How are you building your code changes? Are you rebuilding the entire MQTT library, or are you simply copying in the new mqtt_if.c into your workspace to build as a part of your application? Perhaps the library is being compiled with no printf support.

    If you remove that LOG_ERROR call, are you able to have the function run successfully?

    Regards,

    Michael

  • I am not Rebuilding Entire MQTT Library i am just using it.I have imported MQTT Example and made minor change in MQTT_IF_Publish function as it was causing issue.

    Currently as a Workaround i have removed Arguments Passed to LOG_ERROR function it is working.

    Problem When,

    LOG_ERROR("Publishing Config Message Failed, Topic:%s %d\r\n",ret,Topic_Pub);

    No Problem When,

    LOG_ERROR("Publishing Config Message Failed\r\n");

  • Hi,

    Have you checked your RTOS stack and heap, to ensure you are not running out of memory or experiencing a stack overflow?

    Regards,

    Michael

  • I have checked and i am not running out of memory or stack overflow.

  • Hi,

    As mentioned before, in general there shouldn't be an issue with calling LOG_ERROR or any other vsnprintf based function in that MQTT_IF_Publish() function.

    Now, are you satisfied with the workaround of not passing in arguments to the LOG_ERROR call, so as to avoid the vsnprintf command? Debugging the root cause will likely not be simple, as I do not see any obvious reasons why that function call would fail.

    Regards,

    Michael

  • Currently i am concerned about My Project and am satisfied with workaround.

    Still if You could suggest me than i will look for reason behind problem as Another concern is what if it happens again in my Project. 

  • Hi,

    I hope the changes in MQTT_IF_Publish is final and solves the issue completely. I have tested this fix with QOS 2 messages and never faced any problems. Despite identifying this bug 9 months ago, I am wondering why TI haven't included this in their SDK.

    Regards,

    Zac