Bug 1376874 - Close blocklist Sqlite connection on shutdown r=mak
MozReview-Commit-ID: LUV9b8U8jGY
--- 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]