Stop the launch spin: a workspace's init no longer changes what its window watches
Docs 0.1.7 and 0.1.8 hung at launch for anyone with an account: a beachball on the Mac (Paul's sample: main thread 100% in SwiftUI flushTransactions, DocsAccountWindow.body -> DocsWorkspace.init -> DocsOfflineAccount.account -> drainer.sessionRestored), and on iPhone the watchdog kill TestFlight reported as a crash on 11-16. DocsWorkspace.init called DocsOfflineAccount.account, which for a known account assigned its transport supplier and restored the drainer's session. The window's sync banner reads drainer.status(), which calls the supplier, so the supplier (a closure, never equal to the last one) was an observed dependency: every assignment invalidated the window, whose body built a workspace again, which assigned again, for ever. It only bit with an account, which is why a clean machine launched fine. account() is now a pure lookup-or-create; the supplier is @ObservationIgnored; and the workspace resumes its account (new supplier, restored session) from its .task, once per appearance, which is when a sign-in mounts a new one. WorkspaceInitTests fails on the old code (verified) and passes now; the full Swift suite passes (124 tests, 4 skipped).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
3 files changed,
+84
−3
native/apple/Sources/DocsKit/DocsOfflineAccount.swift+18 −2native/apple/Sources/DocsKit/DocsWorkspace.swift+6 −1native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift+60 −0
diff --git a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift| index f796354..b36026c 100644 |
| --- a/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| +++ b/native/apple/Sources/DocsKit/DocsOfflineAccount.swift |
| @@ -16,10 +16,18 @@ 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, so it must change no observed |
| + /// state: a workspace's init runs on every body pass of its window, and |
| + /// a mutation here re-ran that body, for ever (Docs 0.1.7/0.1.8 spun the |
| + /// main thread at launch). A workspace calls `resume(transport:)` when it |
| + /// appears 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 +77,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..0307d09 |
| --- /dev/null |
| +++ b/native/apple/Tests/DocsKitTests/WorkspaceInitTests.swift |
| @@ -0,0 +1,60 @@ |
| +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 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") |
| + } |
| +} |