fix: prevent PdfiumLibraryBindingsAlreadyInitialized race condition
Problem
When multiple PDFs are processed concurrently (e.g. during library scanning), every call to PdfProcessor::renderer() attempts Pdfium::bind_to_library(), which can only succeed once per process. The second and subsequent concurrent calls fail with:
PdfiumLibraryBindingsAlreadyInitialized → 500 Internal Server Error
This breaks PDF thumbnail generation, page rendering, and the built-in reader for all PDF files when more than one PDF is scanned or opened at the same time.
Fix
Wrap the Pdfium instance in a global Mutex<Option<Pdfium>>:
- First call: binds the PDFium native library and stores the
Pdfiuminstance - Subsequent calls: reuse the already-initialized instance
PdfiumRefwrapper: holds theMutexGuardand implementsDeref<Target=Pdfium>, so all existing callers work without changes- Serialization: the global lock also serializes PDFium access, preventing the thread-safety issues discussed in #1190
Changes
core/src/filesystem/media/format/pdf.rs — 23 lines added, 16 removed. No other files touched.
Related
- PR #1190 discussed using
OnceLock<Mutex<Pdfium>>but the locking mechanism was left as a follow-up item
Tested
Built and deployed on macOS (ARM64 Docker) with a library containing 100+ PDFs — all thumbnails now generate correctly, no more 500 errors.
🤖 Generated with Claude Code
There is already an active effort to improve this situation in https://github.com/stumpapp/stump/pull/1209