-
Notifications
You must be signed in to change notification settings - Fork 61
[Fix] OPFS Multitab Deadlocks #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
base: main
Are you sure you want to change the base?
Changes from 13 commits
e61059a
a12145f
b4f5c1b
355e396
c6ba9f6
9730337
58f412b
8721538
feca863
b0dd596
de7804f
c08e664
aedc855
62b04de
0fc0d73
91db686
deaf83c
3780223
8f95e10
1e880d6
da53396
28472e3
11b7a36
7e7ec91
1e9fa3f
07231cb
cbfb683
f881992
ffe5abe
58d9194
1ef44a3
d73d9d2
a1fd0bb
445ec69
6630097
bbfdcf7
8cfc0e6
abdec98
9cadf24
4a12be8
eb88fde
423e4e4
59b9ce9
23ae289
4108c8e
cf14827
0f1cb7c
9a571f1
33f701e
093312e
c72c2dd
cac6202
977b2a0
b48a685
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -26,6 +26,7 @@ export interface LockedAsyncDatabaseAdapterOptions { | |
| openConnection: () => Promise<AsyncDatabaseConnection>; | ||
| debugMode?: boolean; | ||
| logger?: ILogger; | ||
| defaultLockTimeoutMs?: number; | ||
| } | ||
|
|
||
| export type LockedAsyncDatabaseAdapterListener = DBAdapterListener & { | ||
|
|
@@ -196,7 +197,7 @@ export class LockedAsyncDatabaseAdapter | |
| return this.acquireLock( | ||
| async () => fn(this.generateDBHelpers({ execute: this._execute, executeRaw: this._executeRaw })), | ||
| { | ||
| timeoutMs: options?.timeoutMs | ||
| timeoutMs: options?.timeoutMs ?? this.options.defaultLockTimeoutMs | ||
|
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 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. |
||
| } | ||
| ); | ||
| } | ||
|
|
@@ -206,7 +207,7 @@ export class LockedAsyncDatabaseAdapter | |
| return this.acquireLock( | ||
| async () => fn(this.generateDBHelpers({ execute: this._execute, executeRaw: this._executeRaw })), | ||
| { | ||
| timeoutMs: options?.timeoutMs | ||
| timeoutMs: options?.timeoutMs ?? this.options.defaultLockTimeoutMs | ||
| } | ||
| ); | ||
| } | ||
|
|
||
| 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(); | ||
| }); | ||
| } | ||
| }); | ||
| } | ||
|
|
||
| /** | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.