Fix unhandled PermissionError during file retention sweep on Windows - #1498
Draft
aryansk wants to merge 1 commit into
Draft
Fix unhandled PermissionError during file retention sweep on Windows#1498aryansk wants to merge 1 commit into
aryansk wants to merge 1 commit into
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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()raisesPermissionError, which:The same class of failure occurs when a file is deleted concurrently between the
glob()and theos.remove()call (FileNotFoundError).Change
Both built-in retention functions (
Retention.retention_countandRetention.retention_age) now handle per-filePermissionErrorandFileNotFoundError— 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 existingtest_exception_during_retention_at_remove).Testing
mainand pass with the fix.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.