-
Notifications
You must be signed in to change notification settings - Fork 94
fix the database connection is closing issue. #748
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
Merged
+297
−11
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
2946a6f
fix the database connection is closing issue.
Krishna2323 8aa7ec6
fix tests.
Krishna2323 d5bb9b6
Merge branch 'Expensify:main' into krishna2323/issue/84192
Krishna2323 e824ef7
Merge branch 'Expensify:main' into krishna2323/issue/84192
Krishna2323 f351319
address review comments.
Krishna2323 f02de7b
remove closedBy diagnostic tracking per review feedback
Krishna2323 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -1,26 +1,43 @@ | ||||||||||||||||||||||
| import * as IDB from 'idb-keyval'; | ||||||||||||||||||||||
| import type {UseStore} from 'idb-keyval'; | ||||||||||||||||||||||
| import {logInfo} from '../../../Logger'; | ||||||||||||||||||||||
| import * as Logger from '../../../Logger'; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // This is a copy of the createStore function from idb-keyval, we need a custom implementation | ||||||||||||||||||||||
| // because we need to create the database manually in order to ensure that the store exists before we use it. | ||||||||||||||||||||||
| // If the store does not exist, idb-keyval will throw an error | ||||||||||||||||||||||
| // source: https://github.com/jakearchibald/idb-keyval/blob/9d19315b4a83897df1e0193dccdc29f78466a0f3/src/index.ts#L12 | ||||||||||||||||||||||
| function createStore(dbName: string, storeName: string): UseStore { | ||||||||||||||||||||||
| let dbp: Promise<IDBDatabase> | undefined; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const attachHandlers = (db: IDBDatabase) => { | ||||||||||||||||||||||
| // Browsers may close idle IDB connections at any time, especially Safari. | ||||||||||||||||||||||
| // We clear the cached promise so the next operation opens a fresh connection. | ||||||||||||||||||||||
| // https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/close_event | ||||||||||||||||||||||
| // eslint-disable-next-line no-param-reassign | ||||||||||||||||||||||
| db.onclose = () => { | ||||||||||||||||||||||
| Logger.logInfo('IDB connection closed by browser', {dbName, storeName}); | ||||||||||||||||||||||
| dbp = undefined; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // When another tab triggers a DB version upgrade, we must close the connection | ||||||||||||||||||||||
| // to unblock the upgrade; otherwise the other tab's open request hangs indefinitely. | ||||||||||||||||||||||
| // https://developer.mozilla.org/en-US/docs/Web/API/IDBDatabase/versionchange_event | ||||||||||||||||||||||
| // eslint-disable-next-line no-param-reassign | ||||||||||||||||||||||
| db.onversionchange = () => { | ||||||||||||||||||||||
| Logger.logInfo('IDB connection closing due to version change', {dbName, storeName}); | ||||||||||||||||||||||
| db.close(); | ||||||||||||||||||||||
| dbp = undefined; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| const getDB = () => { | ||||||||||||||||||||||
| if (dbp) return dbp; | ||||||||||||||||||||||
| const request = indexedDB.open(dbName); | ||||||||||||||||||||||
| request.onupgradeneeded = () => request.result.createObjectStore(storeName); | ||||||||||||||||||||||
| dbp = IDB.promisifyRequest(request); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| dbp.then( | ||||||||||||||||||||||
| (db) => { | ||||||||||||||||||||||
| // It seems like Safari sometimes likes to just close the connection. | ||||||||||||||||||||||
| // It's supposed to fire this event when that happens. Let's hope it does! | ||||||||||||||||||||||
| // eslint-disable-next-line no-param-reassign | ||||||||||||||||||||||
| db.onclose = () => (dbp = undefined); | ||||||||||||||||||||||
| }, | ||||||||||||||||||||||
| attachHandlers, | ||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-empty-function | ||||||||||||||||||||||
| () => {}, | ||||||||||||||||||||||
| ); | ||||||||||||||||||||||
|
|
@@ -34,7 +51,7 @@ function createStore(dbName: string, storeName: string): UseStore { | |||||||||||||||||||||
| return db; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| logInfo(`Store ${storeName} does not exist in database ${dbName}.`); | ||||||||||||||||||||||
| Logger.logInfo(`Store ${storeName} does not exist in database ${dbName}.`); | ||||||||||||||||||||||
| const nextVersion = db.version + 1; | ||||||||||||||||||||||
| db.close(); | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
|
|
@@ -45,18 +62,39 @@ function createStore(dbName: string, storeName: string): UseStore { | |||||||||||||||||||||
| return; | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| logInfo(`Creating store ${storeName} in database ${dbName}.`); | ||||||||||||||||||||||
| Logger.logInfo(`Creating store ${storeName} in database ${dbName}.`); | ||||||||||||||||||||||
| updatedDatabase.createObjectStore(storeName); | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| dbp = IDB.promisifyRequest(request); | ||||||||||||||||||||||
| // eslint-disable-next-line @typescript-eslint/no-empty-function | ||||||||||||||||||||||
| dbp.then(attachHandlers, () => {}); | ||||||||||||||||||||||
| return dbp; | ||||||||||||||||||||||
| }; | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| return (txMode, callback) => | ||||||||||||||||||||||
| getDB() | ||||||||||||||||||||||
| function executeTransaction<T>(txMode: IDBTransactionMode, callback: (store: IDBObjectStore) => T | PromiseLike<T>): Promise<T> { | ||||||||||||||||||||||
| return getDB() | ||||||||||||||||||||||
| .then(verifyStoreExists) | ||||||||||||||||||||||
| .then((db) => callback(db.transaction(storeName, txMode).objectStore(storeName))); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| // If the connection was closed between getDB() resolving and db.transaction() executing, | ||||||||||||||||||||||
| // the transaction throws InvalidStateError. We catch it and retry once with a fresh connection. | ||||||||||||||||||||||
| return (txMode, callback) => | ||||||||||||||||||||||
| executeTransaction(txMode, callback).catch((error) => { | ||||||||||||||||||||||
| if (error instanceof DOMException && error.name === 'InvalidStateError') { | ||||||||||||||||||||||
|
Comment on lines
+81
to
+85
Contributor
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.
Suggested change
|
||||||||||||||||||||||
| Logger.logAlert('IDB InvalidStateError, retrying with fresh connection', { | ||||||||||||||||||||||
| dbName, | ||||||||||||||||||||||
| storeName, | ||||||||||||||||||||||
| txMode, | ||||||||||||||||||||||
| errorMessage: error.message, | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| dbp = undefined; | ||||||||||||||||||||||
| // Retry only once — this call is not wrapped, so if it also fails the error propagates normally. | ||||||||||||||||||||||
| return executeTransaction(txMode, callback); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
| throw error; | ||||||||||||||||||||||
| }); | ||||||||||||||||||||||
| } | ||||||||||||||||||||||
|
|
||||||||||||||||||||||
| export default createStore; | ||||||||||||||||||||||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| import * as IDB from 'idb-keyval'; | ||
| import createStore from '../../../../lib/storage/providers/IDBKeyValProvider/createStore'; | ||
| import * as Logger from '../../../../lib/Logger'; | ||
|
|
||
| const STORE_NAME = 'teststore'; | ||
| let testDbCounter = 0; | ||
|
|
||
| function uniqueDBName() { | ||
| testDbCounter += 1; | ||
| return `TestCreateStoreDB_${testDbCounter}`; | ||
| } | ||
|
|
||
| /** | ||
| * Captures the internal IDBDatabase instance used by a store by intercepting | ||
| * the first db.transaction() call. | ||
| */ | ||
| async function captureDB(store: ReturnType<typeof createStore>): Promise<IDBDatabase | undefined> { | ||
| const captured: {db?: IDBDatabase} = {}; | ||
| const original = IDBDatabase.prototype.transaction; | ||
| const spy = jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(function (this: IDBDatabase, ...args) { | ||
| captured.db = this; | ||
| spy.mockRestore(); | ||
| return original.apply(this, args); | ||
| }); | ||
| await store('readonly', (s) => IDB.promisifyRequest(s.getAllKeys())); | ||
| return captured.db; | ||
| } | ||
|
|
||
| describe('createStore', () => { | ||
| let logAlertSpy: jest.SpyInstance; | ||
| let logInfoSpy: jest.SpyInstance; | ||
|
|
||
| beforeEach(() => { | ||
| logAlertSpy = jest.spyOn(Logger, 'logAlert'); | ||
| logInfoSpy = jest.spyOn(Logger, 'logInfo'); | ||
| }); | ||
|
|
||
| afterEach(() => { | ||
| jest.restoreAllMocks(); | ||
| }); | ||
|
|
||
| describe('InvalidStateError retry', () => { | ||
| it('should retry once and succeed when db.transaction throws InvalidStateError', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('initial', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| const original = IDBDatabase.prototype.transaction; | ||
| let callCount = 0; | ||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(function (this: IDBDatabase, ...args) { | ||
| callCount += 1; | ||
| if (callCount === 1) { | ||
| throw new DOMException('The database connection is closing.', 'InvalidStateError'); | ||
| } | ||
| return original.apply(this, args); | ||
| }); | ||
|
|
||
| const result = await store('readonly', (s) => IDB.promisifyRequest(s.get('key1'))); | ||
|
|
||
| expect(result).toBe('initial'); | ||
| expect(callCount).toBe(2); | ||
| }); | ||
|
|
||
| it('should propagate InvalidStateError if retry also fails', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(() => { | ||
| throw new DOMException('The database connection is closing.', 'InvalidStateError'); | ||
| }); | ||
|
|
||
| await expect(store('readonly', (s) => IDB.promisifyRequest(s.get('key1')))).rejects.toThrow(DOMException); | ||
| expect(logAlertSpy).toHaveBeenCalledTimes(1); | ||
| }); | ||
|
|
||
| it('should not retry on non-InvalidStateError DOMException', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| let callCount = 0; | ||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(() => { | ||
| callCount += 1; | ||
| throw new DOMException('Not found', 'NotFoundError'); | ||
| }); | ||
|
|
||
| await expect(store('readonly', (s) => IDB.promisifyRequest(s.get('key1')))).rejects.toThrow(DOMException); | ||
| expect(callCount).toBe(1); | ||
| expect(logAlertSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should not retry on non-DOMException errors', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| let callCount = 0; | ||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(() => { | ||
| callCount += 1; | ||
| throw new TypeError('Something went wrong'); | ||
| }); | ||
|
|
||
| await expect(store('readonly', (s) => IDB.promisifyRequest(s.get('key1')))).rejects.toThrow(TypeError); | ||
| expect(callCount).toBe(1); | ||
| expect(logAlertSpy).not.toHaveBeenCalled(); | ||
| }); | ||
|
|
||
| it('should preserve data integrity after a successful retry', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('existing', 'key0'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| const original = IDBDatabase.prototype.transaction; | ||
| let callCount = 0; | ||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(function (this: IDBDatabase, ...args) { | ||
| callCount += 1; | ||
| if (callCount === 1) { | ||
| throw new DOMException('The database connection is closing.', 'InvalidStateError'); | ||
| } | ||
| return original.apply(this, args); | ||
| }); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('retried_value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| jest.restoreAllMocks(); | ||
| logAlertSpy = jest.spyOn(Logger, 'logAlert'); | ||
|
|
||
| const result = await store('readonly', (s) => IDB.promisifyRequest(s.get('key1'))); | ||
| expect(result).toBe('retried_value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('diagnostic logging', () => { | ||
| it('should log alert with all diagnostic fields on retry', async () => { | ||
| const dbName = uniqueDBName(); | ||
| const store = createStore(dbName, STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| const original = IDBDatabase.prototype.transaction; | ||
| let callCount = 0; | ||
| jest.spyOn(IDBDatabase.prototype, 'transaction').mockImplementation(function (this: IDBDatabase, ...args) { | ||
| callCount += 1; | ||
| if (callCount === 1) { | ||
| throw new DOMException('The database connection is closing.', 'InvalidStateError'); | ||
| } | ||
| return original.apply(this, args); | ||
| }); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value2', 'key2'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| expect(logAlertSpy).toHaveBeenCalledWith('IDB InvalidStateError, retrying with fresh connection', { | ||
| dbName, | ||
| storeName: STORE_NAME, | ||
| txMode: 'readwrite', | ||
| errorMessage: 'The database connection is closing.', | ||
| }); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onclose handler', () => { | ||
| it('should log info when browser closes the connection', async () => { | ||
| const dbName = uniqueDBName(); | ||
| const store = createStore(dbName, STORE_NAME); | ||
|
|
||
| const db = await captureDB(store); | ||
| expect(db).toBeDefined(); | ||
| db?.onclose?.call(db, new Event('close')); | ||
|
|
||
| expect(logInfoSpy).toHaveBeenCalledWith('IDB connection closed by browser', {dbName, storeName: STORE_NAME}); | ||
| }); | ||
|
|
||
| it('should recover with a fresh connection after browser close', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| const db = await captureDB(store); | ||
| expect(db).toBeDefined(); | ||
| db?.onclose?.call(db, new Event('close')); | ||
|
|
||
| const result = await store('readonly', (s) => IDB.promisifyRequest(s.get('key1'))); | ||
| expect(result).toBe('value'); | ||
| }); | ||
| }); | ||
|
|
||
| describe('onversionchange handler', () => { | ||
| it('should close connection and log when versionchange fires', async () => { | ||
| const dbName = uniqueDBName(); | ||
| const store = createStore(dbName, STORE_NAME); | ||
|
|
||
| const db = await captureDB(store); | ||
| expect(db).toBeDefined(); | ||
| const closeSpy = jest.spyOn(db!, 'close'); | ||
|
|
||
| // @ts-expect-error -- our handler ignores the event argument | ||
| db?.onversionchange?.call(db, new Event('versionchange')); | ||
|
|
||
| expect(closeSpy).toHaveBeenCalled(); | ||
| expect(logInfoSpy).toHaveBeenCalledWith('IDB connection closing due to version change', {dbName, storeName: STORE_NAME}); | ||
| }); | ||
|
|
||
| it('should recover with a fresh connection after versionchange', async () => { | ||
| const store = createStore(uniqueDBName(), STORE_NAME); | ||
|
|
||
| await store('readwrite', (s) => { | ||
| s.put('value', 'key1'); | ||
| return IDB.promisifyRequest(s.transaction); | ||
| }); | ||
|
|
||
| const db = await captureDB(store); | ||
| expect(db).toBeDefined(); | ||
| // @ts-expect-error -- our handler ignores the event argument | ||
| db?.onversionchange?.call(db, new Event('versionchange')); | ||
|
|
||
| const result = await store('readonly', (s) => IDB.promisifyRequest(s.get('key1'))); | ||
| expect(result).toBe('value'); | ||
| }); | ||
| }); | ||
| }); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should have a comment explaining why we need this check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done. Please check.