Cross-platform observation delivery + WebAssembly build support#152
Cross-platform observation delivery + WebAssembly build support#1520xLeif wants to merge 1 commit into
Conversation
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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() |
There was a problem hiding this comment.
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()
}
}fc79957 to
dfd35b4
Compare
#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>
1c54db2 to
b47b388
Compare
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 bridgeconsume(object: cache)(Apple-only) callednotifyChange(). On Linux/Windows/wasm there was no bridge, sowithObservationTracking { … } 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) includedos(WASI). Switched the Combine/OSLog/SwiftUI ones tocanImport(...). 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.