python / python/cpython

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

オープン
#143,968 コメント 1 件 リアクション 1 件 担当者 0 名 GitHub で見る

まだ誰も着手していません。

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. リポジトリをフォークし、ブランチを切って変更します。
  4. 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 を短くまとめたダイジェスト。