From 090ff116d453a9f00b4ce8adffb8c75811d3e712 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Thu, 16 Jan 2020 14:55:08 +0100 Subject: [PATCH] Ensure that full clean-up is always run when handling the "Terminate" message in `src/core/worker.js` This is beneficial in situations where the Worker is being re-used, for example with fake workers, since it ensures that things like font resources are actually released. --- src/core/document.js | 13 ++++++++++--- src/core/worker.js | 10 ++++++++-- 2 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/core/document.js b/src/core/document.js index 420fa00c3..3cba84d1a 100644 --- a/src/core/document.js +++ b/src/core/document.js @@ -33,7 +33,14 @@ import { warn, } from "../shared/util.js"; import { Catalog, ObjectLoader, XRef } from "./obj.js"; -import { Dict, isDict, isName, isStream, Ref } from "./primitives.js"; +import { + clearPrimitiveCaches, + Dict, + isDict, + isName, + isStream, + Ref, +} from "./primitives.js"; import { getInheritableProperty, MissingDataException, @@ -815,8 +822,8 @@ class PDFDocument { return this.catalog.fontFallback(id, handler); } - cleanup() { - return this.catalog.cleanup(); + async cleanup() { + return this.catalog ? this.catalog.cleanup() : clearPrimitiveCaches(); } } diff --git a/src/core/worker.js b/src/core/worker.js index 12e46fe73..74c5fc566 100644 --- a/src/core/worker.js +++ b/src/core/worker.js @@ -593,16 +593,22 @@ var WorkerMessageHandler = { handler.on("Terminate", function wphTerminate(data) { terminated = true; + + const waitOn = []; if (pdfManager) { pdfManager.terminate(new AbortException("Worker was terminated.")); + + const cleanupPromise = pdfManager.cleanup(); + waitOn.push(cleanupPromise); + pdfManager = null; + } else { + clearPrimitiveCaches(); } if (cancelXHRs) { cancelXHRs(new AbortException("Worker was terminated.")); } - clearPrimitiveCaches(); - var waitOn = []; WorkerTasks.forEach(function(task) { waitOn.push(task.finished); task.terminate();