From 731743b61850531123ce1dc3a17bba202fde8aaa Mon Sep 17 00:00:00 2001 From: James Brunton Date: Thu, 27 Nov 2025 15:54:35 +0000 Subject: [PATCH] Don't block desktop app on backend starting up (#5041) # Description of Changes Start bundled backend instantly on startup of app and don't wait on it being fully up to spawn app. This is techincally wasteful curently on self-hosted mode where everything runs remotely, but in the future we'll probably route simple operations to the local machine regardless of connection, and it stops unnecessary waiting in the offline mode. --- frontend/src-tauri/src/commands/backend.rs | 4 --- frontend/src-tauri/src/lib.rs | 13 +++++++++- .../src/desktop/components/AppProviders.tsx | 26 ++++--------------- .../desktop/components/SetupWizard/index.tsx | 2 +- 4 files changed, 18 insertions(+), 27 deletions(-) diff --git a/frontend/src-tauri/src/commands/backend.rs b/frontend/src-tauri/src/commands/backend.rs index af2b149bc..aed5f0b87 100644 --- a/frontend/src-tauri/src/commands/backend.rs +++ b/frontend/src-tauri/src/commands/backend.rs @@ -398,10 +398,6 @@ pub async fn start_backend( e })?; - // Wait for the backend to start - println!("⏳ Waiting for backend startup..."); - tokio::time::sleep(std::time::Duration::from_millis(10000)).await; - // Reset the starting flag since startup is complete reset_starting_flag(); add_log("✅ Backend startup sequence completed, starting flag cleared".to_string()); diff --git a/frontend/src-tauri/src/lib.rs b/frontend/src-tauri/src/lib.rs index e4f15ddc9..cb8faf04b 100644 --- a/frontend/src-tauri/src/lib.rs +++ b/frontend/src-tauri/src/lib.rs @@ -66,7 +66,7 @@ pub fn run() { // Emit a generic notification that files were added (frontend will re-read storage) let _ = app.emit("files-changed", ()); })) - .setup(|_app| { + .setup(|app| { add_log("🚀 Tauri app setup started".to_string()); // Process command line arguments on first launch @@ -78,6 +78,17 @@ pub fn run() { } } + // Start backend immediately, non-blocking + let app_handle = app.handle().clone(); + + tauri::async_runtime::spawn(async move { + add_log("🚀 Starting bundled backend in background".to_string()); + let connection_state = app_handle.state::(); + if let Err(e) = commands::backend::start_backend(app_handle.clone(), connection_state).await { + add_log(format!("⚠️ Backend start failed: {}", e)); + } + }); + add_log("🔍 DEBUG: Setup completed".to_string()); Ok(()) }) diff --git a/frontend/src/desktop/components/AppProviders.tsx b/frontend/src/desktop/components/AppProviders.tsx index 84d455c51..6b82fd820 100644 --- a/frontend/src/desktop/components/AppProviders.tsx +++ b/frontend/src/desktop/components/AppProviders.tsx @@ -31,10 +31,10 @@ export function AppProviders({ children }: { children: ReactNode }) { } }, [setupComplete, isFirstLaunch, connectionMode]); - // Only start bundled backend if in SaaS mode (local backend) and setup is complete - // Self-hosted mode connects to remote server so doesn't need local backend - const shouldStartBackend = setupComplete && !isFirstLaunch && connectionMode === 'saas'; - useBackendInitializer(shouldStartBackend); + // Initialize monitoring for bundled backend (already started in Rust) + // This sets up port detection and health checks + const shouldMonitorBackend = setupComplete && !isFirstLaunch && connectionMode === 'saas'; + useBackendInitializer(shouldMonitorBackend); // Show setup wizard on first launch if (isFirstLaunch && !setupComplete) { @@ -51,23 +51,7 @@ export function AppProviders({ children }: { children: ReactNode }) { }} > { - // Wait for backend to become healthy before reloading - // This prevents reloading mid-startup which would interrupt the backend - const maxWaitTime = 60000; // 60 seconds max - const checkInterval = 1000; // Check every second - const startTime = Date.now(); - - while (Date.now() - startTime < maxWaitTime) { - if (tauriBackendService.isBackendHealthy()) { - window.location.reload(); - return; - } - await new Promise(resolve => setTimeout(resolve, checkInterval)); - } - - // If we timeout, reload anyway - console.warn('[AppProviders] Backend health check timeout, reloading anyway...'); + onComplete={() => { window.location.reload(); }} /> diff --git a/frontend/src/desktop/components/SetupWizard/index.tsx b/frontend/src/desktop/components/SetupWizard/index.tsx index a1e0fbf5f..c05e503de 100644 --- a/frontend/src/desktop/components/SetupWizard/index.tsx +++ b/frontend/src/desktop/components/SetupWizard/index.tsx @@ -61,7 +61,7 @@ export const SetupWizard: React.FC = ({ onComplete }) => { } await connectionModeService.switchToSaaS(serverConfig.url); - await tauriBackendService.startBackend(); + tauriBackendService.startBackend().catch(console.error); onComplete(); } catch (err) { console.error('SaaS login failed:', err);