From d7299a7969a327e71f0f8b509489167777c382a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adomas=20Ven=C4=8Dkauskas?= Date: Thu, 26 Oct 2023 12:55:10 +0300 Subject: [PATCH] Fixes race conditions when translator eval fails during detection Addresses zotero/zotero#3182 --- src/translation/translate.js | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/src/translation/translate.js b/src/translation/translate.js index 67ed34b..c0fdc02 100644 --- a/src/translation/translate.js +++ b/src/translation/translate.js @@ -1788,22 +1788,22 @@ Zotero.Translate.Base.prototype = { var parse = function(code) { Zotero.debug("Translate: Parsing code for " + translator.label + " " + "(" + translator.translatorID + ", " + translator.lastUpdated + ")", 4); - try { - this._sandboxManager.eval( - "var exports = {}, ZOTERO_TRANSLATOR_INFO = " + code, - [ - "detect" + this._entryFunctionSuffix, - "do" + this._entryFunctionSuffix, - "exports", - "ZOTERO_TRANSLATOR_INFO" - ], - (translator.file ? translator.file.path : translator.label) - ); - } - catch (e) { - this.complete(false, e); - return; - } + // This might throw but it's better than catching and calling complete(false) here + // since then other execution paths are not aware of this failing and continue running + // at least in Import translation detection, causing weird race conditions. + // The code that calls this function (which is really only code in this file) + // should expect a throw and call complete(false) after catching. + this._sandboxManager.eval( + "var exports = {}, ZOTERO_TRANSLATOR_INFO = " + code, + [ + "detect" + this._entryFunctionSuffix, + "do" + this._entryFunctionSuffix, + "exports", + "ZOTERO_TRANSLATOR_INFO" + ], + (translator.file ? translator.file.path : translator.label) + ); + this._translatorInfo = this._sandboxManager.sandbox.ZOTERO_TRANSLATOR_INFO; }.bind(this);