From ef081a053182bb323d3c769e8f6ed11b35c4357c Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 1 Jun 2018 12:52:47 +0200 Subject: [PATCH] Ensure that the `WorkerTransport._passwordCapability` is always rejected, even when errors are thrown in `PDFDocumentLoadingTask.onPassword` callback Please note that while the current code works, both in the viewer and the unit-tests, it can leave the `WorkerTransport._passwordCapability` Promise in a pending state. In the `PasswordRequest` handler, in src/display/api.js, we're returning the Promise from a `capability` object (rather than just a "plain" Promise). While an error thrown anywhere within this handler was fortunately enough to propagate it to the Worker side, it won't cause the Promise (in `WorkerTransport._passwordCapability`) to actually be rejected. Finally note that while we're now catching errors in the `PasswordRequest` handler, those errors are still propagated to the Worker side via the (now) rejected Promise and the existing `return this._passwordCapability.promise;` line. This prevents warnings about uncaught Promises, with messages such as "Error: Worker was destroyed during onPassword callback", when running the unit-tests both in browsers *and* in Node.js/Travis. --- src/core/worker.js | 4 ++-- src/display/api.js | 6 +++++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/core/worker.js b/src/core/worker.js index 742718aad..72c381a60 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -574,9 +574,9 @@ var WorkerMessageHandler = { finishWorkerTask(task); pdfManager.updatePassword(data.password); pdfManagerReady(); - }).catch(function (ex) { + }).catch(function (boundException) { finishWorkerTask(task); - handler.send('PasswordException', ex); + handler.send('PasswordException', boundException); }.bind(null, e)); } else if (e instanceof InvalidPDFException) { handler.send('InvalidPDF', e); diff --git a/src/display/api.js b/src/display/api.js index ca673438b..0003b8090 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1788,7 +1788,11 @@ var WorkerTransport = (function WorkerTransportClosure() { password, }); }; - loadingTask.onPassword(updatePassword, exception.code); + try { + loadingTask.onPassword(updatePassword, exception.code); + } catch (ex) { + this._passwordCapability.reject(ex); + } } else { this._passwordCapability.reject( new PasswordException(exception.message, exception.code));