Skip to content

Commit

Permalink
Add a watchdog to detect crashed wasm builds and reload the page
Browse files Browse the repository at this point in the history
Fixes #1803
  • Loading branch information
DanielMcInnes committed Dec 17, 2024
1 parent 5a676a9 commit 4e27af8
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 3 deletions.
21 changes: 18 additions & 3 deletions .github/patches/index.html.patch
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
diff -u index.html venus-gui-v2.html
--- index.html 2024-05-16 22:12:44.106129598 +0300
+++ venus-gui-v2.html 2024-07-05 12:44:44.396192697 +0200
--- index.html 2024-12-17 17:13:52.442569994 +1000
+++ venus-gui-v2.html 2024-12-17 17:08:58.496874189 +1000
@@ -12,15 +12,18 @@
<title>venus-gui-v2</title>
<style>
Expand Down Expand Up @@ -63,3 +62,19 @@ diff -u index.html venus-gui-v2.html
qt: {
onLoaded: () => showUi(screen),
onExit: exitData =>
@@ -70,5 +99,15 @@
</script>
<script src="venus-gui-v2.js"></script>
<script type="text/javascript" src="qtloader.js"></script>
+ <script type="text/javascript">
+ var watchdogHit = false // this gets set to 'true' by a timer in BackendConnection::onWatchdogTimerExpired()
+ setInterval(function() {
+ if (!watchdogHit) {
+ console.error("Watchdog timer expired - reloading page")
+ location.reload()
+ }
+ watchdogHit = false
+ }, 20000);
+ </script>
</body>
</html>
1 change: 1 addition & 0 deletions Main.qml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import QtQuick.Window

// *** This file cannot be edited directly on the cerbo filesystem. It is loaded from the binary ***


Window {
id: root

Expand Down
9 changes: 9 additions & 0 deletions src/backendconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,12 @@ void BackendConnection::onReloadPageTimerExpired()
reloadPage();
}

// Sometimes, the wasm code may crash. Use a watchdog to detect this and reload the page when necessary.
void BackendConnection::onWatchdogTimerExpired()
{
emscripten_run_script("watchdogHit = true"); // 'watchdogHit' is defined in venus-gui-v2.html, which checks it periodically and reloads the page if not hit regularly.
}

// If the wasm itself changed the Security Profile, it should normally be notified
// that it is accepted by receiving a Network Config change. Since that event is send
// over a connection which is going to be disconnected, this acts as a fallback if
Expand Down Expand Up @@ -272,6 +278,9 @@ void BackendConnection::initMqttConnection(const QString &address)

VeQItem *item = mqttProducer->services()->itemGetOrCreate("/platform/0/Network/ConfigChanged");
connect(item, &VeQItem::valueChanged, this, &BackendConnection::onNetworkConfigChanged);

connect(&mWatchdogTimer, &QTimer::timeout, this, &BackendConnection::onWatchdogTimerExpired);
mWatchdogTimer.start(1000);
#else
const QStringList parts = address.split(':');
if (parts.size() >= 2) {
Expand Down
7 changes: 7 additions & 0 deletions src/backendconnection.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,9 @@ class BackendConnection : public QObject
private Q_SLOTS:
void onNetworkConfigChanged(const QVariant var);
void onReloadPageTimerExpired();
#if defined(VENUS_WEBASSEMBLY_BUILD)
void onWatchdogTimerExpired();
#endif

private:
explicit BackendConnection(QObject *parent = nullptr);
Expand Down Expand Up @@ -195,6 +198,10 @@ private Q_SLOTS:
AlarmBusitem *m_alarmBusItem = nullptr;
#endif
QNetworkAccessManager *m_network = nullptr;

#if defined(VENUS_WEBASSEMBLY_BUILD)
QTimer mWatchdogTimer;
#endif
};

class BackendConnectionTester : public QObject
Expand Down

0 comments on commit 4e27af8

Please sign in to comment.