python / python/cpython

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

未关闭
#143,968 1 条评论 1 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

3.14 3.15 3.16 stdlib topic-pathlib type-bug
主要语言
Python
星标
77.2k
派生
35.9k
PR 合并指标
PR 指标待抓取

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

从 pathlib.Path.copy() 开始,具体检查 _copy_from,并将其处理方式与 shutil.copyfile 对 SpecialFileError 的处理行为进行比较。复现 FIFO 和 /dev/zero 的情况;完成的标准是两者都能及时失败,不会阻塞或消耗无界数据,并为该行为提供回归覆盖。

由索引模型根据 Issue 内容生成。

评估

技术栈
python
领域
operating-systems
Issue 类型
缺陷
难度
3/5
预计耗时
1-2 天
活跃度
停滞
描述清晰度
描述清楚
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。