Skip to content

A local-first personal finance app

🐛 Fix Initializing the connection to the local database hang (#4375)

Michael Clark7e1c0a86820c
diff --git a/packages/loot-core/src/platform/client/fetch/index.browser.ts b/packages/loot-core/src/platform/client/fetch/index.browser.ts
index 6f212a3465..0a7f0d0569 100644
--- a/packages/loot-core/src/platform/client/fetch/index.browser.ts
+++ b/packages/loot-core/src/platform/client/fetch/index.browser.ts
@@ -96,11 +96,15 @@ function connectWorker(worker, onOpen, onError) {
// ready to handle messages.
if (msg.type === 'connect') {
// Send any messages that were queued while closed
- if (messageQueue.length > 0) {
+ if (messageQueue?.length > 0) {
messageQueue.forEach(msg => worker.postMessage(msg));
messageQueue = null;
}
+ // signal to the backend that we're connected to it
+ globalWorker.postMessage({
+ name: 'client-connected-to-backend',
+ });
onOpen();
} else if (msg.type === 'app-init-failure') {
onError(msg);
diff --git a/packages/loot-core/src/platform/server/connection/index.web.ts b/packages/loot-core/src/platform/server/connection/index.web.ts
index 465b8348ed..6a17684c0e 100644
--- a/packages/loot-core/src/platform/server/connection/index.web.ts
+++ b/packages/loot-core/src/platform/server/connection/index.web.ts
@@ -44,6 +44,12 @@ export const init: T.Init = function (serverChn, handlers) {
return;
}
+ if (msg.name === 'client-connected-to-backend') {
+ // the client is indicating that it is connected to this backend. Stop attempting to connect
+ clearInterval(reconnectToClientInterval);
+ return;
+ }
+
const { id, name, args, undoTag, catchErrors } = msg;
if (handlers[name]) {
@@ -98,7 +104,19 @@ export const init: T.Init = function (serverChn, handlers) {
false,
);
- serverChannel.postMessage({ type: 'connect' });
+ const RECONNECT_INTERVAL_MS = 200;
+ const MAX_RECONNECT_ATTEMPTS = 500;
+ let reconnectAttempts = 0;
+
+ const reconnectToClientInterval = setInterval(() => {
+ serverChannel.postMessage({ type: 'connect' });
+ reconnectAttempts++;
+ if (reconnectAttempts >= MAX_RECONNECT_ATTEMPTS) {
+ // Failed to connect to client - signal server error
+ send('server-error');
+ clearInterval(reconnectToClientInterval);
+ }
+ }, RECONNECT_INTERVAL_MS);
};
export const send: T.Send = function (name, args) {
diff --git a/upcoming-release-notes/4375.md b/upcoming-release-notes/4375.md
new file mode 100644
index 0000000000..6bc9b82912
--- /dev/null
+++ b/upcoming-release-notes/4375.md
@@ -0,0 +1,6 @@
+---
+category: Bugfix
+authors: [MikesGlitch]
+---
+
+Prevent the app getting stuck on the "Initializing the connection to the local database..." screen