pathlib.Path.copy() hangs on FIFO pipes and loops infinitely on /dev/zero
Nobody has claimed this yet.
- Dominant language
- Python
- Stars
- 77.2k
- Forks
- 35.9k
- PR merge metrics
- PR metrics pending
Description
Bug Description
pathlib.Path.copy() (introduced recently) attempts to open and copy any file type that is not a directory or a symlink. This causes severe issues with special files:
- FIFOs (Named Pipes): Opening a FIFO for reading blocks indefinitely if there is no writer, causing the process to hang using CPU.
- Infinite Sources: Reading from character devices like
/dev/zeroresults in an infinite loop, potentially filling up the disk or freezing the application.
shutil.copyfile correctly handles this by raising SpecialFileError (or similar) when encountering these files. pathlib should behave similarly or at least safely.
Reproduction
import os
import pathlib
import signal
import time
fifo_name = "test_fifo_hang"
if os.path.exists(fifo_name):
os.remove(fifo_name)
os.mkfifo(fifo_name)
# This will hang indefinitely
print("Attempting to copy FIFO...")
p = pathlib.Path(fifo_name)
try:
p.copy("test_fifo_dest")
except Exception as e:
print(f"Caught: {e}")
else:
print("Copy finished (unexpected!)")
finally:
os.remove(fifo_name)
Expected Behavior
The operation should fail immediately with io.UnsupportedOperation or shutil.SameFileError / SpecialFileError instead of hanging or looping.
Proposed Solution
Check is_file() in pathlib.Path.copy (specifically _copy_from) to ensure we only copy regular files.
I have a PR ready for this.
CPython versions tested on:
3.14
Operating systems tested on:
macOS
Linked PRs
- gh-143966
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start in pathlib.Path.copy(), specifically _copy_from, and compare its handling with shutil.copyfile's SpecialFileError behavior. Reproduce the FIFO and /dev/zero cases; done means both fail promptly without blocking or consuming unbounded data, with regression coverage for the behavior.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- operating-systems
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 25/100