Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 6 additions & 10 deletions Sources/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
)
}
}
Expand Down
8 changes: 7 additions & 1 deletion Sources/Update/UpdateDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
6 changes: 0 additions & 6 deletions cmuxTests/SessionPersistenceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down