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/zero results 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
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:/dev/zeroresults in an infinite loop, potentially filling up the disk or freezing the application.shutil.copyfilecorrectly handles this by raisingSpecialFileError(or similar) when encountering these files.pathlibshould behave similarly or at least safely.Reproduction
Expected Behavior
The operation should fail immediately with
io.UnsupportedOperationorshutil.SameFileError/SpecialFileErrorinstead of hanging or looping.Proposed Solution
Check
is_file()inpathlib.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