Skip to content

#1 PDF Application on GitHub that lets you edit PDFs on any device anywhere

fix(frontend): prevent PDF preview and worker memory leaks (#7281)

Ludyeebd3327d20c
diff --git a/frontend/editor/src/core/components/tools/addStamp/StampSetupSettings.test.tsx b/frontend/editor/src/core/components/tools/addStamp/StampSetupSettings.test.tsx
new file mode 100644
index 0000000000..287e67b8af
--- /dev/null
+++ b/frontend/editor/src/core/components/tools/addStamp/StampSetupSettings.test.tsx
@@ -0,0 +1,62 @@
+import { MantineProvider } from "@mantine/core";
+import { render } from "@testing-library/react";
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import StampSetupSettings from "@app/components/tools/addStamp/StampSetupSettings";
+import { defaultParameters } from "@app/components/tools/addStamp/useAddStampParameters";
+
+describe("StampSetupSettings image preview", () => {
+ const onParameterChange = vi.fn();
+ const stampImage = new File(["image"], "stamp.png", {
+ type: "image/png",
+ });
+
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ const renderImageStamp = () =>
+ render(
+ <MantineProvider>
+ <StampSetupSettings
+ parameters={{
+ ...defaultParameters,
+ stampType: "image",
+ stampImage,
+ }}
+ onParameterChange={onParameterChange}
+ />
+ </MantineProvider>,
+ );
+
+ it("creates one preview URL per selected image, not per render", () => {
+ const createObjectURL = vi.mocked(URL.createObjectURL);
+ const { rerender } = renderImageStamp();
+
+ expect(createObjectURL).toHaveBeenCalledTimes(1);
+
+ rerender(
+ <MantineProvider>
+ <StampSetupSettings
+ parameters={{
+ ...defaultParameters,
+ stampType: "image",
+ stampImage,
+ pageNumbers: "2",
+ }}
+ onParameterChange={onParameterChange}
+ />
+ </MantineProvider>,
+ );
+
+ expect(createObjectURL).toHaveBeenCalledTimes(1);
+ });
+
+ it("revokes the preview URL when the component unmounts", () => {
+ const revokeObjectURL = vi.mocked(URL.revokeObjectURL);
+ const { unmount } = renderImageStamp();
+
+ unmount();
+
+ expect(revokeObjectURL).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/frontend/editor/src/core/contexts/file/lifecycle.test.ts b/frontend/editor/src/core/contexts/file/lifecycle.test.ts
new file mode 100644
index 0000000000..8bb6dcd46c
--- /dev/null
+++ b/frontend/editor/src/core/contexts/file/lifecycle.test.ts
@@ -0,0 +1,25 @@
+import { beforeEach, describe, expect, it, vi } from "vitest";
+import { FileLifecycleManager } from "@app/contexts/file/lifecycle";
+import type { FileId } from "@app/types/file";
+
+describe("FileLifecycleManager", () => {
+ beforeEach(() => {
+ vi.clearAllMocks();
+ });
+
+ it("revokes tracked blob URLs during cleanup", () => {
+ const fileId = "file-1" as FileId;
+ const filesRef = {
+ current: new Map<FileId, File>([[fileId, new File(["pdf"], "file.pdf")]]),
+ };
+ const dispatch = vi.fn();
+ const manager = new FileLifecycleManager(filesRef, dispatch);
+ const blobUrl = "blob:file-1";
+ manager.trackBlobUrl(blobUrl);
+
+ manager.cleanupAllFiles();
+
+ expect(URL.revokeObjectURL).toHaveBeenCalledWith(blobUrl);
+ expect(URL.revokeObjectURL).toHaveBeenCalledTimes(1);
+ });
+});
diff --git a/frontend/editor/src/core/contexts/file/lifecycle.ts b/frontend/editor/src/core/contexts/file/lifecycle.ts
index 478199b610..41416956c0 100644
--- a/frontend/editor/src/core/contexts/file/lifecycle.ts
+++ b/frontend/editor/src/core/contexts/file/lifecycle.ts
@@ -40,6 +40,16 @@ export class FileLifecycleManager {
}
};
+ private revokeBlobUrl = (url: string): void => {
+ if (!url.startsWith("blob:")) return;
+ try {
+ URL.revokeObjectURL(url);
+ } catch {
+ // Ignore revocation errors.
+ }
+ this.blobUrls.delete(url);
+ };
+
/**
* Clean up resources for a specific file (with stateRef access for complete cleanup)
*/
@@ -158,32 +168,14 @@ export class FileLifecycleManager {
const record = stateRef.current.files.byId[fileId];
if (record) {
// Clean up thumbnail blob URLs
- if (record.thumbnailUrl && record.thumbnailUrl.startsWith("blob:")) {
- try {
- URL.revokeObjectURL(record.thumbnailUrl);
- } catch {
- // Ignore revocation errors
- }
- }
+ if (record.thumbnailUrl) this.revokeBlobUrl(record.thumbnailUrl);
- if (record.blobUrl && record.blobUrl.startsWith("blob:")) {
- try {
- URL.revokeObjectURL(record.blobUrl);
- } catch {
- // Ignore revocation errors
- }
- }
+ if (record.blobUrl) this.revokeBlobUrl(record.blobUrl);
// Clean up processed file thumbnails
if (record.processedFile?.pages) {
record.processedFile.pages.forEach((page: ProcessedFilePage) => {
- if (page.thumbnail && page.thumbnail.startsWith("blob:")) {
- try {
- URL.revokeObjectURL(page.thumbnail);
- } catch {
- // Ignore revocation errors
- }
- }
+ if (page.thumbnail) this.revokeBlobUrl(page.thumbnail);
});
}
}
diff --git a/frontend/editor/src/core/hooks/tools/compare/useCompareOperation.ts b/frontend/editor/src/core/hooks/tools/compare/useCompareOperation.ts
index 73667946eb..18e551df64 100644
--- a/frontend/editor/src/core/hooks/tools/compare/useCompareOperation.ts
+++ b/frontend/editor/src/core/hooks/tools/compare/useCompareOperation.ts
@@ -680,6 +680,8 @@ export const useCompareOperation = (): CompareOperationHook => {
useEffect(() => {
return () => {
+ cancelledRef.current = true;
+ pixelSignalRef.current.cancelled = true;
cleanupDownloadUrl();
revokePixelUrls();
if (workerRef.current) {
diff --git a/frontend/editor/src/core/hooks/useEnhancedProcessedFiles.ts b/frontend/editor/src/core/hooks/useEnhancedProcessedFiles.ts
index 262bda146e..414be57431 100644
--- a/frontend/editor/src/core/hooks/useEnhancedProcessedFiles.ts
+++ b/frontend/editor/src/core/hooks/useEnhancedProcessedFiles.ts
@@ -47,6 +47,8 @@ export function useEnhancedProcessedFiles(
const [processingStates, setProcessingStates] = useState<
Map<string, ProcessingState>
>(new Map());
+ const processedFilesRef = useRef(processedFiles);
+ processedFilesRef.current = processedFiles;
// Subscribe to processing state changes once
useEffect(() => {
@@ -57,6 +59,7 @@ export function useEnhancedProcessedFiles(
// Process files when activeFiles changes
useEffect(() => {
+ let cancelled = false;
console.log(
"useEnhancedProcessedFiles: activeFiles changed",
activeFiles.length,
@@ -77,6 +80,7 @@ export function useEnhancedProcessedFiles(
const newProcessedFiles = new Map<File, ProcessedFile>();
for (const file of activeFiles) {
+ if (cancelled) return;
// Generate hash for this file
const fileHash = await FileHasher.generateHybridHash(file);
fileHashMapRef.current.set(file, fileHash);
@@ -120,57 +124,67 @@ export function useEnhancedProcessedFiles(
(file) => !processedFiles.has(file),
);
- if (hasChanged) {
+ if (hasChanged && !cancelled) {
+ processedFilesRef.current = newProcessedFiles;
setProcessedFiles(newProcessedFiles);
}
};
processFiles();
+ return () => {
+ cancelled = true;
+ };
}, [activeFiles]); // Only depend on activeFiles to avoid infinite loops
// Listen for processing completion
useEffect(() => {
+ const checkInFlightRef = { current: false };
const checkForCompletedFiles = async () => {
+ if (checkInFlightRef.current) return;
+ checkInFlightRef.current = true;
let hasNewFiles = false;
- const updatedFiles = new Map(processedFiles);
-
- // Generate file keys for all files first
- const fileKeyPromises = activeFiles.map(async (file) => ({
- file,
- key: await FileHasher.generateHybridHash(file),
- }));
-
- const fileKeyPairs = await Promise.all(fileKeyPromises);
-
- for (const { file, key } of fileKeyPairs) {
- // Only check files that don't have processed results yet
- if (!updatedFiles.has(file)) {
- const processingState = processingStates.get(key);
-
- // Check for both processing and recently completed files
- // This ensures we catch completed files before they're cleaned up
- if (
- processingState?.status === "processing" ||
- processingState?.status === "completed"
- ) {
- try {
- const processed = await enhancedPDFProcessingService.processFile(
- file,
- config,
- );
- if (processed) {
- updatedFiles.set(file, processed);
- hasNewFiles = true;
+ const updatedFiles = new Map(processedFilesRef.current);
+
+ try {
+ // Generate file keys for all files first
+ const fileKeyPromises = activeFiles.map(async (file) => ({
+ file,
+ key: await FileHasher.generateHybridHash(file),
+ }));
+
+ const fileKeyPairs = await Promise.all(fileKeyPromises);
+
+ for (const { file, key } of fileKeyPairs) {
+ // Only check files that don't have processed results yet
+ if (!updatedFiles.has(file)) {
+ const processingState = processingStates.get(key);
+
+ // Check for both processing and recently completed files
+ // This ensures we catch completed files before they're cleaned up
+ if (
+ processingState?.status === "processing" ||
+ processingState?.status === "completed"
+ ) {
+ try {
+ const processed =
+ await enhancedPDFProcessingService.processFile(file, config);
+ if (processed) {
+ updatedFiles.set(file, processed);
+ hasNewFiles = true;
+ }
+ } catch {
+ // Ignore errors in completion check
}
- } catch {
- // Ignore errors in completion check
}
}
}
- }
- if (hasNewFiles) {
- setProcessedFiles(updatedFiles);
+ if (hasNewFiles) {
+ processedFilesRef.current = updatedFiles;
+ setProcessedFiles(updatedFiles);
+ }
+ } finally {
+ checkInFlightRef.current = false;
}
};
diff --git a/frontend/editor/src/core/services/pdfWorkerManager.ts b/frontend/editor/src/core/services/pdfWorkerManager.ts
index 6da74e1dd1..d3b77a2885 100644
--- a/frontend/editor/src/core/services/pdfWorkerManager.ts
+++ b/frontend/editor/src/core/services/pdfWorkerManager.ts
@@ -14,6 +14,7 @@ import {
class PDFWorkerManager {
private static instance: PDFWorkerManager;
private activeDocuments = new Set<PDFDocumentProxy>();
+ private destroyingDocuments = new WeakSet<PDFDocumentProxy>();
private workerCount = 0;
private maxWorkers = 10; // Limit concurrent workers
private isInitialized = false;
@@ -54,11 +55,12 @@ class PDFWorkerManager {
disableStream?: boolean;
stopAtErrors?: boolean;
verbosity?: number;
+ signal?: { cancelled: boolean };
} = {},
): Promise<PDFDocumentProxy> {
// Wait if we've hit the worker limit
if (this.activeDocuments.size >= this.maxWorkers) {
- await this.waitForAvailableWorker();
+ await this.waitForAvailableWorker(options.signal);
}
// Normalize input data to PDF.js format
@@ -105,7 +107,7 @@ class PDFWorkerManager {
// If document creation fails, make sure to clean up the loading task
if (loadingTask) {
try {
- loadingTask.destroy();
+ void loadingTask.destroy();
} catch {
// Ignore errors
}
@@ -117,43 +119,50 @@ class PDFWorkerManager {
/**
* Properly destroy a PDF document and clean up resources
*/
- destroyDocument(pdf: PDFDocumentProxy): void {
- if (this.activeDocuments.has(pdf)) {
- try {
- pdf.destroy();
- this.activeDocuments.delete(pdf);
- this.workerCount = Math.max(0, this.workerCount - 1);
- } catch {
- // Still remove from tracking even if destroy failed
- this.activeDocuments.delete(pdf);
- this.workerCount = Math.max(0, this.workerCount - 1);
- }
+ async destroyDocument(pdf: PDFDocumentProxy): Promise<void> {
+ if (!this.activeDocuments.has(pdf) || this.destroyingDocuments.has(pdf)) {
+ return;
+ }
+ this.destroyingDocuments.add(pdf);
+ try {
+ await pdf.destroy();
+ } catch {
+ // Still remove from tracking if destroy fails.
+ } finally {
+ this.destroyingDocuments.delete(pdf);
+ this.activeDocuments.delete(pdf);
+ this.workerCount = Math.max(0, this.workerCount - 1);
}
}
/**
* Destroy all active PDF documents
*/
- destroyAllDocuments(): void {
+ async destroyAllDocuments(): Promise<void> {
const documentsToDestroy = Array.from(this.activeDocuments);
- documentsToDestroy.forEach((pdf) => {
- this.destroyDocument(pdf);
- });
-
- this.activeDocuments.clear();
- this.workerCount = 0;
+ await Promise.all(
+ documentsToDestroy.map((pdf) => this.destroyDocument(pdf)),
+ );
}
/**
* Wait for a worker to become available
*/
- private async waitForAvailableWorker(): Promise<void> {
- return new Promise((resolve) => {
+ private async waitForAvailableWorker(signal?: {
+ cancelled: boolean;
+ }): Promise<void> {
+ return new Promise((resolve, reject) => {
+ let timer: ReturnType<typeof setTimeout> | null = null;
const checkAvailability = () => {
+ if (signal?.cancelled) {
+ if (timer !== null) clearTimeout(timer);
+ reject(new Error("CANCELLED"));
+ return;
+ }
if (this.activeDocuments.size < this.maxWorkers) {
resolve();
} else {
- setTimeout(checkAvailability, 100);
+ timer = setTimeout(checkAvailability, 100);
}
};
checkAvailability();
@@ -178,7 +187,7 @@ class PDFWorkerManager {
// Force destroy all documents
this.activeDocuments.forEach((pdf) => {
try {
- pdf.destroy();
+ void pdf.destroy();
} catch {
// Ignore errors
}
diff --git a/frontend/editor/src/core/services/pixelCompareService.ts b/frontend/editor/src/core/services/pixelCompareService.ts
index 0e61582923..4a5db2e22d 100644
--- a/frontend/editor/src/core/services/pixelCompareService.ts
+++ b/frontend/editor/src/core/services/pixelCompareService.ts
@@ -89,12 +89,17 @@ export const runPixelCompare = async ({
};
return await new Promise<CompareResultPixelData>((resolve, reject) => {
+ let settled = false;
+ let cancellationTimer: ReturnType<typeof setInterval> | null = null;
+
const handleMessage = (event: MessageEvent<PixelCompareWorkerResponse>) => {
+ if (settled) return;
const message = event.data;
if (!message) return;
if (signal?.cancelled) {
terminateWorker();
cleanupOnFailure();
+ settled = true;
reject(new Error("CANCELLED"));
return;
}
@@ -131,6 +136,7 @@ export const runPixelCompare = async ({
case "success": {
pages.sort((a, b) => a.pageNumber - b.pageNumber);
terminateWorker();
+ settled = true;
resolve({
mode: "pixel",
base: { fileId: baseFileId, fileName: baseFile.name },
@@ -151,6 +157,7 @@ export const runPixelCompare = async ({
case "error": {
terminateWorker();
cleanupOnFailure();
+ settled = true;
reject(new Error(message.message));
break;
}
@@ -158,14 +165,20 @@ export const runPixelCompare = async ({
};
const handleError = (event: ErrorEvent) => {
+ if (settled) return;
terminateWorker();
cleanupOnFailure();
+ settled = true;
reject(
event.error ?? new Error(event.message || "Pixel compare worker error"),
);
};
const terminateWorker = () => {
+ if (cancellationTimer !== null) {
+ clearInterval(cancellationTimer);
+ cancellationTimer = null;
+ }
worker.removeEventListener("message", handleMessage as EventListener);
worker.removeEventListener("error", handleError as EventListener);
try {
@@ -178,6 +191,17 @@ export const runPixelCompare = async ({
worker.addEventListener("message", handleMessage as EventListener);
worker.addEventListener("error", handleError as EventListener);
+ if (signal) {
+ cancellationTimer = setInterval(() => {
+ if (signal.cancelled && !settled) {
+ terminateWorker();
+ cleanupOnFailure();
+ settled = true;
+ reject(new Error("CANCELLED"));
+ }
+ }, 50);
+ }
+
const request: PixelCompareWorkerRequest = {
type: "pixel-compare",
payload: {
diff --git a/frontend/editor/src/proprietary/services/heuristic/heuristicExtractor.ts b/frontend/editor/src/proprietary/services/heuristic/heuristicExtractor.ts
index fb51b407b6..88a3c27bcc 100644
--- a/frontend/editor/src/proprietary/services/heuristic/heuristicExtractor.ts
+++ b/frontend/editor/src/proprietary/services/heuristic/heuristicExtractor.ts
@@ -38,12 +38,16 @@ export async function extractHeuristicDoc(
if (pageCount >= 1) {
// Page 1 feeds three zones (first, title, window); pump its items once.
const page1 = await pdfDoc.getPage(1);
- const items = await pageTextItems(page1);
- firstZone = textFromItems(items);
- titleZone = titleFromLines(
- buildLines(items),
- page1.getViewport({ scale: 1 }).height,
- );
+ try {
+ const items = await pageTextItems(page1);
+ firstZone = textFromItems(items);
+ titleZone = titleFromLines(
+ buildLines(items),
+ page1.getViewport({ scale: 1 }).height,
+ );
+ } finally {
+ page1.cleanup();
+ }
}
const parts: string[] = [];
for (const pageNo of windowPages(pageCount)) {
@@ -108,7 +112,11 @@ async function pageText(
): Promise<string> {
if (pageNo < 1 || pageNo > pdfDoc.numPages) return "";
const page = await pdfDoc.getPage(pageNo);
- return textFromItems(await pageTextItems(page));
+ try {
+ return textFromItems(await pageTextItems(page));
+ } finally {
+ page.cleanup();
+ }
}
function textFromItems(items: readonly unknown[]): string {