shutil.copytree infinite recursion crash on Windows Directory Junctions (WinError 206)
まだ誰も着手していません。
- 主要言語
- Python
- スター
- 77.2k
- フォーク
- 35.9k
- PR マージ指標
- PR 指標を取得中
説明
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
コントリビューションガイド
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
調査の方向性
Lib/shutil.py の copytree 周辺を読み、issue のスクリプトを使って Windows で報告された junction cycle を再現してください。完了条件は、path-length limit に達することなく再帰的な junctions を処理し、選択されたエラーまたはスキップの結果と一致する動作になることです。gh-142156 はすでにこの issue にリンクされています。
索引モデルが issue の本文から書いたものです。
評価
- 技術スタック
- python
- 領域
- operating-systems
- issue の種類
- バグ
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 活発さ
- 停滞
- 明瞭さ
- おおむね明確
- 初心者へのやさしさ
- 25/100