@@ -1198,21 +1198,31 @@ function clearDatabaseFile(dbPath) {
11981198}
11991199
12001200/**
1201- * Shuts down any servers that might be running.
1201+ * Shuts down any servers that might be running, and closes the database.
1202+ *
1203+ * NOTE: node-sqlite3's Database.close() is always asynchronous, even when the
1204+ * wrapper is called "sync". If the close work completes after Electron has
1205+ * begun tearing down the Node environment, node-sqlite3 will try to invoke its
1206+ * JS completion callback into a dying isolate and trigger napi_fatal_error
1207+ * (SIGABRT). To avoid that, this function returns a promise that resolves
1208+ * only after the database close callback has actually fired. Callers in
1209+ * Electron's 'will-quit' must preventDefault(), await this, and then app.exit().
1210+ *
1211+ * @returns Promise that resolves when shutdown is complete.
12021212 */
1203- function shutdown ( ) {
1213+ async function shutdown ( ) {
12041214 env . logInfo ( 'Shutting down HTTP and IPC servers...' )
12051215 ipcServer . shutdownServerSync ( )
12061216 httpServer . shutdownHttpServerSync ( )
12071217
12081218 if ( mainDatabase != null ) {
1209- // Use a sync call, because you can't have promises in the 'quit' event.
1219+ let dbToClose = mainDatabase
1220+ mainDatabase = null
12101221 try {
1211- dbApi . closeDatabaseSync ( mainDatabase )
1212- mainDatabase = null
1222+ await dbApi . closeDatabase ( dbToClose )
12131223 env . logInfo ( 'Database closed, shutting down.' )
12141224 } catch ( err ) {
1215- env . logError ( ' Failed to close database.' )
1225+ env . logError ( ` Failed to close database: ${ err } ` )
12161226 }
12171227 }
12181228}
0 commit comments