From c1189928d4f8f9bc5bd1a5e850b18e9cea4d7b92 Mon Sep 17 00:00:00 2001 From: CelloSerenity <195480169+CelloSerenity@users.noreply.github.com> Date: Sun, 26 Jul 2026 00:34:07 -0600 Subject: [PATCH] Fix JIT race on cold URL-scheme launch --- StikDebug/App/StikDebugApp.swift | 1 + StikDebug/Views/HomeView.swift | 25 +++++++++++++++++++++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/StikDebug/App/StikDebugApp.swift b/StikDebug/App/StikDebugApp.swift index 5ccd112..d6c70f9 100644 --- a/StikDebug/App/StikDebugApp.swift +++ b/StikDebug/App/StikDebugApp.swift @@ -45,6 +45,7 @@ struct StikDebugApp: App { private func downloadMissingDeveloperDiskImageFiles() async { do { try await DeveloperDiskImageService.shared.downloadMissingFiles() + MountingProgress.shared.pubMount() } catch { await MainActor.run { showAlert( diff --git a/StikDebug/Views/HomeView.swift b/StikDebug/Views/HomeView.swift index a7b1b54..363ba80 100644 --- a/StikDebug/Views/HomeView.swift +++ b/StikDebug/Views/HomeView.swift @@ -308,8 +308,18 @@ struct HomeView: View { let keepAliveLease = DebugKeepAliveLease() defer { keepAliveLease.invalidate() } - if triggeredByURLScheme { - sleep(1) + if triggeredByURLScheme, !waitForJITPrerequisites() { + DispatchQueue.main.async { + withAnimation { + debugFeedback = nil + } + showAlert( + title: "Failed to Enable JIT".localized, + message: "The device connection or Developer Disk Image wasn't ready in time. Open StikDebug directly, wait for it to finish connecting, then try again.".localized, + showOk: true + ) + } + return } let finishProcessing: (Bool, String?) -> Void = { success, detail in @@ -384,6 +394,17 @@ struct HomeView: View { } } + private func waitForJITPrerequisites(timeout: TimeInterval = 20) -> Bool { + let deadline = Date().addingTimeInterval(timeout) + while Date() < deadline { + if TunnelManager.shared.isConnected && MountingProgress.shared.coolisMounted { + return true + } + usleep(250_000) + } + return TunnelManager.shared.isConnected && MountingProgress.shared.coolisMounted + } + private func base64URLToBase64(_ base64url: String) -> String { var base64 = base64url.replacingOccurrences(of: "-", with: "+").replacingOccurrences(of: "_", with: "/") let pad = 4 - (base64.count % 4)