From cc9efde8433b7ec94d4e02878f451b763dab30c6 Mon Sep 17 00:00:00 2001 From: Dan Stillman Date: Tue, 16 Jan 2018 11:04:41 -0500 Subject: [PATCH] Fix translator architecture hangs on bad JSON in translatorCache --- chrome/content/zotero/xpcom/schema.js | 31 +++++++++++++------ .../zotero/xpcom/translation/translators.js | 19 ++++++++++-- 2 files changed, 38 insertions(+), 12 deletions(-) diff --git a/chrome/content/zotero/xpcom/schema.js b/chrome/content/zotero/xpcom/schema.js index aab25fe53..e9703e7b2 100644 --- a/chrome/content/zotero/xpcom/schema.js +++ b/chrome/content/zotero/xpcom/schema.js @@ -744,16 +744,29 @@ Zotero.Schema = new function(){ index[id].extract = true; } - let sql = "SELECT metadataJSON FROM translatorCache"; - let dbCache = yield Zotero.DB.columnQueryAsync(sql); + let sql = "SELECT fileName, metadataJSON FROM translatorCache"; + let rows = yield Zotero.DB.queryAsync(sql); // If there's anything in the cache, see what we actually need to extract - if (dbCache) { - for (let i = 0; i < dbCache.length; i++) { - let metadata = JSON.parse(dbCache[i]); - let id = metadata.translatorID; - if (index[id] && index[id].lastUpdated <= metadata.lastUpdated) { - index[id].extract = false; - } + for (let i = 0; i < rows.length; i++) { + let json = rows[i].metadataJSON; + let metadata; + try { + metadata = JSON.parse(json); + } + catch (e) { + Zotero.logError(e); + Zotero.debug(json, 1); + + // // If JSON is invalid, clear from cache + yield Zotero.DB.queryAsync( + "DELETE FROM translatorCache WHERE fileName=?", + rows[i].fileName + ); + continue; + } + let id = metadata.translatorID; + if (index[id] && index[id].lastUpdated <= metadata.lastUpdated) { + index[id].extract = false; } } diff --git a/chrome/content/zotero/xpcom/translation/translators.js b/chrome/content/zotero/xpcom/translation/translators.js index 98e9a1ec7..2cdfcdc67 100644 --- a/chrome/content/zotero/xpcom/translation/translators.js +++ b/chrome/content/zotero/xpcom/translation/translators.js @@ -116,9 +116,22 @@ Zotero.Translators = new function() { // Get JSON from cache if possible if (memCacheJSON || dbCacheEntry) { - var translator = Zotero.Translators.load( - memCacheJSON || dbCacheEntry.metadataJSON, path - ); + try { + var translator = Zotero.Translators.load( + memCacheJSON || dbCacheEntry.metadataJSON, path + ); + } + catch (e) { + Zotero.logError(e); + Zotero.debug(memCacheJSON || dbCacheEntry.metadataJSON, 1); + + // If JSON is invalid, clear from cache + yield Zotero.DB.queryAsync( + "DELETE FROM translatorCache WHERE fileName=?", + fileName + ); + continue; + } } // Otherwise, load from file else {