Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-142155: Fix infinite recursion in shutil.copytree on Windows junctions#142156

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
ChuheLin wants to merge4 commits intopython:main
base:main
Choose a base branch
Loading
fromChuheLin:fix-shutil-recursion-loop

Conversation

@ChuheLin
Copy link

@ChuheLinChuheLin commentedDec 1, 2025
edited
Loading

Summary

This PR fixes a critical crash (WinError 206 / WinError 1921) inshutil.copytree on Windows when encountering directory junctions that point to a parent directory (recursive cycles).

The Problem

On Windows,shutil.copytree contains specific logic to force traversal into Directory Junctions by treating them as standard directories (is_symlink is forced toFalse).

However, unlikeos.walk orshutil.rmtree (after recent fixes),copytree lacked a cycle detection mechanism for these traversed junctions. This caused infinite recursion until the path exceeded the OS limit, resulting in an unhandledOSError (Crash) rather than a PythonRecursionError.

The Fix

I implemented cycle detection using file system identity (st_dev,st_ino), similar to how deepcopy handles recursion:

  1. Added a hidden_seen set parameter tocopytree and its helper_copytree.
  2. Before entering a directory, the code now checks if(st.st_dev, st.st_ino) is already in_seen.
  3. If a cycle is detected, it raises ashutil.Error("Infinite recursion detected") immediately, preventing the crash.

Verification

  1. Regression Test: Addedtest_copytree_recursive_junction toLib/test/test_shutil.py.
    • Status:PASSED on local Windows build.
  2. Manual Verification: Verified with a reproduction script that previously caused aWinError 206 crash.
    • Before fix: Script crashed after creating a path > 4000 chars.
    • After fix: Script correctly raisesshutil.Error: Infinite recursion detected.

@bedevere-app
Copy link

Most changes to Pythonrequire a NEWS entry. Add one using theblurb_it web app or theblurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply theskip news label instead.

@ChuheLinChuheLin changed the titlegh-142155: Fix infinite recursion in shutil.copytree on Windows junct…gh-142155: Fix infinite recursion in shutil.copytree on Windows junctionsDec 1, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@giampaologiampaoloAwaiting requested review from giampaologiampaolo is a code owner

Assignees

No one assigned

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

1 participant

@ChuheLin

[8]ページ先頭

©2009-2025 Movatter.jp