Skip to content

[Python] Reject overlapping local copies before deleting data - #41748

Open
JustinBinber wants to merge 2 commits into
apache:masterfrom
JustinBinber:codex/fix-local-copy-overlapping-paths
Open

JustinBinber wants to merge 2 commits into
apache:masterfrom
JustinBinber:codex/fix-local-copy-overlapping-paths

Conversation

@JustinBinber

@JustinBinber JustinBinber commented Nov 29, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #41747.

Copying a local source file/directory onto an ancestor directory removes the destination tree, including the source and unrelated siblings, before failing. Directory copies into their own output descendants can also recurse into the output.

Check source access dependencies and output containment before destination removal. The source check follows the argument's ancestors and link targets, including intermediate links; drive-relative Windows paths are anchored without discarding symlink-sensitive .. components. Output checks use the actual entry and target locations so a safe source/../neighbor copy remains valid. Existing self-copy/hard-link checks, ordinary overwrites, byte paths and per-pair error aggregation remain intact. Cyclic leaf and parent paths retain filesystem OSError details inside BeamIOError, including on Python versions where pathlib reports a symlink loop as RuntimeError. Ordinary file-to-file copies skip the ancestry traversal.

Validation on Windows / Python 3.11.7:

  • Collected 32 explicit offline regression cases. On unchanged master: 22 failed, 9 passed, 1 skipped. With the fix: 31 passed, 1 skipped (the skipped case specifically requires POSIX symlink/.. semantics).
  • Complete localfilesystem_test.py, filesystem_test.py and filesystems_test.py: 144 passed, 1 skipped, 50 subtests passed. Coverage includes the real public FileSystems.copy, source/sibling preservation, link aliases and chains, cycle termination, C:/D: drive-relative probes, valid sibling/overwrite copies and mixed-batch errors. Tripwires prevent unsafe recursive-copy/deletion entry in the descendant/link tests.
  • Configured pre-commit checks pass (YAPF 0.43.0 and hook Ruff 0.15.7); SDK Ruff 0.17.6 passes, Pyrefly 1.5.0 reports 0 errors, and git diff --check passes.

The source checkout reused released 2.78.0 generated protobuf bindings in the ignored API directory. A fresh SDK/protobuf build, live POSIX run and full runner/OS matrix were not run locally. This is a per-pair preflight check; concurrent namespace mutation, atomic copying and symlink graphs nested inside copied directory contents retain existing behavior.

@github-actions github-actions Bot added python io size/m M: 50-249 lines changed labels Nov 29, 2026

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

📋 Review Summary

This PR adds preflight ancestor/descendant overlap checks to LocalFileSystem.copy, preventing destructive shutil.rmtree calls when copying a source onto an ancestor directory and preventing recursive infinite copies when copying a directory into its own descendant. The implementation thoughtfully handles symlinks, relative paths (..), and Windows drive-relative paths.

🔍 General Feedback

  • There is one edge case when overwriting a broken symlink (os.path.exists(destination) is False, while os.path.lexists(destination) is True) when source is a file, where destination_path.resolve() can raise RuntimeError on a symlink cycle before reaching shutil.copy2, or where broken symlinks at destination are not removed prior to copying.
  • Otherwise, the ancestor/descendant resolution logic and test coverage are thorough and well-structured.

💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.

'Cannot copy between ancestor and descendant paths: %r, %r' %
(source, destination))
if destination_exists:
if destination_is_directory:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium: When source is a directory (source_is_directory is True) and destination is a broken symlink with a symlink cycle (so destination_is_directory is False and destination_exists is False), calling destination_path.resolve() on line 284 will raise RuntimeError: Symlink loop from ... on Python < 3.13 instead of OSError (note that test_copy_disjoint_link_cycle_fails_without_mutating_source currently catches RuntimeError only because _copy_path is wrapped in except Exception as e: in copy(), rather than turning it into an IOError).

Additionally, because destination_in_source only needs destination_path.resolve() when destination actually resolves to an existing directory/file (or you can catch RuntimeError or check destination_exists before resolving destination_path), guarding destination_path.resolve() with destination_exists avoids unnecessary resolution and RuntimeError on broken/cyclic destination symlinks (since destination_entry already resolves destination_path.parent).

Suggested change
if destination_is_directory:
for output in ((destination_entry, destination_path.resolve()) if destination_exists else (destination_entry,))

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in 6f5165d. Full destination-target resolution is now limited to existing targets. A narrowly scoped resolver converts pathlib's pre-3.13 symlink-loop RuntimeError to OSError(ELOOP), covering cyclic parents as well as cyclic leaf destinations.

The three focused cases (cyclic leaf, cyclic parent, and a broken alias into a missing source child) pass and verify entry/source preservation plus OSError details inside BeamIOError. The complete related suites give 144 passed, 1 platform skip, 50 subtests passed.

For the file-source case mentioned in the summary, a broken destination has neither directory flag set, so it does not enter the ancestry-resolution block; its existing copy2 behavior is unchanged.

@github-actions

Copy link
Copy Markdown
Contributor

Assigning reviewers:

R: @shunping for label python.
Backup reviewer: jrmccluskey (if the main reviewer is unresponsive, you can escalate to them by tagging their username)

Note: If you would like to opt out of this review, comment assign to next reviewer.

Available commands:

  • stop reviewer notifications - opt out of the automated review tooling
  • remind me after tests pass - tag the comment author after tests pass
  • waiting on author - shift the attention set back to the author (any comment or push by the author will return the attention set to the reviewers)

The PR bot will only process comments in the main thread (not review comments).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Local filesystem ancestor copies delete the source and unrelated files

1 participant