Mac: Close and Quit do not wait out a session renewal; they ask
Review #72 delta, finding 2. With a session that could not be renewed, Close and Quit (which wait for edits on their way) sat through the whole renewal hold, about a minute, before anything happened. settle(hurry:) now cuts the hold short: the held edits are marked as not saved at once, and the window (or Quit) asks about them as it does any failed edit. Save in that dialog retries once without the hold. Any new edit or Retry holds for a renewal again. Test: closingCutsASessionHoldShort (settle returns in under a second with a 30 s hold scripted, and the edit is marked so the window can ask).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+62
−16
native/apple/Sources/SheetsKit/SheetCloseGuard.swift+14 −11native/apple/Sources/SheetsKit/SheetModel.swift+6 −2native/apple/Sources/SheetsKit/SheetWrites.swift+22 −3native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift+20 −0
diff --git a/native/apple/Sources/SheetsKit/SheetCloseGuard.swift b/native/apple/Sources/SheetsKit/SheetCloseGuard.swift| index 3b67351..b9ae677 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetCloseGuard.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetCloseGuard.swift |
| @@ -39,10 +39,11 @@ |
| return previousDelegate?.windowShouldClose?(sender) ?? true |
| } |
| if !model.dirty && !model.hasFailedEdits { |
| - // Only edits on their way: let them land, then close. |
| + // Only edits on their way: let them land, then close; any that did |
| + // not (an ended session is not waited out) are asked about now. |
| Task { @MainActor in |
| - await model.settle() |
| - if !model.hasUnconfirmedChanges { sender.performClose(nil) } |
| + await model.settle(hurry: true) |
| + if !model.hasUnconfirmedChanges || model.hasFailedEdits { sender.performClose(nil) } |
| } |
| return false |
| } |
| @@ -55,11 +56,11 @@ |
| alert.beginSheetModal(for: sender) { response in |
| Task { @MainActor in |
| if response == .alertFirstButtonReturn { |
| - await model.saveAll() |
| + await model.saveAll(hurry: true) |
| if !model.hasUnconfirmedChanges { sender.performClose(nil) } |
| } else if response == .alertSecondButtonReturn { |
| model.discardAll() |
| - await model.settle() |
| + await model.settle(hurry: true) |
| model.revertWrites() |
| sender.performClose(nil) |
| } |
| @@ -78,11 +79,13 @@ |
| let pending = models.filter { $0.hasUnconfirmedChanges } |
| if pending.isEmpty { return .terminateNow } |
| if pending.allSatisfy({ !$0.dirty && !$0.hasFailedEdits }) { |
| - // Only edits on their way: quit once they land. One that fails stays |
| - // marked in its window, and the app stays open to say so. |
| + // Only edits on their way: quit once they land. Any that did not (an |
| + // ended session is not waited out) are asked about at once. |
| Task { @MainActor in |
| - for model in pending { await model.settle() } |
| - sender.reply(toApplicationShouldTerminate: !pending.contains { $0.hasUnconfirmedChanges }) |
| + for model in pending { await model.settle(hurry: true) } |
| + let clean = !pending.contains { $0.hasUnconfirmedChanges } |
| + sender.reply(toApplicationShouldTerminate: clean) |
| + if !clean { DispatchQueue.main.async { NSApp.terminate(nil) } } |
| } |
| return .terminateLater |
| } |
| @@ -94,14 +97,14 @@ |
| switch alert.runModal() { |
| case .alertFirstButtonReturn: |
| Task { @MainActor in |
| - for model in pending { await model.saveAll() } |
| + for model in pending { await model.saveAll(hurry: true) } |
| sender.reply(toApplicationShouldTerminate: !pending.contains { $0.hasUnconfirmedChanges }) |
| } |
| return .terminateLater |
| case .alertSecondButtonReturn: |
| pending.forEach { $0.discardAll() } |
| Task { @MainActor in |
| - for model in pending { await model.settle() } |
| + for model in pending { await model.settle(hurry: true) } |
| sender.reply(toApplicationShouldTerminate: true) |
| } |
| return .terminateLater |
diff --git a/native/apple/Sources/SheetsKit/SheetModel.swift b/native/apple/Sources/SheetsKit/SheetModel.swift| index 0844a9c..20f5ef6 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetModel.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetModel.swift |
| @@ -32,6 +32,10 @@ import RastrilloApple |
| @ObservationIgnored var sessionRetryDelays: [Duration] = [ |
| .milliseconds(500), .seconds(1), .seconds(2), .seconds(4), .seconds(8), .seconds(15), .seconds(30), |
| ] |
| + /// Close or Quit is waiting: a session hold gives up at once, so the window |
| + /// can ask about the edits instead of waiting out the renewal. |
| + @ObservationIgnored var hurried = false |
| + @ObservationIgnored var sessionWait: Task<Void, Never>? |
| /// True while `open` replaces the sheet: no edit may start against either. |
| @ObservationIgnored var opening = false |
| /// An edit being typed, or one not yet acknowledged by the server. |
| @@ -206,10 +210,10 @@ import RastrilloApple |
| } |
| /// Close, Quit and switching sheets: commit the edit being typed, retry the |
| /// failed ones, and wait for the answers. |
| - func saveAll() async { |
| + func saveAll(hurry: Bool = false) async { |
| if dirty { commit(moveRight: nil) } |
| retryWrites() |
| - await settle() |
| + await settle(hurry: hurry) |
| } |
| func discard() { |
| originalDraft = rawAt(selection.start) |
diff --git a/native/apple/Sources/SheetsKit/SheetWrites.swift b/native/apple/Sources/SheetsKit/SheetWrites.swift| index c3b1fd0..10bea76 100644 |
| --- a/native/apple/Sources/SheetsKit/SheetWrites.swift |
| +++ b/native/apple/Sources/SheetsKit/SheetWrites.swift |
| @@ -36,6 +36,7 @@ struct PendingEdit: Sendable, Equatable { |
| /// the answer from the server arrives later and never blocks editing. |
| @discardableResult func write(_ writes: [CellWrite]) -> Bool { |
| guard mayWrite else { return false } |
| + hurried = false |
| // Mid-switch, an edit could only be sent to the sheet being left. |
| guard !opening else { return false } |
| guard writes.allSatisfy({ $0.raw.utf8.count <= 8192 }) else { |
| @@ -101,6 +102,7 @@ struct PendingEdit: Sendable, Equatable { |
| func editState(_ point: Coordinate) -> PendingEdit.State? { pending[point]?.state } |
| func retryWrites(_ points: [Coordinate]? = nil) { |
| + hurried = false |
| var next = pending |
| for (point, edit) in pending where edit.state == .failed && (points?.contains(point) ?? true) { |
| next[point]?.state = .queued |
| @@ -134,7 +136,14 @@ struct PendingEdit: Sendable, Equatable { |
| } |
| /// Waits until nothing is queued or in flight. Failed edits stay failed. |
| - func settle() async { |
| + /// `hurry`: Close or Quit is waiting, so an ended session's edits are |
| + /// marked as not saved now rather than after the renewal hold (about a |
| + /// minute), and the window can ask about them straight away. |
| + func settle(hurry: Bool = false) async { |
| + if hurry { |
| + hurried = true |
| + sessionWait?.cancel() |
| + } |
| while let sender { await sender.value } |
| } |
| @@ -185,14 +194,24 @@ struct PendingEdit: Sendable, Equatable { |
| error = "" |
| if !isSending { notice = "Saved" } |
| if showTable { await loadTable() } |
| - } catch NativeClientError.unauthorized where sessionWaits < sessionRetryDelays.count { |
| + } catch NativeClientError.unauthorized where sessionWaits < sessionRetryDelays.count && !hurried { |
| guard grid == source else { return abandon(batch) } |
| // Hold the queue while the session is renewed, then send again: the |
| // edits stay pending (Saving…), and edits made meanwhile join them. |
| requeue(batch[offset...]) |
| let delay = sessionRetryDelays[sessionWaits] |
| sessionWaits += 1 |
| - try? await Task.sleep(for: delay) |
| + let wait = Task { _ = try? await Task.sleep(for: delay) } |
| + sessionWait = wait |
| + await wait.value |
| + sessionWait = nil |
| + if hurried { |
| + // Close or Quit cut the hold short: what is queued is not saved. |
| + var next = pending |
| + for (point, edit) in pending where edit.state == .queued { next[point]?.state = .failed } |
| + pending = next |
| + return |
| + } |
| } catch { |
| guard grid == source else { return abandon(batch) } |
| // The cells say so themselves, with Retry and Revert; editing goes on. |
diff --git a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift| index 12b5049..06cc004 100644 |
| --- a/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| +++ b/native/apple/Tests/SheetsKitTests/OptimisticEditTests.swift |
| @@ -357,3 +357,23 @@ private let b1 = Coordinate(r: 0, c: 1) |
| await model.refresh() |
| #expect(model.pending.isEmpty, "the server no longer has the cell: the clear landed") |
| } |
| + |
| +// Review #72 delta, finding 2: Close and Quit do not wait out a session hold. |
| +@MainActor @Test func closingCutsASessionHoldShort() async { |
| + let server = SheetServerFixture() |
| + server.unauthorizeNext(100) |
| + let model = fixtureModel(server) |
| + model.sessionRetryDelays = [.seconds(30), .seconds(30)] |
| + type(model, a1, "x") |
| + #expect(await eventually { server.batches.count == 1 }) |
| + let start = ProcessInfo.processInfo.systemUptime |
| + await model.settle(hurry: true) |
| + #expect(ProcessInfo.processInfo.systemUptime - start < 1, "Close waited out the renewal") |
| + #expect(model.editState(a1) == .failed, "so the window can ask about it") |
| + // A later edit holds for a renewal again. |
| + server.unauthorizeNext(1) |
| + model.sessionRetryDelays = [.milliseconds(10)] |
| + model.retryWrites() |
| + await model.settle() |
| + #expect(model.pending.isEmpty) |
| +} |