python / python/cpython

shutil.copytree infinite recursion crash on Windows Directory Junctions (WinError 206)

Offen
#142,155 0 Kommentare 0 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen

Dieses Issue hat noch niemand übernommen.

OS-windows stdlib type-bug
Vorherrschende Sprache
Python
Sterne
77.2k
Forks
35.9k
PR-Merge-Kennzahlen
PR-Kennzahlen ausstehend

Beschreibung

Bug report

Bug description:

Bug Description

On Windows, shutil.copytree fails to detect recursive cycles when processing Directory Junctions.

In Lib/shutil.py, the code explicitly forces traversal into Windows Junctions by treating them as standard directories (is_symlink is forced to False). However, there is no cycle detection mechanism (e.g., checking st_ino / nFileIndex) for these traversed directories.

If a Junction points to a parent directory, shutil.copytree enters an infinite recursion loop until it hits the OS path length limit, causing the process to crash with WinError 206 (Path too long) or WinError 1921.

Reproduction Steps

Run the following script on Windows:

import os
import shutil
import subprocess
import tempfile

def test_junction_crash():
    base_temp = tempfile.mkdtemp()
    src = os.path.join(base_temp, "Source")
    dst = os.path.join(base_temp, "Dest")
    junction = os.path.join(src, "Loop")
    
    try:
        os.makedirs(src)
        # Create a recursive Junction: Source/Loop -> Source
        subprocess.run(f'mklink /J "{junction}" "{src}"', shell=True, check=True)
        
        print("Starting copytree (expecting crash)...")
        shutil.copytree(src, dst)
        
    except Exception as e:
        print(e)

if __name__ == "__main__":
    test_junction_crash()

Actual Behavior

The script crashes with an unhandled shutil.Error wrapping a WinError 206, showing a deeply nested path.

Traceback:

Traceback (most recent call last):
  ...
  File "C:\...\lib\shutil.py", line 559, in copytree
    return _copytree(entries=entries, src=src, dst=dst, symlinks=symlinks,
  File "C:\...\lib\shutil.py", line 513, in _copytree
    raise Error(errors)
shutil.Error: [('...\\Source\\Infinite_Loop\\Infinite_Loop\\Infinite_Loop...', '...', "[WinError 206] The filename or extension is too long")]

Expected Behavior

shutil.copytree should detect that the Junction points to a directory that has already been visited (cycle detection) and either raise a specific RecursionError / FileExistsError or skip it.

System Details

OS: Windows 10/11
Python Version: Verified on main branch

CPython versions tested on:

CPython main branch

Operating systems tested on:

Windows

Linked PRs
  • gh-142156

Beitragsleitfaden

Beitragsleitfaden öffnen

Erste Schritte

  1. Lies das ganze Issue und danach den Beitragsleitfaden des Projekts.
  2. Schreib ins Issue, dass du es übernimmst — das erspart doppelte Arbeit.
  3. Forke das Repository und arbeite in einem Branch.
  4. Öffne einen Pull Request, der die Issue-Nummer nennt.

Rechercherichtung

Lies in Lib/shutil.py den Bereich um copytree und reproduziere den gemeldeten junction cycle unter Windows mit dem Skript im Issue. Erledigt ist die Aufgabe, wenn rekursive junctions behandelt werden, ohne das path-length limit zu erreichen, und das Verhalten dem ausgewählten Fehler- oder Überspringen-Ergebnis entspricht; gh-142156 ist bereits mit diesem Issue verknüpft.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
python
Bereich
operating-systems
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
25/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.