Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
FIX schema migration would leave the leader-election `BroadcastChannel` open when the migration threw (for example when a `migrationStrategy` failed), keeping the tab leader forever and blocking other tabs from running the migration. The channel is now closed on every code path. (https://github.com/pubkey/rxdb/pull/7827)
31 changes: 25 additions & 6 deletions src/plugins/migration-schema/rx-migration-state.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,32 @@ export class RxMigrationState {
}
this.started = true;

/**
* Ensure the migration is cleaned up when the collection or the
* database is closed. Registering this here (instead of inside
* migrateStorage()) guarantees the broadcastChannel / leader
* election is released even if the migration throws before the
* replication is set up.
*/
this.collection.onClose.push(() => this.cancel());
this.database.onClose.push(() => this.cancel());

try {
await this.runMigration(batchSize);
} finally {
/**
* Always close the broadcastChannel so that the tab does not
* stay leader forever if the migration throws on any code path.
* @link https://github.com/pubkey/rxdb/pull/7827
*/
if (this.broadcastChannel) {
await this.broadcastChannel.close();
this.broadcastChannel = undefined;
}
}
}

private async runMigration(batchSize: number): Promise<void> {
/**
* To ensure that multiple tabs do not migrate the same collection,
* we use a new broadcastChannel/leaderElector for each collection.
Expand Down Expand Up @@ -303,9 +328,6 @@ export class RxMigrationState {
s.status = 'DONE';
return s;
});
if (this.broadcastChannel) {
await this.broadcastChannel.close();
}
}

public updateStatusHandlers: MigrationStatusUpdate[] = [];
Expand Down Expand Up @@ -391,9 +413,6 @@ export class RxMigrationState {
newStorage: RxStorageInstance<any, any, any>,
batchSize: number
) {

this.collection.onClose.push(() => this.cancel());
this.database.onClose.push(() => this.cancel());
const replicationMetaStorageInstance = await this.database.storage.createStorageInstance({
databaseName: this.database.name,
collectionName: 'rx-migration-state-meta-' + oldStorage.collectionName + '-' + oldStorage.schema.version,
Expand Down
96 changes: 96 additions & 0 deletions test/unit/migration-schema.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1523,6 +1523,102 @@ describe('migration-schema.test.ts', function () {
await db2.close();
});

/**
* The broadcastChannel that is used for the per-collection
* leader-election of the migration must be closed on every code path.
* Previously, when the migration threw (for example because a
* migrationStrategy failed), the broadcastChannel stayed open and the
* tab remained leader forever, blocking other tabs from ever running
* the migration.
* @link https://github.com/pubkey/rxdb/pull/7827
*/
it('#7827 should close the broadcastChannel/leader-election when the migration errors', async () => {
if (!config.storage.hasMultiInstance) {
return;
}
const dbName = randomToken(10);

const schema0 = {
version: 0,
primaryKey: 'id',
type: 'object',
properties: {
id: { type: 'string', maxLength: 100 },
name: { type: 'string' }
},
required: ['id', 'name']
};
const schema1 = {
version: 1,
primaryKey: 'id',
type: 'object',
properties: {
id: { type: 'string', maxLength: 100 },
name: { type: 'string' },
migrated: { type: 'boolean' }
},
required: ['id', 'name', 'migrated']
};

const db = await createRxDatabase({
name: dbName,
storage: config.storage.getStorage(),
multiInstance: true,
ignoreDuplicate: true
});
await db.addCollections({
items: { schema: schema0 }
});
await db.items.bulkInsert([
{ id: 'doc1', name: 'Document 1' },
{ id: 'doc2', name: 'Document 2' }
]);
await db.close();

const db2 = await createRxDatabase({
name: dbName,
storage: config.storage.getStorage(),
multiInstance: true,
ignoreDuplicate: true
});
await db2.addCollections({
items: {
schema: schema1,
autoMigrate: false,
migrationStrategies: {
1: () => {
// force the migration to fail
throw new Error('migrationStrategy failed on purpose');
}
}
}
});

const migrationState = db2.items.getMigrationState();

// The migration must reject because the migrationStrategy throws.
// Attach the catch synchronously so the rejection is never treated
// as an unhandledRejection by the test harness.
let failed = false;
await migrationState.migratePromise().catch(() => {
failed = true;
});
assert.strictEqual(failed, true, 'the migration must have failed');

// Even though the migration errored, the broadcastChannel (and thus
// the leader-election) must have been closed and released. The fix
// only sets broadcastChannel back to undefined after close() ran, so
// observing undefined proves the channel was closed.
// A bounded timeout is used so that a regression fails fast with a
// clear error instead of polling forever.
await waitUntil(
() => typeof migrationState.broadcastChannel === 'undefined',
5 * 1000
);

await db2.close();
});


it('#7008 migrate schema with multiple connected storages', async () => {
// create a schema
Expand Down
Loading