Skip to content

Fix unhandled PermissionError during file retention sweep on Windows - #1498

Draft
aryansk wants to merge 1 commit into
Delgan:masterfrom
aryansk:fix-retention-permission-error-1495
Draft

Fix unhandled PermissionError during file retention sweep on Windows#1498
aryansk wants to merge 1 commit into
Delgan:masterfrom
aryansk:fix-retention-permission-error-1495

Conversation

@aryansk

@aryansk aryansk commented Aug 15, 2026

Copy link
Copy Markdown

Fixes #1495

Problem

On program exit, loguru deletes log files that fall out of a retention policy. On Windows, when another process still holds one of those files open, os.remove() raises PermissionError, which:

  1. propagates unhandled during interpreter exit (stack trace), and
  2. aborts the sweep, so the remaining out-of-retention files that could have been deleted are left behind.

The same class of failure occurs when a file is deleted concurrently between the glob() and the os.remove() call (FileNotFoundError).

Change

Both built-in retention functions (Retention.retention_count and Retention.retention_age) now handle per-file PermissionError and FileNotFoundError — skipping the affected file and continuing the sweep, so the failure of one file never blocks the deletion of the others or bubbles up during exit. retention_count's sort key also tolerates a file that disappears before sorting.

The behavior of user-supplied retention= functions is unchanged (exceptions from custom functions still propagate, as covered by the existing test_exception_during_retention_at_remove).

Testing

  • New regression tests: locked file skipped while others are still deleted (count-based), concurrently-deleted file tolerated (count-based), locked file skipped (age-based). All three fail on main and pass with the fix.
  • Full suite: 1595 passed, 53 skipped.

Note on AI-assisted contribution, per loguru's CONTRIBUTING policy: this change was drafted with the assistance of an AI coding tool and reviewed and tested by a human before submission.

The built-in retention functions (retention_count and retention_age) now
skip files that another process has locked (PermissionError on Windows)
or that were deleted concurrently (FileNotFoundError), instead of letting
the exception propagate and aborting the sweep - which also prevented the
other out-of-retention files from being deleted.
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 raised on Windows during logfile deletion for retention policies propagates unhandled during exit

1 participant