Skip to content

Fastest and only macOS Dictation app with on-device STT and custom trained AI enhancement model. Windows, iOS testflight! Android, Linux waitlist open! A local Wispr Flow alternative. Follow https://x.com/fluidvoiceapp for more

fix(updater): retire leftover old copy after update (#1083)

ALTIC7daffb529fb9
diff --git a/Sources/Fluid/AppDelegate.swift b/Sources/Fluid/AppDelegate.swift
index 68e608b9..aa276175 100644
--- a/Sources/Fluid/AppDelegate.swift
+++ b/Sources/Fluid/AppDelegate.swift
@@ -100,6 +100,7 @@ class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCenterDele
source: "AppDelegate"
)
UNUserNotificationCenter.current().delegate = self
+ SupersededInstanceRetirement.start()
// Initialize app settings (dock visibility, etc.)
SettingsStore.shared.initializeAppSettings()
diff --git a/Sources/Fluid/Services/UpdateTerminationScheduler.swift b/Sources/Fluid/Services/UpdateTerminationScheduler.swift
index 11e6f920..7a75feff 100644
--- a/Sources/Fluid/Services/UpdateTerminationScheduler.swift
+++ b/Sources/Fluid/Services/UpdateTerminationScheduler.swift
@@ -14,3 +14,79 @@ enum UpdateTerminationScheduler {
}
}
}
+
+/// Releases before 1.6.10-beta.8 quit after an update from a main-queue block, which can leave the
+/// old copy stuck mid-quit with its install progress window on screen. Those releases cannot be
+/// changed, so the first launch of a fixed build removes that stuck copy, once.
+enum SupersededInstanceRetirement {
+ struct Instance: Equatable {
+ let processID: pid_t
+ let launchDate: Date?
+ let bundlePath: String?
+ }
+
+ nonisolated static let completedDefaultsKey = "SupersededLegacyInstanceRetirementCompleted"
+ /// The old copy normally quits itself about two seconds after launching this one.
+ nonisolated static let quitRequestDelay: Duration = .seconds(5)
+ /// A copy that accepted the quit may still be saving: AppDelegate allows 8s for private AI,
+ /// 12s for ASR and meeting shutdown, and 2s for Zeppelin. Never cut that short.
+ nonisolated static let forceQuitGracePeriod: Duration = .seconds(30)
+
+ /// An update replaces the app in place, so the outgoing copy was launched earlier from this same
+ /// path. Never this process, a newer copy, a copy opened from another location (a second install
+ /// or a saved rollback), or one whose age or location is unknown.
+ nonisolated static func superseded(
+ among instances: [Instance],
+ currentProcessID: pid_t,
+ currentLaunchDate: Date,
+ currentBundlePath: String
+ ) -> [pid_t] {
+ instances.compactMap { instance in
+ guard instance.processID != currentProcessID,
+ let launchDate = instance.launchDate,
+ launchDate < currentLaunchDate,
+ instance.bundlePath == currentBundlePath
+ else { return nil }
+ return instance.processID
+ }
+ }
+
+ /// One bounded pass, repeated on later launches only until one completes. Fixed builds close
+ /// their own progress window and are never asked or forced to quit by a later copy.
+ static func start() {
+ guard let bundleIdentifier = Bundle.main.bundleIdentifier,
+ ProcessInfo.processInfo.environment["XCTestConfigurationFilePath"] == nil,
+ !UserDefaults.standard.bool(forKey: Self.completedDefaultsKey)
+ else { return }
+ let currentProcessID = ProcessInfo.processInfo.processIdentifier
+ let currentLaunchDate = NSRunningApplication.current.launchDate ?? Date()
+ let currentBundlePath = Bundle.main.bundleURL.standardizedFileURL.path
+ Task.detached(priority: .utility) {
+ let supersededApplications: @Sendable () -> [NSRunningApplication] = {
+ let applications = NSRunningApplication.runningApplications(withBundleIdentifier: bundleIdentifier)
+ let superseded = Set(Self.superseded(
+ among: applications.map {
+ Instance(processID: $0.processIdentifier, launchDate: $0.launchDate, bundlePath: $0.bundleURL?.standardizedFileURL.path)
+ },
+ currentProcessID: currentProcessID,
+ currentLaunchDate: currentLaunchDate,
+ currentBundlePath: currentBundlePath
+ ))
+ return applications.filter { superseded.contains($0.processIdentifier) }
+ }
+ try? await Task.sleep(for: Self.quitRequestDelay)
+ let lingering = supersededApplications()
+ if !lingering.isEmpty {
+ await DebugLogger.shared.info("Asking \(lingering.count) superseded app instance(s) to quit", source: "AppDelegate")
+ lingering.forEach { $0.terminate() }
+ try? await Task.sleep(for: Self.forceQuitGracePeriod)
+ let stuck = supersededApplications()
+ if !stuck.isEmpty {
+ await DebugLogger.shared.warning("Force quitting \(stuck.count) superseded app instance(s)", source: "AppDelegate")
+ stuck.forEach { $0.forceTerminate() }
+ }
+ }
+ UserDefaults.standard.set(true, forKey: Self.completedDefaultsKey)
+ }
+ }
+}
diff --git a/Tests/FluidDictationIntegrationTests/UpdatePromptIntegrationTests.swift b/Tests/FluidDictationIntegrationTests/UpdatePromptIntegrationTests.swift
index 241a0ce7..e5b9bbac 100644
--- a/Tests/FluidDictationIntegrationTests/UpdatePromptIntegrationTests.swift
+++ b/Tests/FluidDictationIntegrationTests/UpdatePromptIntegrationTests.swift
@@ -15,6 +15,39 @@ final class UpdatePromptIntegrationTests: XCTestCase {
XCTAssertNil(try JSONDecoder().decode(SettingsBackupPayload.self, from: legacyData).showUpdatePopups)
}
+ func testOnlyOlderCopiesAtTheSameLocationAreSuperseded() {
+ typealias Instance = SupersededInstanceRetirement.Instance
+ let launch = Date(timeIntervalSinceReferenceDate: 1000)
+ let path = "/Applications/FluidVoice.app"
+ let instances = [
+ Instance(processID: 10, launchDate: launch.addingTimeInterval(-60), bundlePath: path),
+ Instance(processID: 20, launchDate: launch, bundlePath: path),
+ Instance(processID: 30, launchDate: launch.addingTimeInterval(60), bundlePath: path),
+ Instance(processID: 40, launchDate: nil, bundlePath: path),
+ Instance(processID: 50, launchDate: launch.addingTimeInterval(-60), bundlePath: "/Users/me/Downloads/FluidVoice.app"),
+ Instance(processID: 60, launchDate: launch.addingTimeInterval(-60), bundlePath: nil),
+ ]
+ func superseded(_ instances: [Instance], as processID: pid_t, launchedAt date: Date) -> [pid_t] {
+ SupersededInstanceRetirement.superseded(among: instances, currentProcessID: processID, currentLaunchDate: date, currentBundlePath: path)
+ }
+ XCTAssertEqual(
+ superseded(instances, as: 20, launchedAt: launch),
+ [10],
+ "only the copy already running from this location is retired; a second install, a rollback copy and unknowns are left alone"
+ )
+ XCTAssertEqual(superseded([instances[1]], as: 20, launchedAt: launch), [], "a lone instance never retires itself")
+ XCTAssertEqual(
+ superseded(instances, as: 10, launchedAt: launch.addingTimeInterval(-60)),
+ [],
+ "the older copy never retires the newer ones"
+ )
+ }
+
+ func testForceQuitWaitsLongerThanTheOldCopysShutdownBudget() {
+ // AppDelegate.applicationWillTerminate: 8s private AI + 12s ASR and meeting + 2s Zeppelin.
+ XCTAssertGreaterThan(SupersededInstanceRetirement.forceQuitGracePeriod, .seconds(8 + 12 + 2))
+ }
+
func testOfferWithQueuedFailureTransitionsToOnlyInstallStatus() async throws {
let presenter = UpdatePromptPresenter.shared
let updater = SimpleUpdater()