[BUG] `TemporaryDirectory` cleanup fails if the directory contains a symlink

Open Beginner friendly
#7,778 0 comments 0 reactions 0 assignees View on GitHub

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

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

accepted bug bug_feature_triaged good first issue mojo Team: Mojo Libraries

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 rmdir fails with "Directory not empty".
  • A symlink to a directory looks like a directory, so _rmtree recurses 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

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from modular/modular

All issues in modular/modular

Similar issues

More Operating Systems issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.