python / python/cpython

pathlib.Path.copy() hangs on FIFO pipes and loops infinitely on /dev/zero

Đang mở
#143,968 1 bình luận 1 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

3.14 3.15 3.16 stdlib topic-pathlib type-bug
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:

  1. FIFOs (Named Pipes): Opening a FIFO for reading blocks indefinitely if there is no writer, causing the process to hang using CPU.
  2. 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
  • gh-143966

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. 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.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. 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

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.