Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading