{"action":"created","issue":{"url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/698","repository_url":"https://api.github.com/repos/eclipse/paho.mqtt.c","labels_url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/698/labels{/name}","comments_url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/698/comments","events_url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/698/events","html_url":"https://github.com/eclipse/paho.mqtt.c/issues/698","id":476468942,"node_id":"MDU6SXNzdWU0NzY0Njg5NDI=","number":698,"title":"Deadlock when subscribe","user":{"login":"REBAUD","id":14061743,"node_id":"MDQ6VXNlcjE0MDYxNzQz","avatar_url":"https://avatars1.githubusercontent.com/u/14061743?v=4","gravatar_id":"","url":"https://api.github.com/users/REBAUD","html_url":"https://github.com/REBAUD","followers_url":"https://api.github.com/users/REBAUD/followers","following_url":"https://api.github.com/users/REBAUD/following{/other_user}","gists_url":"https://api.github.com/users/REBAUD/gists{/gist_id}","starred_url":"https://api.github.com/users/REBAUD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/REBAUD/subscriptions","organizations_url":"https://api.github.com/users/REBAUD/orgs","repos_url":"https://api.github.com/users/REBAUD/repos","events_url":"https://api.github.com/users/REBAUD/events{/privacy}","received_events_url":"https://api.github.com/users/REBAUD/received_events","type":"User","site_admin":false},"labels":[{"id":300532468,"node_id":"MDU6TGFiZWwzMDA1MzI0Njg=","url":"https://api.github.com/repos/eclipse/paho.mqtt.c/labels/bug","name":"bug","color":"fc2929","default":true}],"state":"open","locked":false,"assignee":{"login":"icraggs","id":10279016,"node_id":"MDQ6VXNlcjEwMjc5MDE2","avatar_url":"https://avatars1.githubusercontent.com/u/10279016?v=4","gravatar_id":"","url":"https://api.github.com/users/icraggs","html_url":"https://github.com/icraggs","followers_url":"https://api.github.com/users/icraggs/followers","following_url":"https://api.github.com/users/icraggs/following{/other_user}","gists_url":"https://api.github.com/users/icraggs/gists{/gist_id}","starred_url":"https://api.github.com/users/icraggs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icraggs/subscriptions","organizations_url":"https://api.github.com/users/icraggs/orgs","repos_url":"https://api.github.com/users/icraggs/repos","events_url":"https://api.github.com/users/icraggs/events{/privacy}","received_events_url":"https://api.github.com/users/icraggs/received_events","type":"User","site_admin":false},"assignees":[{"login":"icraggs","id":10279016,"node_id":"MDQ6VXNlcjEwMjc5MDE2","avatar_url":"https://avatars1.githubusercontent.com/u/10279016?v=4","gravatar_id":"","url":"https://api.github.com/users/icraggs","html_url":"https://github.com/icraggs","followers_url":"https://api.github.com/users/icraggs/followers","following_url":"https://api.github.com/users/icraggs/following{/other_user}","gists_url":"https://api.github.com/users/icraggs/gists{/gist_id}","starred_url":"https://api.github.com/users/icraggs/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/icraggs/subscriptions","organizations_url":"https://api.github.com/users/icraggs/orgs","repos_url":"https://api.github.com/users/icraggs/repos","events_url":"https://api.github.com/users/icraggs/events{/privacy}","received_events_url":"https://api.github.com/users/icraggs/received_events","type":"User","site_admin":false}],"milestone":null,"comments":6,"created_at":"2019-08-03T16:46:53Z","updated_at":"2019-08-07T13:10:40Z","closed_at":null,"author_association":"NONE","body":"Hello,\r\n\r\nI work on Linux 18.04 with the release 1.3.0.\r\nthe compilation is done with the parameters : \r\n\"cmake -GNinja -DPAHO_WITH_SSL=TRUE -DPAHO_BUILD_DOCUMENTATION=TRUE -DPAHO_BUILD_SAMPLES=TRUE --config Release ..\"\r\n\r\nDuring the use of the lib, i detect a deadlock when using the fonction MQTTAsync_subscribeMany\r\n\r\nThe function is call inside the connect cbk, you can find the implementation right below.\r\n\r\n```\r\nvoid onConnectionSuccess(void* context, char* cause)\r\n{\r\n    // Check if context not NULL\r\n    if(NULL != context)\r\n    {\r\n        // Set good context type\r\n        MQTT_Manager* MQTTManager = (MQTT_Manager*)context;\r\n\r\n        // Get internal value\r\n        // !!! Take Mutex !!!\r\n        bool verbose            = MQTTManager->getVerbose();\r\n        bool firstTime          = MQTTManager->getFirstTime();\r\n        bool needSubscription   = MQTTManager->getNeedSubscribtion();\r\n\r\n        // On first time, force disconneciton to connect well\r\n        if(firstTime)\r\n        {\r\n            if(verbose)\r\n            {\r\n                printf(\"[MQTT_Manager::onConnectionSuccess::%s] First connexion done => launch deconnexion\\n\", MQTTManager->getClientId().c_str());\r\n            }\r\n            //Force disconnection\r\n            MQTTManager->disconnect();\r\n        }\r\n        else\r\n        {\r\n            // Set state as connected\r\n            // !!! Take Mutex !!!\r\n            MQTTManager->setState(COMMUNICATION_MQTT_MANAGER_STATE_CONNECTED);\r\n            if(verbose)\r\n            {\r\n                printf(\"[MQTT_Manager::onConnectionSuccess::%s] Connection succesful (%s)\\n\", MQTTManager->getClientId().c_str(), cause);\r\n                fflush(stdout);\r\n            }\r\n\r\n            if(needSubscription)\r\n            {\r\n                MQTTManager->subscribe(verbose);\r\n            }\r\n        }\r\n    }\r\n    else\r\n    {\r\n        printf(\"[MQTT_Manager::onConnect] context can't be NULL\");\r\n    }\r\n}\r\n\r\n\r\nvoid MQTT_Manager::subscribe(bool verbose)\r\n{\r\n\r\n    // Get number of topics\r\n    uint16_t nbOfTopics = (uint16_t)m_options.topicsList.size();\r\n\r\n    // Init list of topics to subscribe\r\n    char** topicsPath  = (char**)calloc(nbOfTopics, sizeof(char*));\r\n    int* topicsQoS     = (int*)calloc(nbOfTopics, sizeof(int));\r\n    for(uint16_t i = 0; i < nbOfTopics; i++)\r\n    {\r\n        topicsPath[i]   = const_cast<char*>(m_options.topicsList.at(i).path.c_str());\r\n        topicsQoS[i]    = m_options.topicsList.at(i).qos;\r\n    }\r\n\r\n    if(verbose)\r\n    {\r\n        for(int i = 0; i < nbOfTopics; i++)\r\n        {\r\n            printf(\"test %d %s %d\\n \", i, topicsPath[i], topicsQoS[i]);\r\n        }\r\n        printf(\"[MQTT_Manager::subscribe::%s] Manager subsciption started...\\n\", m_options.clientId.c_str());\r\n    }\r\n\r\n    // Create subcribe parameters\r\n    MQTTAsync_responseOptions opts = MQTTAsync_responseOptions_initializer;\r\n    opts.onSuccess = onSubscribtionSuccess;\r\n    opts.onFailure = onSubscribtionFailure;\r\n    opts.context = this;\r\n\r\n    // Subscribe and disconnect if error\r\n    int subscribeResult = MQTTAsync_subscribeMany(m_client, (int)(nbOfTopics), topicsPath, topicsQoS, &opts);\r\n    if(MQTTASYNC_SUCCESS != subscribeResult)\r\n    {\r\n        printf(\"[MQTT_Manager::subscribe::%s] Manager subsciption error %d\\n\", m_options.clientId.c_str(), subscribeResult);\r\n        disconnect();\r\n    }\r\n    else\r\n    {\r\n        printf(\"[MQTT_Manager::subscribe::%s] Manager subsciption success\\n\", m_options.clientId.c_str());\r\n    }\r\n    \r\n    free((void*)topicsPath);\r\n    free(topicsQoS);\r\n}\r\n```\r\n\r\nAfter activate Trace, and adding print it seems that the issue is iside the funciton MQTTAsync_assignMsgId. The function block with the mutex.lock (deadLock?)\r\nThe issue is only reproductible with the lib in release and not in debug.\r\nI add 2 files of trace, one ok, and the other with the bug:\r\n[MQTTOkPrint.txt](https://github.com/eclipse/paho.mqtt.c/files/3464080/MQTTOkPrint.txt)\r\n[MQTTBugPrint.txt](https://github.com/eclipse/paho.mqtt.c/files/3464081/MQTTBugPrint.txt)\r\n\r\nIf you need more information, or other tests i stay available.\r\n\r\nThank you in advance for your help.\r\n\r\n\r\n\r\n"},"comment":{"url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/comments/519089453","html_url":"https://github.com/eclipse/paho.mqtt.c/issues/698#issuecomment-519089453","issue_url":"https://api.github.com/repos/eclipse/paho.mqtt.c/issues/698","id":519089453,"node_id":"MDEyOklzc3VlQ29tbWVudDUxOTA4OTQ1Mw==","user":{"login":"REBAUD","id":14061743,"node_id":"MDQ6VXNlcjE0MDYxNzQz","avatar_url":"https://avatars1.githubusercontent.com/u/14061743?v=4","gravatar_id":"","url":"https://api.github.com/users/REBAUD","html_url":"https://github.com/REBAUD","followers_url":"https://api.github.com/users/REBAUD/followers","following_url":"https://api.github.com/users/REBAUD/following{/other_user}","gists_url":"https://api.github.com/users/REBAUD/gists{/gist_id}","starred_url":"https://api.github.com/users/REBAUD/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/REBAUD/subscriptions","organizations_url":"https://api.github.com/users/REBAUD/orgs","repos_url":"https://api.github.com/users/REBAUD/repos","events_url":"https://api.github.com/users/REBAUD/events{/privacy}","received_events_url":"https://api.github.com/users/REBAUD/received_events","type":"User","site_admin":false},"created_at":"2019-08-07T13:10:40Z","updated_at":"2019-08-07T13:10:40Z","author_association":"NONE","body":"In the previous comment ,you will find the Trace when it happen.\r\nI'm also quite confused with the Trace because i don't see exactly where it deadlock.\r\nIf you need more information, you can tell me where a print is needed and i can provide it to you.\r\n\r\ni'm sure that each of my client is well destroy, each one in it's thread (because of my middleware)\r\ni call MQTTAsync_destroy(&m_client); for each\r\n\r\nI made the change on my side with the new mutex, and it seems good for my implementation.\r\n\r\nHave a very good day\r\n\r\nVincent "}}
{"actor":{"display_login":"REBAUD"}}