Bug 1376874 - Close blocklist Sqlite connection on shutdown r=mak draft
authorMathieu Leplatre <mathieu@mozilla.com>
Tue, 04 Jul 2017 12:36:08 +0200
changeset 604063 607f8054ad00387f17d124c87c0b0b12ce3ee2e2
parent 603705 fef489e8c2a193dde885adc48deb74cc883a5881
child 636073 7e1e0a25e50cfffdada9087211b18d267c4a91a5
push id66941
push usermleplatre@mozilla.com
push dateWed, 05 Jul 2017 07:35:42 +0000
reviewersmak
bugs1376874
milestone56.0a1
Bug 1376874 - Close blocklist Sqlite connection on shutdown r=mak MozReview-Commit-ID: LUV9b8U8jGY
services/common/kinto-storage-adapter.js
services/common/tests/unit/test_storage_adapter.js
services/common/tests/unit/test_storage_adapter_shutdown.js
services/common/tests/unit/xpcshell.ini
--- a/services/common/kinto-storage-adapter.js
+++ b/services/common/kinto-storage-adapter.js
@@ -240,17 +240,26 @@ class FirefoxAdapter extends Kinto.adapt
    *
    * Options:
    *   - path: The path for the Sqlite database
    *
    * @returns SqliteConnection
    */
   static async openConnection(options) {
     const opts = Object.assign({}, { sharedMemoryCache: false }, options);
-    return await Sqlite.openConnection(opts).then(this._init);
+    const conn = await Sqlite.openConnection(opts).then(this._init);
+    try {
+      Sqlite.shutdown.addBlocker("Kinto storage adapter connection closing",
+                                 async () => await conn.close());
+    } catch (e) {
+      // It's too late to block shutdown, just close the connection.
+      await conn.close();
+      throw e;
+    }
+    return conn;
   }
 
   clear() {
     const params = { collection_name: this.collection };
     return this._executeStatement(statements.clearData, params);
   }
 
   execute(callback, options = { preload: [] }) {
--- a/services/common/tests/unit/test_storage_adapter.js
+++ b/services/common/tests/unit/test_storage_adapter.js
@@ -253,12 +253,8 @@ add_test(function test_creation_from_emp
     run_next_test();
   });
 
   test_collection_operations();
 
   cleanup_kinto();
   run_next_test();
 });
-
-function run_test() {
-  run_next_test();
-}
new file mode 100644
--- /dev/null
+++ b/services/common/tests/unit/test_storage_adapter_shutdown.js
@@ -0,0 +1,24 @@
+/* Any copyright is dedicated to the Public Domain.
+   http://creativecommons.org/publicdomain/zero/1.0/ */
+
+Cu.import("resource://gre/modules/Services.jsm");
+Cu.import("resource://gre/modules/AsyncShutdown.jsm");
+
+Cu.import("resource://services-common/kinto-storage-adapter.js");
+
+
+add_task(async function test_sqlite_shutdown() {
+  const sqliteHandle = await FirefoxAdapter.openConnection({path: "kinto.sqlite"});
+
+  // Shutdown Sqlite.jsm synchronously.
+  Services.prefs.setBoolPref("toolkit.asyncshutdown.testing", true);
+  AsyncShutdown.profileBeforeChange._trigger();
+  Services.prefs.clearUserPref("toolkit.asyncshutdown.testing");
+
+  try {
+    sqliteHandle.execute("SELECT 1;");
+    equal("Should not succeed, connection should be closed.", false);
+  } catch (e) {
+    equal(e.message, "Connection is not open.");
+  }
+});
--- a/services/common/tests/unit/xpcshell.ini
+++ b/services/common/tests/unit/xpcshell.ini
@@ -20,16 +20,18 @@ tags = blocklist
 tags = blocklist
 
 [test_kinto.js]
 tags = blocklist
 [test_blocklist_signatures.js]
 tags = blocklist
 [test_storage_adapter.js]
 tags = blocklist
+[test_storage_adapter_shutdown.js]
+tags = blocklist
 
 [test_utils_atob.js]
 [test_utils_convert_string.js]
 [test_utils_dateprefs.js]
 [test_utils_deepCopy.js]
 [test_utils_encodeBase32.js]
 [test_utils_encodeBase64URL.js]
 [test_utils_ensureMillisecondsTimestamp.js]