python / python/cpython

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

Open
#143,968 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

3.14 3.15 3.16 stdlib topic-pathlib type-bug
Dominant language
Python
Stars
77.2k
Forks
35.9k
PR merge metrics
PR metrics pending

Description

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in pathlib.Path.copy(), specifically _copy_from, and compare its handling with shutil.copyfile's SpecialFileError behavior. Reproduce the FIFO and /dev/zero cases; done means both fail promptly without blocking or consuming unbounded data, with regression coverage for the behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
operating-systems
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.