[BUG] `TemporaryDirectory` cleanup fails if the directory contains a symlink
Maintainers usually reply within 2 days
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 75/100
- Issue type
- Bug
- Clarity
- Clearly specified
- Activity status
- Active
- Domain
- operating-systems
Research direction
Start in Mojo/stdlib/std/tempfile/tempfile.mojo at the _rmtree helper called from TemporaryDirectory.__exit__, and look at where each entry is classified with os.path.isfile/os.path.isdir. Switch that classification to an lstat-based check so a symlink is unlinked rather than followed or skipped, then recurse only into real directories. Verify with the two repro programs in the report (dangling link and link-to-directory): both should exit without an exception and leave the link target untouched. Adding a stdlib test alongside the existing tempfile tests would pin the regression.
Written by the indexing model from the issue text.
Description
The _rmtree helper behind TemporaryDirectory.__exit__ checks each entry with os.path.isfile and os.path.isdir. Both follow symlinks. That breaks cleanup in two ways:
- A dangling symlink is neither a file nor a directory, so it's skipped, and the final
rmdirfails with "Directory not empty". - A symlink to a directory looks like a directory, so
_rmtreerecurses into it. The recursive call then rejects the path because it's a symlink, and cleanup raises.
Either way the with block raises on exit, and the temporary directory stays on disk. Tests that create symlinks in a scratch directory (a common thing to test) hit this.
Dangling link:
import std.os
from std.tempfile import TemporaryDirectory
def main() raises:
with TemporaryDirectory() as tmp:
std.os.symlink("missing-target", std.os.path.join(tmp, "link"))
print("cleaned up")
$ mojo run rmtree_dangling.mojo
Unhandled exception caught during execution: rmdir /tmp/tmpkbxygmj9: Directory not empty (os error 39)
Link to a directory:
import std.os
from std.tempfile import TemporaryDirectory, mkdtemp
def main() raises:
var keep = mkdtemp()
with TemporaryDirectory() as tmp:
std.os.symlink(keep, std.os.path.join(tmp, "link"))
$ mojo run rmtree_dirlink.mojo
Unhandled exception caught during execution: `path`can not be a symbolic link: /tmp/tmpjhojx9he/link
The link target isn't touched, which is good, but the temp directory is leaked.
Python's shutil.rmtree checks each entry with lstat and unlinks symlinks without following them. I'd expect the same here: remove the link itself, never descend into its target. Passing ignore_cleanup_errors=True hides the exception but still leaves the directory behind.
Code: Mojo/stdlib/std/tempfile/tempfile.mojo, _rmtree.
Seen on the 2026-11-24 Mojo 1.3 nightly, Linux x86_64.
- Dominant language
- Mojo
- Stars
- 30.2k
- Forks
- 3.2k
- Avg merge
- 5d 18h
- Merged PRs (30d)
- 27
Getting set up
- No Dockerfile or Docker Compose file
- Has a pull request template
- Read the contributing guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from modular/modular
-
accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
bug_feature_triaged Team: Mojo Libraries
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
bug_feature_triaged Team: Mojo Libraries
Difficulty 2/5 1-3 hours Newbie friendliness 65/100
Maintainers usually reply within 2 days
-
bug_feature_triaged
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
Maintainers usually reply within 2 days
-
bug_feature_triaged Team: Mojo Libraries
Difficulty 2/5 1-3 hours Newbie friendliness 74/100
Maintainers usually reply within 2 days
Similar issues
-
Difficulty 1/5 1-3 hours Newbie friendliness 75/100
dividebysandwich/rust-dos#1795 · 1 comment ·
Maintainers usually reply within 1 day
-
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
apache/nuttx#22270 · 1 comment ·
Maintainers usually reply within 1 day
-
area: services kind: bug preview-effort: small priority: low
Difficulty 1/5 Under an hour Newbie friendliness 85/100
Maintainers usually reply within 1 day
-
Static Analysis failurePossibly taken A pull request linked to this issue is open or already merged. Open
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
akitaonrails/ai-jail#257 ·
Maintainers usually reply within 1 day