Skip to content

Commit

Permalink
🐛 Fix Initializing the connection to the local database hang (#4375)
Browse files Browse the repository at this point in the history
* fix initializing to the local db

* release notes
  • Loading branch information
MikesGlitch authored Feb 13, 2025
1 parent 454f019 commit 7e1c0a8
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
20 changes: 19 additions & 1 deletion packages/loot-core/src/platform/server/connection/index.web.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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]) {
Expand Down Expand Up @@ -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) {
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/4375.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Bugfix
authors: [MikesGlitch]
---

Prevent the app getting stuck on the "Initializing the connection to the local database..." screen

0 comments on commit 7e1c0a8

Please sign in to comment.