diff --git a/chrome/content/zotero/xpcom/db.js b/chrome/content/zotero/xpcom/db.js index e67a24dcec..16cc748778 100644 --- a/chrome/content/zotero/xpcom/db.js +++ b/chrome/content/zotero/xpcom/db.js @@ -794,8 +794,11 @@ Zotero.DBConnection.prototype.checkException = function (e) { Zotero.DBConnection.prototype.closeDatabase = function () { if(this._connection) { this.stopDummyStatement(); - this._connection.asyncClose(); - return true; + var deferred = Q.defer(); + this._connection.asyncClose(deferred.resolve); + return deferred.promise; + } else { + return Q(); } } diff --git a/chrome/content/zotero/xpcom/zotero.js b/chrome/content/zotero/xpcom/zotero.js index 2d90a85699..7dbe63ba8d 100644 --- a/chrome/content/zotero/xpcom/zotero.js +++ b/chrome/content/zotero/xpcom/zotero.js @@ -423,7 +423,7 @@ Components.utils.import("resource://gre/modules/Services.jsm"); } // Register shutdown handler to call Zotero.shutdown() - var _shutdownObserver = {observe:Zotero.shutdown}; + var _shutdownObserver = {observe:function() { Zotero.shutdown() }}; Services.obs.addObserver(_shutdownObserver, "quit-application", false); try { @@ -783,7 +783,7 @@ Components.utils.import("resource://gre/modules/Services.jsm"); } - this.shutdown = function (subject, topic, data) { + this.shutdown = function(callback) { Zotero.debug("Shutting down Zotero"); try { @@ -811,10 +811,13 @@ Components.utils.import("resource://gre/modules/Services.jsm"); Components.utils.forceGC(); // unlock DB - Zotero.DB.closeDatabase(); - - // broadcast that DB lock has been released - Zotero.IPC.broadcast("lockReleased"); + Zotero.DB.closeDatabase().then(function() { + // broadcast that DB lock has been released + Zotero.IPC.broadcast("lockReleased"); + callback(); + }); + } else { + callback(); } } catch(e) { Zotero.debug(e); diff --git a/components/zotero-service.js b/components/zotero-service.js index e00edaa59b..2804361c60 100644 --- a/components/zotero-service.js +++ b/components/zotero-service.js @@ -164,11 +164,16 @@ ZoteroContext.prototype = { */ "switchConnectorMode":function(isConnector) { if(isConnector !== this.isConnector) { - zContext.Zotero.shutdown(); - - // create a new zContext - makeZoteroContext(isConnector); - zContext.Zotero.init(); + zContext.Zotero.shutdown(function() { + try { + // create a new zContext + makeZoteroContext(isConnector); + zContext.Zotero.init(); + } catch(e) { + dump(e.toSource()); + throw e; + } + }); } return zContext; @@ -294,9 +299,16 @@ function ZoteroService() { } catch(e) { if(e === "ZOTERO_SHOULD_START_AS_CONNECTOR") { // if Zotero should start as a connector, reload it - zContext.Zotero.shutdown(); - makeZoteroContext(true); - zContext.Zotero.init(); + zContext.Zotero.shutdown(function() { + try { + makeZoteroContext(true); + zContext.Zotero.init(); + } catch(e) { + dump(e.toSource()); + Components.utils.reportError(e); + throw e; + } + }); } else { dump(e.toSource()); Components.utils.reportError(e);