Skip to content

Commit

Permalink
Infinite loop protection
Browse files Browse the repository at this point in the history
  • Loading branch information
dfahlander committed Nov 19, 2021
1 parent af3cbcc commit 3e1944c
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/classes/dexie/dexie.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export interface DbReadyState {
openCanceller: Promise<any> & { _stackHolder?: Error };
autoSchema: boolean;
vcFired?: boolean;
PR1398_maxLoop?: number;
}

export class Dexie implements IDexie {
Expand Down Expand Up @@ -120,7 +121,8 @@ export class Dexie implements IDexie {
dbReadyPromise: null as Promise,
cancelOpen: nop,
openCanceller: null as Promise,
autoSchema: true
autoSchema: true,
PR1398_maxLoop: 3
};
state.dbReadyPromise = new Promise(resolve => {
state.dbReadyResolve = resolve;
Expand Down
5 changes: 3 additions & 2 deletions src/classes/dexie/transaction-helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,11 @@ export function enterTransactionScope(
} else {
try {
trans.create(); // Create the native transaction so that complete() or error() will trigger even if no operation is made upon it.
db._state.PR1398_maxLoop = 3;
} catch (ex) {
if (ex.name === errnames.InvalidState && db.isOpen()) {
if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) {
console.warn('Dexie: Need to reopen db');
db.close();
db._close();
return db.open().then(() => enterTransactionScope(
db,
mode,
Expand Down
9 changes: 6 additions & 3 deletions src/functions/temp-transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,13 @@ export function tempTransaction (
return db._state.dbReadyPromise.then(() => tempTransaction(db, mode, storeNames, fn));
} else {
var trans = db._createTransaction(mode, storeNames, db._dbSchema);
try { trans.create(); } catch (ex) {
if (ex.name === errnames.InvalidState && db.isOpen()) {
try {
trans.create();
db._state.PR1398_maxLoop = 3;
} catch (ex) {
if (ex.name === errnames.InvalidState && db.isOpen() && --db._state.PR1398_maxLoop > 0) {
console.warn('Dexie: Need to reopen db');
db.close();
db._close();
return db.open().then(()=>tempTransaction(db, mode, storeNames, fn));
}
return rejection(ex);
Expand Down

0 comments on commit 3e1944c

Please sign in to comment.