Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(standalone): stop a background failure from blanking the app
Elian Doran64a2cd506aad
diff --git a/apps/client/src/services/llm_chat.ts b/apps/client/src/services/llm_chat.tsindex abc5c0d813..959dea54db 100644--- a/apps/client/src/services/llm_chat.ts+++ b/apps/client/src/services/llm_chat.ts@@ -188,6 +188,11 @@ async function streamChatOverMessages(const onMessage = (message: WebSocketMessage) => {if (message.type === "llm-stream" && message.streamId === streamId) {handled = handled.then(() => handleChunk(message.chunk, callbacks));+ // Marks the link handled the moment it is made. `await handled` below+ // still rethrows, but without this a chunk handler that threw would sit+ // as an unhandled rejection until the stream ends — and in the+ // standalone build a stray rejection is reported, not ignored.+ handled.catch(() => undefined);} else if (message.type === "llm-stream-end" && message.streamId === streamId) {resolveEnd();}diff --git a/apps/standalone/src/local-bridge.ts b/apps/standalone/src/local-bridge.tsindex 888305b4c6..af0830bb59 100644--- a/apps/standalone/src/local-bridge.ts+++ b/apps/standalone/src/local-bridge.ts@@ -316,11 +316,13 @@ export function startLocalServerWorker() {return;}- // Handle worker error reports. These are uncaught errors and unhandled- // rejections in the worker; an initialization failure in particular- // never resolves a request (it just hangs), so this message is the only- // signal the user would otherwise get — surface it instead of a blank- // screen.+ // An error that escaped the worker before it finished starting up, which is+ // to say the application never came up: nothing will ever answer a request,+ // and it hangs rather than failing, so this message is the only signal the+ // user would otherwise get. Errors escaping a *running* worker do not come+ // this way — they arrive as `unhandled-error` and become a notification, so+ // one background task's failure cannot blank the screen (see+ // `reportEscapedError` in local-server-worker.ts).if (msg?.type === "WORKER_ERROR") {console.error("[LocalBridge] Worker reported error:", msg.error);showErrorOverlay(diff --git a/apps/standalone/src/local-server-worker.ts b/apps/standalone/src/local-server-worker.tsindex b9973c7fb0..a757ea0579 100644--- a/apps/standalone/src/local-server-worker.ts+++ b/apps/standalone/src/local-server-worker.ts@@ -4,41 +4,56 @@// We use dynamic imports below to ensure error handlers are registered first.// =============================================================================-self.onerror = (message, source, lineno, colno, error) => {- const errorMsg = `[Worker] Uncaught error: ${message}\n at ${source}:${lineno}:${colno}`;- console.error(errorMsg, error);+/**+ * Set once the worker has finished starting up, which is what decides an escaped+ * error's cost.+ *+ * Before it, an error means the application never came up — an unreadable database,+ * a failed migration — and the page has nothing to show but the overlay saying so.+ * After it, the worker is serving requests and the error belongs to one background+ * task: an LLM completion, a sync tick, a timer. Blanking the screen and failing+ * every request in flight over that costs the user their session for something that+ * is usually inconsequential to it, so those are reported and the worker carries on.+ *+ * The server draws the same line for the same reason, in `process_errors.ts`.+ */+let workerStarted = false;++/**+ * Report an error nothing else caught, as fatal or as an aside depending on+ * {@link workerStarted}. Deliberately free of imports: this runs before any module+ * has loaded, which is exactly when the fatal case happens.+ */+function reportEscapedError(label: string, message: string, stack?: string) {+ console.error(`[Worker] ${label}: ${message}`, stack);try {- self.postMessage({- type: "WORKER_ERROR",- error: {- message: String(message),- source,- lineno,- colno,- stack: error?.stack || new Error().stack- }- });+ self.postMessage(workerStarted+ // The same destination as the server's process-level safety net: a+ // notification naming the failure, with the stack behind a details+ // step. The client renders it from `unhandled-error` either way.+ ? { type: "WS_MESSAGE", message: { type: "unhandled-error", message, stack } }+ : { type: "WORKER_ERROR", error: { message, stack } });} catch (e) {- console.error("[Worker] Failed to report error:", e);+ console.error(`[Worker] Failed to report ${label.toLowerCase()}:`, e);}+}++self.onerror = (message, source, lineno, colno, error) => {+ reportEscapedError(+ "Uncaught error",+ `${message}\n at ${source}:${lineno}:${colno}`,+ error?.stack || new Error().stack+ );return false;};self.onunhandledrejection = (event) => {const reason = event.reason;- const errorMsg = `[Worker] Unhandled rejection: ${reason?.message || reason}`;- console.error(errorMsg, reason);- try {- self.postMessage({- type: "WORKER_ERROR",- error: {- message: String(reason?.message || reason),- stack: reason?.stack || new Error().stack- }- });- } catch (e) {- console.error("[Worker] Failed to report rejection:", e);- }+ reportEscapedError(+ "Unhandled rejection",+ String(reason?.message || reason),+ reason?.stack || new Error().stack+ );};console.log("[Worker] Error handlers installed, loading modules...");@@ -352,6 +367,9 @@ async function initialize(): Promise<void> {coreModule.scheduler.startScheduler();logService.info("[Worker] Initialization complete");+ // Past this point an escaped error is one background task's problem+ // rather than the application's — see `workerStarted`.+ workerStarted = true;} catch (error) {initError = error instanceof Error ? error : new Error(String(error));console.error("[Worker] Initialization failed:", initError);