Docs native: fix the launch hang (0.1.7/0.1.8 beachball on Mac, watchdog kill on iPhone)
With a saved account, DocsAccountWindow.body built a DocsWorkspace whose init called DocsOfflineAccount.account, which read and then mutated observed drainer state (retryNow's backoff, the session) and the supplier closure. The window body tracked those reads, so each pass invalidated the next: the main thread spun in SwiftUI's transaction flush for ever (Paul's sample, 11-19). On iPhone the watchdog killed it, which TestFlight reported as the 11-16 crash. account() is now a pure lookup-or-create, the supplier is @ObservationIgnored, and the workspace resumes its account from .task. WorkspaceInitTests (builds a workspace inside observation tracking, as a window body does) fails on the old code and passes now. Also: iPhone declares its supported orientations and exempt encryption, so App Store Connect accepts the upload without overrides. Reviewed by Claude Fable (APPROVE, three low findings; two addressed, the third is a pre-existing server-account document-window sync gap, noted as a follow-up). Hub CI passed on fix/workspace-init-loop 9a3c421.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
4 files changed,
+110
−3
native/apple/Sources/DocsKit/DocsOfflineAccount.swift+20 −2native/apple/Sources/DocsKit/DocsWorkspace.swift+6 −1native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift+78 −0native/ios/project.yml+6 −0
CI passed — run details
diff --git a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift| index f796354..d76c85c 100644 |
| --- a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| +++ b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| @@ -16,10 +16,20 @@ struct DocsOfflineMetadata: Codable { |
| let outbox: Outbox |
| let connectivity: NativeConnectivity |
| let replicaCache: DocsReplicaCache |
| - private var supplier: @MainActor () -> NativeTransport? |
| + /// Never observed: a view reads the drainer's state, not which closure |
| + /// supplies its transport. |
| + @ObservationIgnored private var supplier: @MainActor () -> NativeTransport? |
| private(set) var drainer: OutboxDrainer! |
| + /// The account's shared store, outbox and drainer, made on first use. |
| + /// Called from a view's initialiser, which runs inside its window's body |
| + /// on every pass: anything observed it reads joins that body's |
| + /// dependencies, and anything observed it mutates (the drainer's |
| + /// backoff, its session, this supplier) invalidates them. Docs |
| + /// 0.1.7/0.1.8 did both here, so the body re-ran for ever and the main |
| + /// thread spun at launch. So: a lookup, nothing else. A workspace calls |
| + /// `resume(transport:)` from its `.task` instead. |
| static func account(_ id: String, origin: URL, fallback: NativeTransport, transport: (@MainActor () -> NativeTransport?)?) -> DocsOfflineAccount { |
| - if let existing = accounts[id] { if let transport { existing.supplier = transport; existing.drainer.sessionRestored() }; return existing } |
| + if let existing = accounts[id] { return existing } |
| let root = accountsRoot.appendingPathComponent(id) |
| let store = NativeSealedStore(root: root, accountID: id, keys: keys(accountID: id, origin: origin)) |
| let account = DocsOfflineAccount(store: store, accountID: id, connectivity: .shared, transport: transport ?? { fallback }) |
| @@ -69,6 +79,14 @@ struct DocsOfflineMetadata: Codable { |
| /// The account's session was replaced (Oficina ID re-minted it): an |
| /// outbox that stopped on the old one's 401 sends again. |
| static func sessionRestored(_ id: String) { accounts[id]?.drainer.sessionRestored() } |
| + |
| + /// A workspace for this account appeared with a session (a sign-in made |
| + /// a new one): its outbox sends through it, and sends again if it had |
| + /// stopped on the old session's 401. |
| + func resume(transport: @escaping @MainActor () -> NativeTransport?) { |
| + supplier = transport |
| + drainer.sessionRestored() |
| + } |
| init(store: BlobStore, accountID: String, connectivity: NativeConnectivity, transport: @escaping @MainActor () -> NativeTransport?) { |
| self.store = store; outbox = Outbox(store: store, account: accountID) |
| replicaCache = DocsReplicaCache(store: store) |
diff --git a/native/apple/Sources/DocsKit/DocsWorkspace.swift b/native/apple/Sources/DocsKit/DocsWorkspace.swift| index 9a26437..6732e8e 100644 |
| --- a/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| +++ b/native/apple/Sources/DocsKit/DocsWorkspace.swift |
| @@ -35,6 +35,7 @@ import SwiftUI |
| private let accounts: (() -> Void)? |
| private let client: NativeHTTP |
| private let transport: (@MainActor () -> NativeTransport?)? |
| + private let accountID: String? |
| /// Why a sign-out asked from Settings was refused, said rather than swallowed. |
| @State private var signOutRefusal: DocsLeaveBlock? |
| /// `transport` supplies the account's current session to the model on every |
| @@ -43,6 +44,7 @@ import SwiftUI |
| self._signOutRequested = signOutRequested; self.accountsLabel = accountsLabel; self.signOutLabel = signOutLabel |
| self.updateRecents = updateRecents; self.canSignOut = canSignOut; self._createRequested = createRequested; self._accountsRequested = accountsRequested; self.documentID = documentID; self.openDocument = openDocument |
| self.newDocument = newDocument; self.createDocument = createDocument; self.documentChanged = documentChanged |
| + self.accountID = accountID |
| let offline = accountID.map { DocsOfflineAccount.account($0, origin: client.origin, fallback: client, transport: transport) } |
| _model = State(initialValue: DocsModel(transport: transport ?? { client }, draft: newDocument, offlineAccount: offline)); self.signOut = signOut; self.accounts = accounts; self.client = client; self.transport = transport |
| } |
| @@ -124,7 +126,10 @@ import SwiftUI |
| .onChange(of: createRequested, initial: true) { _, requested in |
| if requested && documentID == nil { beginDocument(); createRequested = false } |
| } |
| - .task { if newDocument { await model.newDraft() } else if let documentID { await model.open(documentID) } else { await model.load() } } |
| + .task { |
| + // Here, not in init: see DocsOfflineAccount.account. |
| + if accountID != nil, let transport { model.offlineAccount.resume(transport: transport) } |
| + if newDocument { await model.newDraft() } else if let documentID { await model.open(documentID) } else { await model.load() } } |
| .onChange(of: model.renameInput) { _, value in if value == nil { renaming = false } } |
| .onChange(of: [model.detail?.sqid, model.detail?.title]) { |
| if !model.isDraft, let detail = model.detail { documentChanged?(detail.sqid, detail.title) } |
diff --git a/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift b/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift| new file mode 100644 |
| index 0000000..e3a42ef |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift |
| @@ -0,0 +1,78 @@ |
| +import Foundation |
| +import Observation |
| +import Synchronization |
| +import RastrilloApple |
| +import RastrilloTestSupport |
| +import XCTest |
| +@testable import DocsKit |
| + |
| +/// A workspace's initialiser runs on every body pass of its window, so it |
| +/// must leave alone everything a view watches. Docs 0.1.7 and 0.1.8 restored |
| +/// the account's session from there: the window's sync banner saw the |
| +/// drainer change, re-ran the body, which made a workspace again, which |
| +/// changed the drainer again — the main thread spun at launch for anyone |
| +/// with an account (a beachball on the Mac, the watchdog on iPhone). |
| +@MainActor final class WorkspaceInitTests: XCTestCase { |
| + private func knownAccount(_ transport: @escaping @MainActor () -> NativeTransport?) -> (String, DocsOfflineAccount) { |
| + let id = "workspace-init-\(UUID().uuidString)" |
| + let offline = DocsOfflineAccount(store: EphemeralDocsStore(), accountID: id, |
| + connectivity: NativeConnectivity(source: ScriptedPathSource(.satisfied)), |
| + transport: transport) |
| + DocsOfflineAccount.accounts[id] = offline |
| + return (id, offline) |
| + } |
| + |
| + func testLookingUpAKnownAccountChangesNothingAViewWatches() { |
| + let server = ScriptedTransport([]) |
| + let (id, offline) = knownAccount({ server }) |
| + defer { DocsOfflineAccount.accounts[id] = nil } |
| + let changed = Mutex(false) |
| + withObservationTracking { |
| + // What the window reads: the sync banner's status, and the account itself. |
| + _ = offline.drainer.status() |
| + _ = offline.drainer.sessionEnded |
| + _ = offline.drainer.phase |
| + _ = offline.drainer |
| + } onChange: { changed.withLock { $0 = true } } |
| + |
| + for _ in 0..<3 { |
| + let again = DocsOfflineAccount.account(id, origin: URL(string: "https://docs.example")!, fallback: server, transport: { server }) |
| + XCTAssertTrue(again === offline, "one account per ID") |
| + } |
| + XCTAssertFalse(changed.withLock { $0 }, "a lookup made from a view's init must not invalidate the view") |
| + } |
| + |
| + func testBuildingAWorkspaceForAKnownAccountChangesNothingItsWindowWatches() { |
| + let server = ScriptedTransport([]) |
| + let (id, offline) = knownAccount({ server }) |
| + defer { DocsOfflineAccount.accounts[id] = nil } |
| + let client = NativeHTTP(origin: URL(string: "https://docs.example")!) |
| + let changed = Mutex(false) |
| + // As a window's body does: build the workspace (its init reads |
| + // whatever it reads), then build it again on the next pass. |
| + withObservationTracking { |
| + _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| + _ = offline.drainer.status() |
| + } onChange: { changed.withLock { $0 = true } } |
| + for _ in 0..<3 { |
| + _ = DocsWorkspace(client: client, accountID: id, transport: { server }, signOut: {}) |
| + } |
| + XCTAssertFalse(changed.withLock { $0 }, "a workspace's init must not invalidate the window that builds it") |
| + } |
| + |
| + func testResumeRestartsAnOutboxStoppedByTheEndedSession() async throws { |
| + var supplied: NativeTransport? = nil |
| + let (id, offline) = knownAccount({ supplied }) |
| + defer { DocsOfflineAccount.accounts[id] = nil } |
| + try await offline.outbox.enqueue(lane: "doc:k", kind: "update", payload: Data([1])) |
| + let stopped = await offline.drainer.drain() |
| + XCTAssertEqual(stopped, .sessionEnded) |
| + XCTAssertTrue(offline.drainer.sessionEnded) |
| + |
| + // Signing in again mounts a new workspace; it resumes when it appears. |
| + let server = ScriptedTransport([]) |
| + supplied = server |
| + offline.resume(transport: { server }) |
| + XCTAssertFalse(offline.drainer.sessionEnded, "the new session restarts the stopped outbox") |
| + } |
| +} |
diff --git a/native/ios/project.yml b/native/ios/project.yml| index ee5dea5..5e04e4f 100644 |
| --- a/native/ios/project.yml |
| +++ b/native/ios/project.yml |
| @@ -42,6 +42,12 @@ targets: |
| GENERATE_INFOPLIST_FILE: true |
| INFOPLIST_KEY_CFBundleDisplayName: Oficina Docs |
| INFOPLIST_KEY_UILaunchScreen_Generation: true |
| + # App Store Connect refuses an iPad-capable upload without these |
| + # (all four on iPad, for multitasking). |
| + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPhone: UIInterfaceOrientationPortrait UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight |
| + # Docs uses only the system's HTTPS: exempt (as build 10 declared). |
| + INFOPLIST_KEY_ITSAppUsesNonExemptEncryption: false |
| + INFOPLIST_KEY_UISupportedInterfaceOrientations_iPad: UIInterfaceOrientationPortrait UIInterfaceOrientationPortraitUpsideDown UIInterfaceOrientationLandscapeLeft UIInterfaceOrientationLandscapeRight |
| info: |
| path: Info.plist |
| properties: |