Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-112984

Qt MQTT crashes when disconnecting *without fully unsubscribing first*, after receiving a message

    XMLWordPrintable

Details

    • Bug
    • Resolution: Fixed
    • P1: Critical
    • 5.15.15
    • 5.15.13, 6.2.8
    • MQTT
    • Windows 22H2, MSVC 2019 x64
    • macOS, Windows
    • 9971e5795 (tqtc/lts-5.15)

    Description

      Code

      #include <QtMqtt>
      #include <QCoreApplication>
      
      int main(int argc, char *argv[])
      {
          QCoreApplication app(argc, argv);
      
          QString hostname = "broker.hivemq.com";
          quint16 port = 1883;
          QString topicText = "Hello";
          QString messageText = "World";
      
          auto client = new QMqttClient;
          client->setCleanSession(true);
          client->setHostname(hostname);
          client->setPort(port);
      
          qDebug() << "### Connecting to" << QString("%1:%2").arg(hostname).arg(port);
          client->connectToHost();
          QObject::connect(client, &QMqttClient::connected, qApp, [=]
          {
              qDebug() << "Connected";
              qDebug() << "### Subscribing to topic" << topicText;
              auto sub = client->subscribe(topicText, 1);
              QObject::connect(sub, &QMqttSubscription::stateChanged, qApp, [=](QMqttSubscription::SubscriptionState state)
              {
                  qDebug() << "Subscription state:" << state;
                  if (state == QMqttSubscription::Subscribed)
                  {
                      qDebug() << "### Publishing:" << QString("Topic=%1, Message=%2").arg(topicText, messageText);
                      client->publish(QMqttTopicName(topicText), messageText.toUtf8(), 1);
                  }
              });
      
              QObject::connect(sub, &QMqttSubscription::messageReceived, qApp, [=](QMqttMessage message)
              {
                  qDebug() << "Message received:" << message.payload();
      
                  qDebug("### Unsubscribing...");
                  sub->unsubscribe();
      
                  qDebug("### Disconnecting...");
                  client->disconnectFromHost();
      
                  qDebug("!!! I WILL CRASH NOW !!!");
              });
          });
          QObject::connect(client, &QMqttClient::disconnected, qApp, []
          {
              qDebug("Disconnected");
          });
      
          return app.exec();
      }
      
      

       

      Debug output

      15:49:08: Starting Z:\QtSupport\00472934\build-mqtttest-Desktop_Qt_5_15_13_MSVC2019_64bit-Debug\debug\mqtttest.exe...
      ### Connecting to "broker.hivemq.com:1883"
      Connected
      ### Subscribing to topic "Hello"
      Subscription state: 2
      ### Publishing: "Topic=Hello, Message=World"
      Message received: "World"
      ### Unsubscribing...
      Subscription state: 3
      ### Disconnecting...
      Disconnected
      !!! I WILL CRASH NOW !!!
      15:49:11: Z:\QtSupport\00472934\build-mqtttest-Desktop_Qt_5_15_13_MSVC2019_64bit-Debug\debug\mqtttest.exe crashed.
      

       

      Stack trace

      1  QCommandLineOption::isHidden              Qt5Cored            0x7ff80380a8c5 
      2  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876eaf1e 
      3  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876dd440 
      4  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876de448 
      5  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876de333 
      6  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876db271 
      7  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876ebaec 
      8  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876e8687 
      9  QMqttMessage::operator==                  Qt5Mqttd            0x7ff8876ede41 
      10 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b56222 
      11 QCommandLineOption::isHidden              Qt5Cored            0x7ff803bb09b8 
      12 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b9cd77 
      13 QCommandLineOption::isHidden              Qt5Cored            0x7ff8039e0f80 
      14 QNetworkInterface::interfaceNameFromIndex Qt5Networkd         0x7ff80a46abec 
      15 QNetworkInterface::interfaceNameFromIndex Qt5Networkd         0x7ff80a468035 
      16 QNetworkInterface::interfaceNameFromIndex Qt5Networkd         0x7ff80a46ece0 
      17 QNetworkInterface::interfaceNameFromIndex Qt5Networkd         0x7ff80a461937 
      18 QNetworkInterface::interfaceNameFromIndex Qt5Networkd         0x7ff80a492dfc 
      19 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b3cfe1 
      20 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b3f13f 
      21 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b3a3e1 
      22 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b3bb4f 
      23 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b39c32 
      24 QCommandLineOption::isHidden              Qt5Cored            0x7ff803c1182d 
      25 CallWindowProcW                           USER32              0x7ff8c0ebe858 
      26 DispatchMessageW                          USER32              0x7ff8c0ebe299 
      27 QCommandLineOption::isHidden              Qt5Cored            0x7ff803c12296 
      28 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b363db 
      29 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b36644 
      30 QCommandLineOption::isHidden              Qt5Cored            0x7ff803b399b9 
      31 main                                      main.cpp        53  0x7ff673f21ad2 
      32 WinMain                                   qtmain_win.cpp  97  0x7ff673f27675 
      33 invoke_main                               exe_common.inl  107 0x7ff673f24e42 
      34 __scrt_common_main_seh                    exe_common.inl  288 0x7ff673f24cee 
      35 __scrt_common_main                        exe_common.inl  331 0x7ff673f24bae 
      36 WinMainCRTStartup                         exe_winmain.cpp 17  0x7ff673f24ede 
      37 BaseThreadInitThunk                       KERNEL32            0x7ff8c1067604 
      38 RtlUserThreadStart                        ntdll               0x7ff8c1ae26a1 
      

       

      Notes
      Qt 6.5.0 does not crash but prints a message at the end,

      QIODevice::write (QTcpSocket): device not open
      

      Attachments

        Issue Links

          For Gerrit Dashboard: QTBUG-112984
          # Subject Branch Project Status CR V

          Activity

            People

              jbornema Joerg Bornemann
              skoh-qt Sze Howe Koh
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Gerrit Reviews

                  There are no open Gerrit changes