Skip to content

Commit 953e4de

Browse files
authored
feat: 🎸 Handle corrupted sqlite DB (#2976)
* feat: 🎸 Handle corrupted sqlite DB
1 parent d76d17e commit 953e4de

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

‎addons/api/addon/handlers/sqlite-handler.js‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,8 @@ export default class SqliteHandler {
182182
// If we get an invalid list token, we'll delete the token and
183183
// clear the DB and try again without a token.
184184
if (
185-
err?.errors[0].status === 400 &&
186-
err?.errors[0].code === 'invalid list token'
185+
err?.errors?.[0].status === 400 &&
186+
err?.errors?.[0].code === 'invalid list token'
187187
) {
188188
await this.sqlite.deleteResource(type);
189189
// Delete all tokens of the same resource type so we don't keep clearing the DB.

‎addons/api/addon/workers/sqlite-worker.js‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ const MAX_HOST_PARAMETERS = 32766;
2727
const SCHEMA_VERSION = 1;
2828
const isSecure = self.isSecureContext;
2929

30+
// Error code for sqlite corruption errors.
31+
// See https://sqlite.org/rescode.html#corrupt
32+
const SQLITE_CORRUPT = 11;
33+
3034
const methods = {
3135
initializeSQLite: async () => {
3236
sqlite3 = await sqlite3InitModule({
@@ -65,7 +69,7 @@ const methods = {
6569

6670
setupDb();
6771
done = true;
68-
} catch (err) {
72+
} catch (/** @type {import('@sqlite.org/sqlite-wasm').SQLite3Error} */ err) {
6973
// If we get a SQLite3Error and the pool is at capacity, we'll assume the error is
7074
// because we hit the cap so we add more capacity and see if that solves it.
7175
if (
@@ -81,7 +85,13 @@ const methods = {
8185
}
8286
} else {
8387
console.error('SQLite initialization error:', err);
84-
throw err;
88+
89+
// We might have gotten ourselves into a bad state which corrupted the DB so clear it out and retry
90+
if (err?.resultCode === SQLITE_CORRUPT) {
91+
poolUtil.unlink(`/${dbName}`);
92+
} else {
93+
throw err;
94+
}
8595
}
8696
}
8797
}

0 commit comments

Comments
 (0)