python / python/cpython

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

未关闭
#142,155 0 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

OS-windows stdlib type-bug
主要语言
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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

阅读 Lib/shutil.py 中 copytree 附近的代码,并使用 issue 中的脚本在 Windows 上复现报告的 junction cycle。完成标准是处理递归 junctions 时不会达到 path-length limit,且行为与所选的错误或跳过结果一致;gh-142156 已经链接到此 issue。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
4/5
预计耗时
3-5 天
活跃度
停滞
描述清晰度
基本清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。