-
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
base: main
Are you sure you want to change the base?
Changes from 33 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
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| '@powersync/common': minor | ||
| --- | ||
|
|
||
| Serializing upload and download errors for SyncStatus events. Small changes to how delay values are passed to the sync implementation internally. |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,7 @@ | ||
| import { CoreStreamSubscription } from '../../client/sync/stream/core-instruction.js'; | ||
| import { SyncClientImplementation } from '../../client/sync/stream/AbstractStreamingSyncImplementation.js'; | ||
| import { InternalProgressInformation, ProgressWithOperations, SyncProgress } from './SyncProgress.js'; | ||
| import { CoreStreamSubscription } from '../../client/sync/stream/core-instruction.js'; | ||
| import { SyncStreamDescription, SyncSubscriptionDescription } from '../../client/sync/sync-streams.js'; | ||
| import { InternalProgressInformation, ProgressWithOperations, SyncProgress } from './SyncProgress.js'; | ||
|
|
||
| export type SyncDataFlowStatus = Partial<{ | ||
| downloading: boolean; | ||
|
|
@@ -250,13 +250,28 @@ export class SyncStatus { | |
| return { | ||
| connected: this.connected, | ||
| connecting: this.connecting, | ||
| dataFlow: this.dataFlowStatus, | ||
| dataFlow: { | ||
| ...this.dataFlowStatus, | ||
| uploadError: this.serializeError(this.dataFlowStatus.uploadError), | ||
| downloadError: this.serializeError(this.dataFlowStatus.downloadError) | ||
| }, | ||
| lastSyncedAt: this.lastSyncedAt, | ||
| hasSynced: this.hasSynced, | ||
| priorityStatusEntries: this.priorityStatusEntries | ||
| }; | ||
| } | ||
|
|
||
| protected serializeError(error?: Error) { | ||
|
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. Not all errors are serialisable over a MessagePort. E.g. some |
||
| if (typeof error == 'undefined') { | ||
| return undefined; | ||
| } | ||
| return { | ||
| name: error.name, | ||
| message: error.message, | ||
| stack: error.stack | ||
| }; | ||
| } | ||
|
|
||
| private static comparePriorities(a: SyncPriorityStatus, b: SyncPriorityStatus) { | ||
| return b.priority - a.priority; // Reverse because higher priorities have lower numbers | ||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,31 @@ | ||
| import { | ||
| type BucketStorageAdapter, | ||
| type PowerSyncBackendConnector, | ||
| type PowerSyncCloseOptions, | ||
| type RequiredAdditionalConnectionOptions, | ||
| AbstractPowerSyncDatabase, | ||
| DBAdapter, | ||
| DEFAULT_POWERSYNC_CLOSE_OPTIONS, | ||
| isDBAdapter, | ||
| isSQLOpenFactory, | ||
| PowerSyncDatabaseOptions, | ||
| PowerSyncDatabaseOptionsWithDBAdapter, | ||
| PowerSyncDatabaseOptionsWithOpenFactory, | ||
| PowerSyncDatabaseOptionsWithSettings, | ||
| SqliteBucketStorage, | ||
| StreamingSyncImplementation | ||
| StreamingSyncImplementation, | ||
| isDBAdapter, | ||
| isSQLOpenFactory, | ||
| type BucketStorageAdapter, | ||
| type PowerSyncBackendConnector, | ||
| type PowerSyncCloseOptions, | ||
| type RequiredAdditionalConnectionOptions | ||
| } from '@powersync/common'; | ||
| import { Mutex } from 'async-mutex'; | ||
| import { getNavigatorLocks } from '../shared/navigator'; | ||
| import { WebDBAdapter } from './adapters/WebDBAdapter'; | ||
| import { WASQLiteOpenFactory } from './adapters/wa-sqlite/WASQLiteOpenFactory'; | ||
| import { | ||
| DEFAULT_WEB_SQL_FLAGS, | ||
| ResolvedWebSQLOpenOptions, | ||
| resolveWebSQLFlags, | ||
| WebSQLFlags | ||
| WebSQLFlags, | ||
| resolveWebSQLFlags | ||
| } from './adapters/web-sql-flags'; | ||
| import { WebDBAdapter } from './adapters/WebDBAdapter'; | ||
| import { SharedWebStreamingSyncImplementation } from './sync/SharedWebStreamingSyncImplementation'; | ||
| import { SSRStreamingSyncImplementation } from './sync/SSRWebStreamingSyncImplementation'; | ||
| import { SharedWebStreamingSyncImplementation } from './sync/SharedWebStreamingSyncImplementation'; | ||
| import { WebRemote } from './sync/WebRemote'; | ||
| import { | ||
| WebStreamingSyncImplementation, | ||
|
|
@@ -160,14 +159,13 @@ export class PowerSyncDatabase extends AbstractPowerSyncDatabase { | |
| * By default the sync stream client is only disconnected if | ||
| * multiple tabs are not enabled. | ||
| */ | ||
| close(options: PowerSyncCloseOptions = DEFAULT_POWERSYNC_CLOSE_OPTIONS): Promise<void> { | ||
| close(options?: PowerSyncCloseOptions): Promise<void> { | ||
| if (this.unloadListener) { | ||
| window.removeEventListener('unload', this.unloadListener); | ||
| } | ||
|
|
||
| return super.close({ | ||
| // Don't disconnect by default if multiple tabs are enabled | ||
| disconnect: options.disconnect ?? !this.resolvedFlags.enableMultiTabs | ||
|
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 default param above actually causes the opposite of this to occur. A |
||
| disconnect: options?.disconnect ?? !this.resolvedFlags.enableMultiTabs | ||
| }); | ||
| } | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,7 +11,7 @@ import { | |
| type ILogger | ||
| } from '@powersync/common'; | ||
| import { getNavigatorLocks } from '../..//shared/navigator'; | ||
| import { AsyncDatabaseConnection } from './AsyncDatabaseConnection'; | ||
| import { AsyncDatabaseConnection, ConnectionClosedError } from './AsyncDatabaseConnection'; | ||
| import { SharedConnectionWorker, WebDBAdapter } from './WebDBAdapter'; | ||
| import { WorkerWrappedAsyncDatabaseConnection } from './WorkerWrappedAsyncDatabaseConnection'; | ||
| import { WASQLiteVFS } from './wa-sqlite/WASQLiteConnection'; | ||
|
|
@@ -26,10 +26,16 @@ export interface LockedAsyncDatabaseAdapterOptions { | |
| openConnection: () => Promise<AsyncDatabaseConnection>; | ||
| debugMode?: boolean; | ||
| logger?: ILogger; | ||
| defaultLockTimeoutMs?: number; | ||
| reOpenOnConnectionClosed?: boolean; | ||
| } | ||
|
|
||
| export type LockedAsyncDatabaseAdapterListener = DBAdapterListener & { | ||
| initialized?: () => void; | ||
| /** | ||
| * Fired when the database is re-opened after being closed. | ||
| */ | ||
| databaseReOpened?: () => void; | ||
| }; | ||
|
|
||
| /** | ||
|
|
@@ -51,6 +57,7 @@ export class LockedAsyncDatabaseAdapter | |
| private _config: ResolvedWebSQLOpenOptions | null = null; | ||
| protected pendingAbortControllers: Set<AbortController>; | ||
| protected requiresHolds: boolean | null; | ||
| protected databaseOpenPromise: Promise<void> | null = null; | ||
|
|
||
| closing: boolean; | ||
| closed: boolean; | ||
|
|
@@ -105,18 +112,52 @@ export class LockedAsyncDatabaseAdapter | |
| return this.initPromise; | ||
| } | ||
|
|
||
| protected async _init() { | ||
| protected async openInternalDB() { | ||
| // Dispose any previous table change listener. | ||
| this._disposeTableChangeListener?.(); | ||
| this._disposeTableChangeListener = null; | ||
|
|
||
| const isReOpen = !!this._db; | ||
|
|
||
| this._db = await this.options.openConnection(); | ||
| await this._db.init(); | ||
| this._config = await this._db.getConfig(); | ||
| await this.registerOnChangeListener(this._db); | ||
| this.iterateListeners((cb) => cb.initialized?.()); | ||
| if (isReOpen) { | ||
| this.iterateListeners((cb) => cb.databaseReOpened?.()); | ||
| } | ||
| /** | ||
| * This is only required for the long-lived shared IndexedDB connections. | ||
| */ | ||
| this.requiresHolds = (this._config as ResolvedWASQLiteOpenFactoryOptions).vfs == WASQLiteVFS.IDBBatchAtomicVFS; | ||
| } | ||
|
|
||
| protected _reOpen() { | ||
| this.databaseOpenPromise = this.openInternalDB().finally(() => { | ||
| this.databaseOpenPromise = null; | ||
| }); | ||
| return this.databaseOpenPromise; | ||
| } | ||
|
|
||
| /** | ||
| * Re-opens the underlying database. | ||
| * Returns a pending operation if one is already in progress. | ||
| */ | ||
| async reOpenInternalDB(): Promise<void> { | ||
| if (!this.options.reOpenOnConnectionClosed) { | ||
| throw new Error(`Cannot re-open underlying database, reOpenOnConnectionClosed is not enabled`); | ||
| } | ||
| if (this.databaseOpenPromise) { | ||
| return this.databaseOpenPromise; | ||
| } | ||
| return this._reOpen(); | ||
| } | ||
|
|
||
| protected async _init() { | ||
| await this.openInternalDB(); | ||
| this.iterateListeners((cb) => cb.initialized?.()); | ||
| } | ||
|
|
||
| getConfiguration(): ResolvedWebSQLOpenOptions { | ||
| if (!this._config) { | ||
| throw new Error(`Cannot get config before initialization is completed`); | ||
|
|
@@ -196,7 +237,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 +247,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 | ||
| } | ||
| ); | ||
| } | ||
|
|
@@ -222,7 +263,7 @@ export class LockedAsyncDatabaseAdapter | |
| this.pendingAbortControllers.add(abortController); | ||
| const { timeoutMs } = options ?? {}; | ||
|
|
||
| const timoutId = timeoutMs | ||
| const timeoutId = timeoutMs | ||
| ? setTimeout(() => { | ||
| abortController.abort(`Timeout after ${timeoutMs}ms`); | ||
| this.pendingAbortControllers.delete(abortController); | ||
|
|
@@ -234,12 +275,32 @@ export class LockedAsyncDatabaseAdapter | |
| { signal: abortController.signal }, | ||
| async () => { | ||
| this.pendingAbortControllers.delete(abortController); | ||
| if (timoutId) { | ||
| clearTimeout(timoutId); | ||
| if (timeoutId) { | ||
| clearTimeout(timeoutId); | ||
| } | ||
| const holdId = this.requiresHolds ? await this.baseDB.markHold() : null; | ||
| let holdId: string | null = null; | ||
| try { | ||
| // The database is being opened in the background. Wait for it here. | ||
| if (this.databaseOpenPromise) { | ||
| /** | ||
| * We can't await this since it uses the same lock as we're in now. | ||
| */ | ||
| throw new ConnectionClosedError('Connection is busy re-opening'); | ||
| } | ||
|
|
||
| holdId = this.requiresHolds ? await this.baseDB.markHold() : null; | ||
| return await callback(); | ||
| } catch (ex) { | ||
| if ( | ||
| ex instanceof ConnectionClosedError || | ||
| (ex instanceof Error && ex.name === 'NoModificationAllowedError') | ||
| ) { | ||
| if (this.options.reOpenOnConnectionClosed && !this.databaseOpenPromise && !this.closing) { | ||
| // Immediately re-open the database. We need to miss as little table updates as possible. | ||
| this.reOpenInternalDB(); | ||
| } | ||
| } | ||
| throw ex; | ||
| } finally { | ||
| if (holdId) { | ||
| await this.baseDB.releaseHold(holdId); | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.