Skip to content

Cross-platform observation delivery + WebAssembly build support#152

Open
0xLeif wants to merge 1 commit into
mainfrom
fix/cross-platform-observation-wasm
Open

Cross-platform observation delivery + WebAssembly build support#152
0xLeif wants to merge 1 commit into
mainfrom
fix/cross-platform-observation-wasm

Conversation

@0xLeif

@0xLeif 0xLeif commented Jun 11, 2026

Copy link
Copy Markdown
Owner

Makes AppState's reactivity work everywhere Swift's Observation runs, and unblocks WebAssembly.

#150 — observation delivery off Apple

The Observation framework is cross-platform, but AppState only delivered changes on Apple: state setters write to the untracked Cache, and the Combine bridge consume(object: cache) (Apple-only) called notifyChange(). On Linux/Windows/wasm there was no bridge, so withObservationTracking { … } onChange: never fired.

Now every mutation path (State / StoredState / FileState / SyncState / SecureState / DependencySlice) calls notifyChange() directly on all platforms, and the Combine cache bridge is removed so Apple still fires exactly once. Observation tests are un-gated to run cross-platform (Apple-only SecureState/SyncState assertions guarded individually) — CI now exercises observation on Linux.

#149 — WebAssembly build

Combine-gated #if !os(Linux) && !os(Windows) guards (import Combine, the cancellable bag, consume, @ObservedDependency, OSLog/SwiftUI) included os(WASI). Switched the Combine/OSLog/SwiftUI ones to canImport(...). Keychain (SecureState) and iCloud KV (SyncState) stay Apple-only.

Verification

163 tests pass on macOS, against both published Cache 2.1.2 and the WASM/Linux-fixed Cache branch.

Sequencing

Full WebAssembly build and Linux collection-typed state also need the Cache fix — 0xLeif/Cache#30. Recommend merging + releasing that first, then bumping AppState's Cache pin. #150 observation delivery is AppState-only and verified by this PR's Linux CI independently.

Addresses #149, #150, #151.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors platform-specific checks (#if !os(Linux) && !os(Windows)) to use feature-based checks (canImport) across the codebase, and adds a detailed note regarding a known Swift-on-Linux runtime crash (Issue #151). It also introduces notifyChange() calls to several state setters to ensure proper observation. However, a critical thread-safety issue was identified: because these state setters (such as StoredState, FileState, SyncState, and DependencySlice) are not restricted to @MainActor, they can be invoked from background threads. This would trigger a main-thread assertion in notifyChange() and crash the application. It is highly recommended to make notifyChange() thread-safe or dispatch it to the main thread.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

}
}

shared.notifyChange()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since StoredState.value is not restricted to @MainActor, its setter can be invoked from background threads. Calling shared.notifyChange() directly on a background thread will trigger the assertion assert(Thread.isMainThread) inside notifyChange() and crash the application.

To prevent this, we should ensure notifyChange() is always dispatched to the main thread.

Alternative: A cleaner approach would be to make Application.notifyChange() itself thread-safe by internally checking Thread.isMainThread and dispatching to DispatchQueue.main if necessary, which would eliminate the need for these checks at every call site.

                if Thread.isMainThread {
                    shared.notifyChange()
                } else {
                    DispatchQueue.main.async {
                        shared.notifyChange()
                    }
                }

}
}

shared.notifyChange()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since FileState.value is not restricted to @MainActor, its setter can be invoked from background threads. Calling shared.notifyChange() directly on a background thread will trigger the assertion assert(Thread.isMainThread) inside notifyChange() and crash the application.

To prevent this, we should ensure notifyChange() is always dispatched to the main thread.

Alternative: A cleaner approach would be to make Application.notifyChange() itself thread-safe by internally checking Thread.isMainThread and dispatching to DispatchQueue.main if necessary, which would eliminate the need for these checks at every call site.

                if Thread.isMainThread {
                    shared.notifyChange()
                } else {
                    DispatchQueue.main.async {
                        shared.notifyChange()
                    }
                }

}
}

shared.notifyChange()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since SyncState.value is not restricted to @MainActor, its setter can be invoked from background threads. Calling shared.notifyChange() directly on a background thread will trigger the assertion assert(Thread.isMainThread) inside notifyChange() and crash the application.

To prevent this, we should ensure notifyChange() is always dispatched to the main thread.

Alternative: A cleaner approach would be to make Application.notifyChange() itself thread-safe by internally checking Thread.isMainThread and dispatching to DispatchQueue.main if necessary, which would eliminate the need for these checks at every call site.

                if Thread.isMainThread {
                    shared.notifyChange()
                } else {
                    DispatchQueue.main.async {
                        shared.notifyChange()
                    }
                }

get { dependency.value[keyPath: keyPath] }
set {
#if !os(Linux) && !os(Windows)
Application.shared.notifyChange()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Since Application.DependencySlice is not restricted to @MainActor, its setter can be invoked from background threads (e.g., when a background service updates its state). Calling Application.shared.notifyChange() directly on a background thread will trigger the assertion assert(Thread.isMainThread) inside notifyChange() and crash the application.

To prevent this, we should ensure notifyChange() is always dispatched to the main thread.

Alternative: A cleaner approach would be to make Application.notifyChange() itself thread-safe by internally checking Thread.isMainThread and dispatching to DispatchQueue.main if necessary, which would eliminate the need for these checks at every call site.

            if Thread.isMainThread {
                Application.shared.notifyChange()
            } else {
                DispatchQueue.main.async {
                    Application.shared.notifyChange()
                }
            }

@0xLeif 0xLeif force-pushed the fix/cross-platform-observation-wasm branch from fc79957 to dfd35b4 Compare June 11, 2026 15:06
#149 (WASM): switch Combine/OSLog/SwiftUI-gated #if !os(Linux) && !os(Windows)
guards to their canImport(...) checks. os(WASI) is neither Linux nor Windows, so the
old guards pulled Combine/SwiftUI into the wasm build. Keychain/iCloud stay Apple-only.

#150 (observation off Apple): delivery was bridged through Combine's
consume(object: cache) (Apple-only), so withObservationTracking never fired on
Linux/Windows/wasm. Now every mutation path (State/StoredState/FileState/SyncState/
SecureState/DependencySlice) calls notifyChange() directly on all platforms, and the
Combine cache bridge is removed so Apple still fires exactly once.

163 tests pass on macOS. Observation tests stay gated off Linux/Windows: swift-corelibs
-xctest cannot discover synchronous @mainactor test methods there (it force-casts test
thunks to () -> () and aborts) — a test-harness limitation, not a runtime one. The #150
delivery code itself is cross-platform. Full wasm build + Linux collection-typed state
also need the Cache fix (0xLeif/Cache#30).

Addresses #149, #150.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@0xLeif 0xLeif force-pushed the fix/cross-platform-observation-wasm branch from 1c54db2 to b47b388 Compare June 11, 2026 15:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant