From b982ac42e4120fe99ee683fdace69fc3f39bf66e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicol=C3=B2=20Ribaudo?= Date: Fri, 23 Oct 2026 19:47:22 +0200 Subject: [PATCH] Suppress more errors due to closed documents Closing/switching the document in the viewer will cause multiple tasks to fail. Other code paths already have `if (pdfDocument !== this.pdfDocument) return;` checks to know if the document has been changed after an async step, but it was missing in some places leading to errors being logged in the console. This commit adds the check in those places. --- web/pdf_viewer.js | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/web/pdf_viewer.js b/web/pdf_viewer.js index bdb2a7d15..d36c9ea1e 100644 --- a/web/pdf_viewer.js +++ b/web/pdf_viewer.js @@ -1195,6 +1195,10 @@ class PDFViewer { } }, reason => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while loading the page. + } + console.error( `Unable to get page ${pageNum} to initialize viewer`, reason @@ -1207,6 +1211,10 @@ class PDFViewer { if (pageNum % PagesCountLimit.PAUSE_EAGER_PAGE_INIT === 0) { await promise; + + if (pdfDocument !== this.pdfDocument) { + break; // The document was closed, stop fetching more pages. + } } } }); @@ -1230,6 +1238,10 @@ class PDFViewer { } }) .catch(reason => { + if (pdfDocument !== this.pdfDocument) { + return; // The document was closed while the first page loaded. + } + console.error("Unable to initialize viewer", reason); this._pagesCapability.reject(reason); @@ -2137,13 +2149,17 @@ class PDFViewer { if (pageView.pdfPage) { return pageView.pdfPage; } + const { pdfDocument } = this; try { - const pdfPage = await this.pdfDocument.getPage(pageView.id); + const pdfPage = await pdfDocument.getPage(pageView.id); if (!pageView.pdfPage) { pageView.setPdfPage(pdfPage); } return pdfPage; } catch (reason) { + if (pdfDocument !== this.pdfDocument) { + return null; // The document was closed while the page was loading. + } console.error("Unable to get page for page view", reason); return null; // Page error -- there is nothing that can be done. } @@ -2193,8 +2209,10 @@ class PDFViewer { ); if (pageView) { - this.#ensurePdfPageLoaded(pageView).then(() => { - this.renderingQueue.renderView(pageView); + this.#ensurePdfPageLoaded(pageView).then(pdfPage => { + if (pdfPage !== null) { + this.renderingQueue.renderView(pageView); + } }); return true; }