-
Notifications
You must be signed in to change notification settings - Fork 12
Multi-process database support: absolute paths, open retry, cross-process change signal #147
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
simolus3
merged 4 commits into
powersync-ja:main
from
Chubby-Studio:feature/multiprocess-core
Jun 29, 2026
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
f8a2a71
Multi-process database support: absolute paths, open retry, cross-pro…
asiergmorato dac1f69
Address review on multi-process core changes
asiergmorato 541e7f9
Address minor review comments on multi-process core
asiergmorato e059a3a
Merge branch 'main' into feature/multiprocess-core
asiergmorato File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
75 changes: 75 additions & 0 deletions
75
Sources/PowerSync/Implementation/CrossProcessChangeSignal.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,75 @@ | ||
| import Foundation | ||
| import notify | ||
|
|
||
| /// Cross-process change signaling for a database file, built on Darwin notifications — | ||
| /// the same mechanism Core Data uses for its remote-change notifications. | ||
| /// | ||
| /// PowerSync's update hooks only observe writes made through the local connection pool. | ||
| /// When several processes share a database file (an app and its widgets or App Intents | ||
| /// extensions), each process posts this signal after committing a write; the others | ||
| /// re-emit their `tableUpdates` with ``EXTERNAL_CHANGES_MARKER`` so `watch` queries | ||
| /// re-run and the upload client checks `ps_crud`. | ||
| /// | ||
| /// Darwin notifications carry no payload and are coalesced under pressure, which is fine: | ||
| /// the marker means "something changed, re-query". Deliveries to the posting process | ||
| /// itself are not suppressed — a redundant re-query (already throttled) is preferable to | ||
| /// the race a sender-stamp suppression scheme introduces, where an external change could | ||
| /// be misattributed and silently dropped. | ||
| final class CrossProcessChangeSignal: @unchecked Sendable { | ||
| private let name: String | ||
| private let logger: any LoggerProtocol | ||
| private var token: Int32 = NOTIFY_TOKEN_INVALID | ||
| private let queue = DispatchQueue(label: "powersync.cross-process-signal") | ||
|
|
||
| init(databasePath: String, logger: any LoggerProtocol) { | ||
| // Stable across processes: both sides derive the name from the canonical path. | ||
| let canonical = URL(fileURLWithPath: databasePath).standardizedFileURL.path | ||
| self.name = "com.powersync.changes.\(Self.fnv1a(canonical))" | ||
| self.logger = logger | ||
| } | ||
|
|
||
| /// Starts listening; `onExternalChange` runs on a private queue for every signal | ||
| /// (including this process's own posts). | ||
| func start(onChange: @escaping @Sendable () -> Void) { | ||
| guard token == NOTIFY_TOKEN_INVALID else { | ||
| return | ||
| } | ||
| let status = notify_register_dispatch(name, &token, queue) { _ in | ||
| onChange() | ||
| } | ||
| if status != NOTIFY_STATUS_OK { | ||
| logger.warning( | ||
| "could not register cross-process change signal (status \(status)); " | ||
| + "changes from other processes will not wake watch queries", | ||
| tag: "CrossProcessChangeSignal" | ||
| ) | ||
| token = NOTIFY_TOKEN_INVALID | ||
| } | ||
| } | ||
|
|
||
| /// Posts the signal; called after every committed write. | ||
| func post() { | ||
| notify_post(name) | ||
| } | ||
|
|
||
| func stop() { | ||
| if token != NOTIFY_TOKEN_INVALID { | ||
| notify_cancel(token) | ||
| token = NOTIFY_TOKEN_INVALID | ||
| } | ||
| } | ||
|
|
||
| deinit { | ||
| stop() | ||
| } | ||
|
|
||
| /// FNV-1a 64-bit, hex-encoded: deterministic and dependency-free. | ||
| private static func fnv1a(_ input: String) -> String { | ||
| var hash: UInt64 = 0xCBF2_9CE4_8422_2325 | ||
| for byte in input.utf8 { | ||
| hash ^= UInt64(byte) | ||
| hash = hash &* 0x0000_0100_0000_01B3 | ||
| } | ||
| return String(hash, radix: 16) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.