Skip to content

Commit

Permalink
Better reporting for DB corruption errors
Browse files Browse the repository at this point in the history
  • Loading branch information
indutny-signal authored Oct 21, 2021
1 parent 092c2fd commit 1b1ed2c
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"array-move": "2.1.0",
"axe-core": "4.1.4",
"backbone": "1.4.0",
"better-sqlite3": "https://github.com/signalapp/better-sqlite3#2fa02d2484e9f9a10df5ac7ea4617fb2dff30006",
"better-sqlite3": "https://github.com/signalapp/better-sqlite3#32828e03be0489572ab334239c5768c86697989f",
"bezier-easing": "2.1.0",
"blob-util": "2.0.2",
"blueimp-load-image": "5.14.0",
Expand Down
12 changes: 12 additions & 0 deletions patches/@types+better-sqlite3+7.4.0.patch
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
diff --git a/node_modules/@types/better-sqlite3/index.d.ts b/node_modules/@types/better-sqlite3/index.d.ts
index d6e4309..5948fd0 100755
--- a/node_modules/@types/better-sqlite3/index.d.ts
+++ b/node_modules/@types/better-sqlite3/index.d.ts
@@ -84,6 +84,7 @@ declare namespace BetterSqlite3 {
prototype: Database;

SqliteError: typeof SqliteError;
+ setCorruptionLogger(fn: (message: string) => void): void;
}
}

5 changes: 4 additions & 1 deletion ts/sql/Client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,7 +573,10 @@ function makeChannel(fnName: string) {
'Detected sql corruption in renderer process. ' +
`Restarting the application immediately. Error: ${error.message}`
);
ipc?.send('database-error', error.stack);
ipc?.send(
'database-error',
`${error.stack}\n${Server.getCorruptionLog()}`
);
}
log.error(
`Renderer SQL channel job (${fnName}) error ${error.message}`
Expand Down
2 changes: 2 additions & 0 deletions ts/sql/Interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -550,6 +550,8 @@ export type ServerInterface = DataInterface & {

// Server-only

getCorruptionLog: () => string;

initialize: (options: {
configDir: string;
key: string;
Expand Down
14 changes: 14 additions & 0 deletions ts/sql/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,7 @@ const dataInterface: ServerInterface = {

// Server-only

getCorruptionLog,
initialize,
initializeRenderer,

Expand Down Expand Up @@ -2695,6 +2696,19 @@ let globalInstanceRenderer: Database | undefined;
let databaseFilePath: string | undefined;
let indexedDBPath: string | undefined;

let corruptionLog = new Array<string>();

SQL.setCorruptionLogger(line => {
logger.error(`SQL corruption: ${line}`);
corruptionLog.push(line);
});

function getCorruptionLog(): string {
const result = corruptionLog.join('\n');
corruptionLog = [];
return result;
}

async function initialize({
configDir,
key,
Expand Down
9 changes: 8 additions & 1 deletion ts/sql/mainWorker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,17 @@ const port = parentPort;

// eslint-disable-next-line @typescript-eslint/no-explicit-any
function respond(seq: number, error: Error | undefined, response?: any) {
const corruptionLog = db.getCorruptionLog();

const errorMessage = [
...(error ? [error.stack] : []),
...(corruptionLog ? [corruptionLog] : []),
].join('\n');

const wrappedResponse: WrappedWorkerResponse = {
type: 'response',
seq,
error: error ? error.stack : undefined,
error: errorMessage,
response,
};
port.postMessage(wrappedResponse);
Expand Down
4 changes: 2 additions & 2 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -4794,9 +4794,9 @@ bcrypt-pbkdf@^1.0.0:
dependencies:
tweetnacl "^0.14.3"

"better-sqlite3@https://github.com/signalapp/better-sqlite3#2fa02d2484e9f9a10df5ac7ea4617fb2dff30006":
"better-sqlite3@https://github.com/signalapp/better-sqlite3#32828e03be0489572ab334239c5768c86697989f":
version "7.1.4"
resolved "https://github.com/signalapp/better-sqlite3#2fa02d2484e9f9a10df5ac7ea4617fb2dff30006"
resolved "https://github.com/signalapp/better-sqlite3#32828e03be0489572ab334239c5768c86697989f"
dependencies:
bindings "^1.5.0"
tar "^6.1.0"
Expand Down

0 comments on commit 1b1ed2c

Please sign in to comment.