Skip to content

Commit

Permalink
Detect and work-around stuck-reconnection issue
Browse files Browse the repository at this point in the history
  • Loading branch information
chriadam committed Nov 3, 2023
1 parent 7b63d59 commit fa53a6e
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/qt/ve_qitems_mqtt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,21 @@ void VeQItemMqttProducer::onConnected()
this, &VeQItemMqttProducer::onSubscriptionMessageReceived, Qt::UniqueConnection);
doKeepAlive();
}

// QMqttClient has some internal state which sometimes isn't cleaned up
// properly on disconnection. To allow reconnection, we need to delete
// the client, transition back to idle state, and then reconnect.
QTimer::singleShot(3000, this, [this] {
// If we're still waiting to receive a message from the broker more
// than 3 seconds after connecting, assume we are in the stuck state.
if (connectionState() == VeQItemMqttProducer::Connected) {
qWarning() << "Automatic reconnection failed, trying clean reconnect.";
mMqttConnection->deleteLater();
mMqttConnection = nullptr;
setConnectionState(Idle);
open(QHostAddress(mHostName), mPort);
}
});
}

void VeQItemMqttProducer::onDisconnected()
Expand Down

4 comments on commit fa53a6e

@jhofstee
Copy link
Contributor

@jhofstee jhofstee commented on fa53a6e Nov 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I sincerely doubt you or I or we are waiting for a broker message. And I sincerely doubt we get stuck if the message doesn't make it. Please do me a favor and write that the program gets stuck or whatever, it doesn't affect us ;)

@DanielMcInnes
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chriadam how often does this happen? Is this discussed in slack somewhere? I had a look but couldn't see it

@chriadam
Copy link
Author

@chriadam chriadam commented on fa53a6e Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jhofstee I believe it's a problem in QMqttClient. If I am correct, I will create a patch to fix QtMqtt and we can build a patched Qt. I will let you know how it goes.

Of course I might be mistaken - it might be that my state machine has a problem and the subscription is being invalidated and not recreated properly - I will investigate more into that side, too, of course.

@chriadam
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@DanielMcInnes see issue #214.

Please sign in to comment.