Skip to content

fix: remove orphan trashinfo after failed rename - #113

Open
pentaoa wants to merge 2 commits into
arsenetar:masterfrom
pentaoa:fix/101-orphan-trashinfo
Open

pentaoa wants to merge 2 commits into
arsenetar:masterfrom
pentaoa:fix/101-orphan-trashinfo

Conversation

@pentaoa

@pentaoa pentaoa commented Sep 9, 2026

Copy link
Copy Markdown

Summary

Fixes #101.

trash_move() writes the .trashinfo file before calling os.rename(). If the rename fails with EXDEV, send2trash() retries with shutil.move(), but the first information file remains. The retry then chooses a different destination name, leaving an orphan record in the trash.

Remove the information file created by the failed rename before re-raising the error. Cleanup uses the actual collision-resolved filename, so it does not delete an older trash entry with the same original name. The existing cross-device retry can then reuse that name.

This preserves the required information-file-before-move ordering in the Trash specification. The shutil.move() branch is unchanged; this patch handles the failed initial rename, not partial-copy recovery.

Tests

Four new regression cases fail before the fix and pass after it:

  • Cross-device retry with an empty trash.
  • Cross-device retry with an existing same-name file and information record; both existing contents are preserved.
  • Failed EXDEV and EACCES renames leave the source intact, remove the new information record, and propagate the original exception.

Validation on macOS with Python 3.14.3:

python -m pytest tests/test_plat_other.py -k 'cross_device_retry or rename_failure or trash_topdir' -q
7 passed, 6 deselected

flake8 .
# passed

The new tests use temporary directories, stub device detection, and force os.rename() to raise EXDEV; the retry uses the real shutil.move() copy fallback. No real bind mounts or user's trash are required. The selected existing tests cover external-volume trash success, fallback, and permission failure. The complete platform/Python CI matrix was not run locally.

AI assistance

Codex implemented the patch and ran the checks at my request. This contribution is based on reproducing the reported issue; I am not claiming production usage of Send2Trash.

Comment thread send2trash/plat_other.py Outdated
try:
os.rename(src, dest_path)
except OSError:
os.unlink(info_file)

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So if there is an error on unlink that could surface to the user instead of the original error, possible handling could be:

   with contextlib.suppress(OSError):
       os.unlink(info_file)
   raise

Although its unlikely in most cases and throwing away that error if it happens is probably better than surfacing.

This branch has not been deployed

No deployments
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.

Orphan trashinfo files created on EXDEV fallback with bind mounts

3 participants