Skip to content

Fix PermissionError on Windows when parsing multiple PDFs (#678)#702

Open
AmSach wants to merge 1 commit intocamelot-dev:masterfrom
AmSach:master
Open

Fix PermissionError on Windows when parsing multiple PDFs (#678)#702
AmSach wants to merge 1 commit intocamelot-dev:masterfrom
AmSach:master

Conversation

@AmSach
Copy link
Copy Markdown

@AmSach AmSach commented Apr 27, 2026

Fixes #678

Bug

When parsing multiple PDFs on Windows with Python 3.13+, Camelot raises PermissionError during atexit cleanup of temporary directories. This happens because:

  1. TemporaryDirectory registers shutil.rmtree via atexit to delete the temp dir at program exit
  2. When processing multiple PDFs, the pdfium/pypdf file handles may still be open when atexit runs
  3. On Windows, open file handles prevent directory deletion, causing PermissionError (WinError 5)

Fix

In TemporaryDirectory.exit, we now:

  1. Unregister the atexit callback to prevent double-cleanup
  2. Immediately call shutil.rmtree with ignore_errors=True while file handles may still be closing

This ensures the temp directory is cleaned up deterministically when the context manager exits, rather than deferring to program exit where timing of handle release is unreliable on Windows.

TemporaryDirectory.__exit__ now unregisters the atexit callback and
immediately deletes the temp directory instead of deferring cleanup to
program exit. This prevents PermissionError (WinError 5) when pdfium
file handles are still open during atexit cleanup on Windows with
Python 3.13+.

Fixes camelot-dev#678
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

PermissionError when parsing multiple PDFs on Windows with Camelot

1 participant