Stirling-PDF icon indicating copy to clipboard operation
Stirling-PDF copied to clipboard

Bug: "Generating previews..." spinner never completes after PDF compression

Open Alingsaskommun opened this issue 8 months ago • 3 comments

Bug: "Generating previews..." spinner never completes after PDF compression

Version: 2.7.2 Deployment: Docker (self-hosted)

Describe the bug After compressing a PDF, the compression itself completes successfully (confirmed in logs), but the UI gets stuck on "Generating previews..." indefinitely. The download button never appears and the user cannot proceed.

Steps to reproduce

  1. Upload a PDF file (tested with a 3.53 MB file containing embedded JPEG images)
  2. Select compression settings and click Compress
  3. Compression completes successfully (visible in logs)
  4. UI shows "Generating previews..." spinner — never resolves

Expected behavior After compression completes, preview is generated and the download button appears.

Actual behavior UI is permanently stuck on "Generating previews...". No error is shown to the user.

Logs Compression completes successfully at e.g. 06:42:31:

INFO CompressController - Overall PDF compression: 3.53 MB → 780.48 KB (reduced by 78.4%)
INFO CompressController - Saving compressed PDF to /tmp/stirling-pdf/stirling-pdf/stirling-pdf-14594374465729347323.pdf

Three seconds later, TempFileCleanupService runs:

INFO TempFileCleanupService - Running scheduled temporary file cleanup
INFO TempFileManager - Cleaned up 0 old temporary files

No preview-related log entries appear after compression completes.

Environment

  • Docker image: stirlingtools/stirling-pdf:2.7.2
  • JVM: OpenJDK 25.0.2
  • STIRLING_JVM_PROFILE=balanced
  • SECURITY_LOGINMETHOD=saml2
  • PREMIUM_ENABLED=true
  • Host OS: Ubuntu, SSD storage
  • Memory at time of compression: ~1.65 GiB used of 3.82 GiB available (43%)

Workaround None found. User must reload the page and re-upload the file.

Alingsaskommun avatar Mar 24 '26 06:03 Alingsaskommun

Hey, I would like to solve this issue, could you please assign me? @Alingsaskommun @Frooodle @ConnorYoh

MateusLyoshka avatar Mar 24 '26 18:03 MateusLyoshka

Investigation

Traced the full frontend flow from compress API response → thumbnail generation → UI state. The backend works fine (as reported). The bug is in the frontend thumbnail/worker lifecycle.

Root Cause Chain

1. PDF document leak in generatePDFThumbnail (primary) frontend/src/core/utils/thumbnailUtils.ts ~L544-565

If generateStandardPDFThumbnail() throws any error that isn't a PasswordException, the PDF document is never destroyed — the worker stays in activeDocuments permanently:

try {
  const pdf = await pdfWorkerManager.createDocument(arrayBuffer, { ... });
  const thumbnail = await generateStandardPDFThumbnail(pdf, scale); // if this throws...
  pdfWorkerManager.destroyDocument(pdf); // ...this is skipped
  return thumbnail;
} catch (error) {
  if (error instanceof Error && error.name === "PasswordException") {
    return generateEncryptedPDFThumbnail(file);
  }
  throw error; // re-thrown WITHOUT destroying the PDF
}

The same pattern exists in generateThumbnailWithMetadata (~L695-704) — the catch returns a placeholder but never destroys the PDF.

2. Worker pool exhaustion frontend/src/core/services/pdfWorkerManager.ts ~L143-154

After enough leaked documents, the worker pool (max 10) fills up. waitForAvailableWorker() blocks indefinitely with no timeout or fallback.

3. isGeneratingThumbnails never reset on error frontend/src/core/hooks/tools/shared/useToolOperation.ts ~L305-310

actions.setGeneratingThumbnails(true);
const [thumbnails, downloadInfo] = await Promise.all([
  generateThumbnails(processedFiles),
  createDownloadInfo(processedFiles, config.operationType),
]);
actions.setGeneratingThumbnails(false); // never reached if Promise.all rejects or hangs

The finally block resets isLoading but not isGeneratingThumbnails, so the spinner stays visible forever.

Reproduction Path

  1. Compress a PDF (backend succeeds, returns the file)
  2. Frontend calls generateThumbnails() on the result
  3. PDF rendering throws during thumbnail generation (e.g. corrupted page, unsupported feature)
  4. PDF document leaks → worker pool shrinks
  5. On repeated use, pool fills → waitForAvailableWorker hangs forever
  6. Promise.all never resolves → "Generating previews..." spinner stuck indefinitely

Suggested Fixes

  1. try/finally for PDF cleanup in generatePDFThumbnail and generateThumbnailWithMetadata — ensure destroyDocument is always called
  2. Reset isGeneratingThumbnails in the finally block of useToolOperation, not just isLoading
  3. Add a timeout to thumbnail generation so it can't hang indefinitely even if the worker pool is exhausted

dripsmvcp avatar Apr 11 '26 15:04 dripsmvcp

Thanks for this research! You are welcome to raise PR for this

Frooodle avatar Apr 11 '26 16:04 Frooodle