diff --git a/Sources/AppDelegate.swift b/Sources/AppDelegate.swift index 7e346a71f1b8..39974dea3aa4 100644 --- a/Sources/AppDelegate.swift +++ b/Sources/AppDelegate.swift @@ -4511,12 +4511,6 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent !isTerminatingApp } - nonisolated static func shouldRemoveSnapshotWhenNoWindowsRemainOnWindowUnregister( - isTerminatingApp: Bool - ) -> Bool { - !isTerminatingApp - } - nonisolated static func shouldSkipSessionSaveDuringStartupRestore( isApplyingStartupSessionRestore: Bool, includeScrollback: Bool @@ -12981,12 +12975,14 @@ final class AppDelegate: NSObject, NSApplicationDelegate, UNUserNotificationCent // During app termination we already persisted a full snapshot (with scrollback) // in applicationShouldTerminate/applicationWillTerminate. Saving again here would // overwrite it as windows tear down one-by-one, dropping closed windows and replay. + // + // For user-initiated red-X closes (app stays running), capture scrollback for the + // remaining windows and keep the snapshot file even if this was the last window — + // so relaunching cmux still restores the prior session instead of starting blank. if Self.shouldPersistSnapshotOnWindowUnregister(isTerminatingApp: isTerminatingApp) { _ = saveSessionSnapshot( - includeScrollback: false, - removeWhenEmpty: Self.shouldRemoveSnapshotWhenNoWindowsRemainOnWindowUnregister( - isTerminatingApp: isTerminatingApp - ) + includeScrollback: true, + removeWhenEmpty: false ) } } diff --git a/Sources/Update/UpdateDelegate.swift b/Sources/Update/UpdateDelegate.swift index 5017009e945f..119bf726865d 100644 --- a/Sources/Update/UpdateDelegate.swift +++ b/Sources/Update/UpdateDelegate.swift @@ -108,7 +108,13 @@ extension UpdateDriver: SPUUpdaterDelegate { } func updaterWillRelaunchApplication(_ updater: SPUUpdater) { - Task { @MainActor in + // Sparkle calls this delegate method on the main thread immediately + // before it proceeds to terminate + relaunch the app. Run synchronously + // so the session snapshot (with scrollback) is flushed to disk before + // Sparkle tears everything down. Dispatching via `Task { @MainActor }` + // here caused the save to race with termination and occasionally lose + // scrollback on updates. + MainActor.assumeIsolated { AppDelegate.shared?.persistSessionForUpdateRelaunch() TerminalController.shared.stop() NSApp.invalidateRestorableState() diff --git a/cmuxTests/SessionPersistenceTests.swift b/cmuxTests/SessionPersistenceTests.swift index a8b58f7d3af0..8e0dd30b82d4 100644 --- a/cmuxTests/SessionPersistenceTests.swift +++ b/cmuxTests/SessionPersistenceTests.swift @@ -425,12 +425,6 @@ final class SessionPersistenceTests: XCTestCase { XCTAssertFalse( AppDelegate.shouldPersistSnapshotOnWindowUnregister(isTerminatingApp: true) ) - XCTAssertTrue( - AppDelegate.shouldRemoveSnapshotWhenNoWindowsRemainOnWindowUnregister(isTerminatingApp: false) - ) - XCTAssertFalse( - AppDelegate.shouldRemoveSnapshotWhenNoWindowsRemainOnWindowUnregister(isTerminatingApp: true) - ) } func testShouldSkipSessionSaveDuringStartupRestorePolicy() {