Skip to content

firestore: null out IndexedDB connection when it is unexpectedly closed #8870

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

Closed
wants to merge 2 commits into from
Closed
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
6 changes: 6 additions & 0 deletions .changeset/slow-students-fry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
'@firebase/firestore': patch
'firebase': patch
---

Ensure that IndexedDB connections are discarded if they are closed unexpectedly.
29 changes: 25 additions & 4 deletions packages/firestore/src/local/simple_db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { getUA, isIndexedDBAvailable } from '@firebase/util';

import { debugAssert } from '../util/assert';
import { Code, FirestoreError } from '../util/error';
import { logDebug, logError } from '../util/log';
import { logDebug, logError, logWarn } from '../util/log';
import { Deferred } from '../util/promise';

import { PersistencePromise } from './persistence_promise';
Expand Down Expand Up @@ -359,6 +359,26 @@ export class SimpleDb {
});
};
});

this.db.addEventListener(
'close',
() => {
// Null out this.db if the IndexedDb database connection is closed
// unexpectedly, as opposed to being closed via a call to this.close()
// (see dpjg74s26h). Such an unexpected close could occur, for
// example, by a user explicitly clearing the storage for a website in
// a browser.
if (this.db) {
this.db = undefined;
logWarn(
LOG_TAG,
'Database unexpectedly closed, ' +
'possibly due to browser data being cleared for this web site'
);
}
},
{ passive: true }
);
}

if (this.versionchangelistener) {
Expand Down Expand Up @@ -453,10 +473,11 @@ export class SimpleDb {
}

close(): void {
if (this.db) {
this.db.close();
}
// Set this.db=undefined before calling this.db.close() so that the "close"
// event listener (see dpjg74s26h) won't log a spurious warning message.
const db = this.db;
this.db = undefined;
db?.close();
}
}

Expand Down
Loading