Skip to content

Commit 943b000

Browse files
committed
src/local/simple_db.ts: null out this.db if it closes unexpectedly, so that it does not get used after being closed, which is guaranteed to fail and can cause issues.
1 parent dcc62c0 commit 943b000

File tree

1 file changed

+27
-4
lines changed

1 file changed

+27
-4
lines changed

packages/firestore/src/local/simple_db.ts

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { getUA, isIndexedDBAvailable } from '@firebase/util';
1919

2020
import { debugAssert } from '../util/assert';
2121
import { Code, FirestoreError } from '../util/error';
22-
import { logDebug, logError } from '../util/log';
22+
import { logDebug, logError, logWarn } from '../util/log';
2323
import { Deferred } from '../util/promise';
2424

2525
import { PersistencePromise } from './persistence_promise';
@@ -359,6 +359,28 @@ export class SimpleDb {
359359
});
360360
};
361361
});
362+
363+
this.db.addEventListener(
364+
'close',
365+
() => {
366+
// Null out this.db if the IndexedDb database connection is closed
367+
// unexpectedly, as opposed to being closed via a call to this.close()
368+
// (see dpjg74s26h). Such an unexpected close could occur, for
369+
// example, by a user explicitly clearing the storage for a website in
370+
// a browser. This also avoids using a closed IndexedDb connection,
371+
// which can cause issues
372+
// (e.g. https://github.com/firebase/firebase-js-sdk/issues/8593)
373+
if (this.db) {
374+
this.db = undefined;
375+
logWarn(
376+
LOG_TAG,
377+
'Database unexpectedly closed, ' +
378+
'possibly due to browser data being cleared for this web site'
379+
);
380+
}
381+
},
382+
{ passive: true }
383+
);
362384
}
363385

364386
if (this.versionchangelistener) {
@@ -453,10 +475,11 @@ export class SimpleDb {
453475
}
454476

455477
close(): void {
456-
if (this.db) {
457-
this.db.close();
458-
}
478+
// Set this.db=undefined before calling this.db.close() so that the "close"
479+
// event listener (see dpjg74s26h) won't log a spurious warning message.
480+
const db = this.db;
459481
this.db = undefined;
482+
db?.close();
460483
}
461484
}
462485

0 commit comments

Comments
 (0)