Skip to content

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

@tonghuaroot

Description

@tonghuaroot

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.14bugs and security fixes3.15new features, bugs and security fixesstdlibStandard Library Python modules in the Lib/ directorytopic-pathlibtype-bugAn unexpected behavior, bug, or error

    Projects

    Status

    No status

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions