pathlib.Path.copy() hangs on FIFO pipes and loops infinitely on /dev/zero
Chưa có ai nhận issue này.
- Ngôn ngữ chính
- Python
- Star
- 77.2k
- Fork
- 35.9k
- Chỉ số merge pull request
- Chỉ số pull request đang chờ
Mô tả
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
Hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Hướng nghiên cứu
Bắt đầu tại pathlib.Path.copy(), cụ thể là _copy_from, và so sánh cách xử lý của nó với hành vi SpecialFileError của shutil.copyfile. Tái hiện các trường hợp FIFO và /dev/zero; được xem là hoàn tất khi cả hai đều thất bại nhanh chóng mà không bị chặn hoặc tiêu thụ dữ liệu không giới hạn, kèm theo kiểm thử hồi quy cho hành vi này.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Đánh giá
- Công nghệ
- python
- Lĩnh vực
- operating-systems
- Loại issue
- Lỗi
- Độ khó
- 3/5
- Thời gian dự kiến
- 1-2 ngày
- Mức độ hoạt động
- Đình trệ
- Độ rõ ràng
- Đặc tả rõ ràng
- Mức phù hợp với người mới
- 25/100