-
Notifications
You must be signed in to change notification settings - Fork 60
[Fix] OPFS Multitab issue #786
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
Draft
Chriztiaan
wants to merge
47
commits into
main
Choose a base branch
from
fix/opfs-multitab-issue
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 13 commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
e61059a
No longer awaiting when aborting connection on tab closure. Fixes som…
Chriztiaan a12145f
fix opfs deadlocks
stevensJourney b4f5c1b
Add multiple tabs test
stevensJourney 355e396
enable headless
stevensJourney c6ba9f6
reenable browsers
stevensJourney 9730337
cleanup tests
stevensJourney 58f412b
Merge branch 'dead-tab-detection' into fix/opfs-multitab-issue
stevensJourney 8721538
Add default lock timeout for shared sync workers. Add flagging for u…
stevensJourney feca863
fix broken test
stevensJourney b0dd596
cleanup tests
stevensJourney de7804f
restore collectActiveSubscriptions
stevensJourney c08e664
cleanup code flow
stevensJourney aedc855
Add withTimeout for opening db connections
stevensJourney 62b04de
reduce number of iframes for CI
stevensJourney 0fc0d73
Use a distributed database adapter instead of reconnecting. Retry ope…
stevensJourney 91db686
Catch closed errors for hold requests. Use crud throttle time for cru…
stevensJourney deaf83c
Synchronize lock requests better.
stevensJourney 3780223
Re-open database as soon as it's closed. Trigger uploads if database …
stevensJourney 8f95e10
Update tabs test to be more indicative of the actual issues.
stevensJourney 1e880d6
Add mocked sync tests for shared webworkers
stevensJourney da53396
Update tests for shared web workers
stevensJourney 28472e3
Add automatic responses or mocked sync service.
stevensJourney 11b7a36
Update more tests to use mocked sync service
stevensJourney 7e7ec91
increase timeout for ci
stevensJourney 1e9fa3f
Improve test stability
stevensJourney 07231cb
cleanup port assignments and init flow
stevensJourney cbfb683
cleanup test code
stevensJourney f881992
Listen for database close events to catch closed items earlier.
stevensJourney ffe5abe
Fire and forget close operations.
stevensJourney 58d9194
Common changeset.
Chriztiaan 1ef44a3
Try and catch Accesshandle errors. Fork OPFSCoopSyncVFS for potential…
stevensJourney d73d9d2
Cleanup aborted operations if aborted. Try and gracefully handle Acce…
stevensJourney a1fd0bb
cleanup
stevensJourney 445ec69
add a finalization registry entry to release access handles
stevensJourney 6630097
Wrap entire open operation init in writeLock.
stevensJourney bbfdcf7
revert opfs test
stevensJourney 8cfc0e6
update wa-sqlite
stevensJourney abdec98
Update changesets
stevensJourney 9cadf24
try stabilize test
stevensJourney 4a12be8
increase timeouts
stevensJourney eb88fde
cleanup any opened access handles if an error is caught.
stevensJourney 423e4e4
cleanup tests structure
stevensJourney 59b9ce9
remove dev logs
stevensJourney 23ae289
fix types
stevensJourney 4108c8e
Changesets for Drizzle/Kysely.
Chriztiaan cf14827
use latest dev packages
stevensJourney 0f1cb7c
Code cleanup. make minor bump.
stevensJourney 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@powersync/web': patch | ||
| --- | ||
|
|
||
| No longer awaiting when aborting connection on tab closure. Fixes some edge cases where multiple tabs with OPFS/Safari can cause deadlocks. |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,16 +6,16 @@ import { | |
| SyncStatusOptions | ||
| } from '@powersync/common'; | ||
| import * as Comlink from 'comlink'; | ||
| import { getNavigatorLocks } from '../../shared/navigator'; | ||
| import { AbstractSharedSyncClientProvider } from '../../worker/sync/AbstractSharedSyncClientProvider'; | ||
| import { ManualSharedSyncPayload, SharedSyncClientEvent } from '../../worker/sync/SharedSyncImplementation'; | ||
| import { DEFAULT_CACHE_SIZE_KB, resolveWebSQLFlags, TemporaryStorageOption } from '../adapters/web-sql-flags'; | ||
| import { WorkerClient } from '../../worker/sync/WorkerClient'; | ||
| import { WebDBAdapter } from '../adapters/WebDBAdapter'; | ||
| import { DEFAULT_CACHE_SIZE_KB, TemporaryStorageOption, resolveWebSQLFlags } from '../adapters/web-sql-flags'; | ||
| import { | ||
| WebStreamingSyncImplementation, | ||
| WebStreamingSyncImplementationOptions | ||
| } from './WebStreamingSyncImplementation'; | ||
| import { WorkerClient } from '../../worker/sync/WorkerClient'; | ||
| import { getNavigatorLocks } from '../../shared/navigator'; | ||
|
|
||
| /** | ||
| * The shared worker will trigger methods on this side of the message port | ||
|
|
@@ -146,7 +146,25 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem | |
| ).port; | ||
| } | ||
|
|
||
| /** | ||
| * Pass along any sync status updates to this listener | ||
| */ | ||
| this.clientProvider = new SharedSyncClientProvider( | ||
| this.webOptions, | ||
| (status) => { | ||
| this.updateSyncStatus(status); | ||
| }, | ||
| options.db | ||
| ); | ||
|
|
||
| this.syncManager = Comlink.wrap<WorkerClient>(this.messagePort); | ||
| /** | ||
| * The sync worker will call this client provider when it needs | ||
| * to fetch credentials or upload data. | ||
| * This performs bi-directional method calling. | ||
| */ | ||
| Comlink.expose(this.clientProvider, this.messagePort); | ||
|
|
||
| this.syncManager.setLogLevel(this.logger.getLevel()); | ||
|
|
||
| this.triggerCrudUpload = this.syncManager.triggerCrudUpload; | ||
|
|
@@ -157,10 +175,47 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem | |
| * DB worker, but a port to the DB worker can be transferred to the | ||
| * sync worker. | ||
| */ | ||
|
|
||
| this.isInitialized = this._init(); | ||
| } | ||
|
|
||
| protected async _init() { | ||
| /** | ||
| * The general flow of initialization is: | ||
| * - The client requests a unique navigator lock. | ||
| * - Once the lock is acquired, we register the lock with the shared worker. | ||
| * - The shared worker can then request the same lock. The client has been closed if the shared worker can acquire the lock. | ||
| * - Once the shared worker knows the client's lock, we can guarentee that the shared worker will detect if the client has been closed. | ||
| * - This makes the client safe for the shared worker to use. | ||
| * - The client side lock is held until the client is disposed. | ||
| * - We resolve the top-level promise after the lock has been registered with the shared worker. | ||
| * - The client sends the params to the shared worker after locks have been registered. | ||
| */ | ||
| await new Promise<void>((resolve) => { | ||
| // Request a random lock until this client is disposed. The name of the lock is sent to the shared worker, which | ||
| // will also attempt to acquire it. Since the lock is returned when the tab is closed, this allows the share worker | ||
| // to free resources associated with this tab. | ||
| // We take hold of this lock as soon-as-possible in order to cater for potentially closed tabs. | ||
| getNavigatorLocks().request(`tab-close-signal-${crypto.randomUUID()}`, async (lock) => { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The initialisation flow here is meant to assist the Shared Sync worker to only use clients which have a navigator-lock based close detection established. We don't want to use clients which might have been registered, but closed before the lock has been configured. |
||
| if (this.abortOnClose.signal.aborted) { | ||
| return; | ||
| } | ||
| // Awaiting here ensures the worker is waiting for the lock | ||
| await this.syncManager.addLockBasedCloseSignal(lock!.name); | ||
|
|
||
| // The lock has been registered, we can continue with the initialization | ||
| resolve(); | ||
|
|
||
| await new Promise<void>((r) => { | ||
| this.abortOnClose.signal.onabort = () => r(); | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| const { crudUploadThrottleMs, identifier, retryDelayMs } = this.options; | ||
| const flags = { ...this.webOptions.flags, workers: undefined }; | ||
|
|
||
| this.isInitialized = this.syncManager.setParams( | ||
| await this.syncManager.setParams( | ||
| { | ||
| dbParams: this.dbAdapter.getConfiguration(), | ||
| streamOptions: { | ||
|
|
@@ -170,39 +225,8 @@ export class SharedWebStreamingSyncImplementation extends WebStreamingSyncImplem | |
| flags: flags | ||
| } | ||
| }, | ||
| options.subscriptions | ||
| ); | ||
|
|
||
| /** | ||
| * Pass along any sync status updates to this listener | ||
| */ | ||
| this.clientProvider = new SharedSyncClientProvider( | ||
| this.webOptions, | ||
| (status) => { | ||
| this.iterateListeners((l) => this.updateSyncStatus(status)); | ||
| }, | ||
| options.db | ||
| this.options.subscriptions | ||
| ); | ||
|
|
||
| /** | ||
| * The sync worker will call this client provider when it needs | ||
| * to fetch credentials or upload data. | ||
| * This performs bi-directional method calling. | ||
| */ | ||
| Comlink.expose(this.clientProvider, this.messagePort); | ||
|
|
||
| // Request a random lock until this client is disposed. The name of the lock is sent to the shared worker, which | ||
| // will also attempt to acquire it. Since the lock is returned when the tab is closed, this allows the share worker | ||
| // to free resources associated with this tab. | ||
| getNavigatorLocks().request(`tab-close-signal-${crypto.randomUUID()}`, async (lock) => { | ||
| if (!this.abortOnClose.signal.aborted) { | ||
| this.syncManager.addLockBasedCloseSignal(lock!.name); | ||
|
|
||
| await new Promise<void>((r) => { | ||
| this.abortOnClose.signal.onabort = () => r(); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The timeouts here are meant to help expose potentially held locks. E.g. in the past we've seen some DBAdapters created in the shared sync worker - which were not completely closed - could hold on to a lock indefinitely.
Without a timeout it just seems like the whole process is stuck. With a timeout, we'll at least get some indication of this.