From c572ad2a3e70ccdc42625c8aacca72a94f4001b7 Mon Sep 17 00:00:00 2001 From: Jelmer van der Linde Date: Thu, 14 Jul 2022 11:04:10 +0200 Subject: [PATCH] Catch more error cases in the wasm translation worker --- extension/controller/backgroundScript.js | 8 ++- .../translation/WASMTranslationHelper.js | 14 ++++-- .../translation/WASMTranslationWorker.js | 49 +++++++++++-------- 3 files changed, 45 insertions(+), 26 deletions(-) diff --git a/extension/controller/backgroundScript.js b/extension/controller/backgroundScript.js index 1fa07983..c763d460 100644 --- a/extension/controller/backgroundScript.js +++ b/extension/controller/backgroundScript.js @@ -209,7 +209,8 @@ class Tab extends EventTarget { // language. We leave to selected as is pendingTranslationRequests: 0, totalTranslationRequests: 0, - state: State.PAGE_LOADING + state: State.PAGE_LOADING, + error: null }; } }); @@ -382,6 +383,11 @@ let provider = new class { this.#provider.onerror = err => { console.error('Translation provider error:', err); + tabs.forEach(tab => tab.update(() => ({ + state: State.PAGE_ERROR, + error: `Translation provider error: ${err.message}`, + }))); + // Try falling back to WASM is the current provider doesn't work // out. Might lose some translations the process but // InPageTranslation should be able to deal with that. diff --git a/extension/controller/translation/WASMTranslationHelper.js b/extension/controller/translation/WASMTranslationHelper.js index 800d33b6..c62a4cfb 100644 --- a/extension/controller/translation/WASMTranslationHelper.js +++ b/extension/controller/translation/WASMTranslationHelper.js @@ -428,11 +428,15 @@ class WorkerChannel { // No worker free, but space for more? if (!worker && this.workers.length < this.workerLimit) { - worker = { - idle: true, - worker: this.loadWorker() - }; - this.workers.push(worker); + try { + worker = { + idle: true, + worker: this.loadWorker() + }; + this.workers.push(worker); + } catch (e) { + this.onerror(new Error(`Could not initialise translation worker: ${e.message}`)); + } } // If no worker, that's the end of it. diff --git a/extension/controller/translation/WASMTranslationWorker.js b/extension/controller/translation/WASMTranslationWorker.js index 5358551d..5362896b 100644 --- a/extension/controller/translation/WASMTranslationWorker.js +++ b/extension/controller/translation/WASMTranslationWorker.js @@ -68,25 +68,33 @@ class WASMTranslationWorker { */ loadModule() { return new Promise(async (resolve, reject) => { - const response = await fetch('bergamot-translator-worker.wasm'); - - Object.assign(Module, { - instantiateWasm: (info, accept) => { - WebAssembly.instantiateStreaming(response, { - ...info, - 'wasm_gemm': this.options.useNativeIntGemm - ? this.linkNativeIntGemm(info) - : this.linkFallbackIntGemm(info) - }).then(({instance}) => accept(instance)); - return {}; - }, - onRuntimeInitialized: () => { - resolve(Module); - } - }); - - // Emscripten glue code - importScripts('bergamot-translator-worker.js'); + try { + const response = await fetch('bergamot-translator-worker.wasm'); + + Object.assign(Module, { + instantiateWasm: (info, accept) => { + try { + WebAssembly.instantiateStreaming(response, { + ...info, + 'wasm_gemm': this.options.useNativeIntGemm + ? this.linkNativeIntGemm(info) + : this.linkFallbackIntGemm(info) + }).then(({instance}) => accept(instance)).catch(reject); + } catch (err) { + reject(err); + } + return {}; + }, + onRuntimeInitialized: () => { + resolve(Module); + } + }); + + // Emscripten glue code + importScripts('bergamot-translator-worker.js'); + } catch (err) { + reject(err); + } }); } @@ -219,6 +227,7 @@ class WASMTranslationWorker { } } + onmessage = ({data}) => { if (!data.options){ console.warn('Did not receive initial message with options'); @@ -233,7 +242,7 @@ onmessage = ({data}) => { const result = await worker[message.name](...message.args); postMessage({id, message: result}); } catch (err) { - console.error(err); + console.error('Worker runtime error', err); postMessage({ id, error: {