Skip to content

-Werror doesn't error out on DeprecationWarning for os.fork() #135427

Open
@fperrin

Description

@fperrin

Bug report

Bug description:

Hi,

With the recent change that Python now throws a warning for programs that fork() when there are more than one threads. I was hoping to use -Werror to find and pinpoint all such occurrences in my software.

Using python3.13 as installed by uv (cpython-3.13.4-linux-x86_64-gnu). With the following test code:

import os, sys, threading, time

t = threading.Thread(target=time.sleep, args=(1,), daemon=True)
t.start()

assert threading.active_count() == 2

pid = os.fork()
if pid < 0:
    print(f"fork failed: {pid=}")
elif pid == 0:
    print(f"after fork, I am {os.getpid()=}; {threading.active_count()=}")
    sys.exit(0)
print(f"successful fork; I am {os.getpid()=}, child is {pid=}; {threading.active_count()=}")

pid, _ = os.forkpty()
if pid < 0:
    print(f"forkpty failed: {pid=}")
elif pid == 0:
    print(f"after forkpty, I am {os.getpid()=}; {threading.active_count()=}")
    sys.exit(0)
print(f"successful forkpty; I am {os.getpid()=}, child is {pid=}; {threading.active_count()=}")

Running with -Walways gives me the expected warnings:

$ python3 -Walways ex.py 
after fork, I am os.getpid()=275006; threading.active_count()=1
/home/fred/demo/ex.py:8: DeprecationWarning: This process (pid=275004) is multi-threaded, use of fork() may lead to deadlocks in the child.
  pid = os.fork()
successful fork; I am os.getpid()=275004, child is pid=275006; threading.active_count()=2
/home/fred/demo/ex.py:16: DeprecationWarning: This process (pid=275004) is multi-threaded, use of forkpty() may lead to deadlocks in the child.
  pid, _ = os.forkpty()
successful forkpty; I am os.getpid()=275004, child is pid=275007; threading.active_count()=2

However, running with -Werror, the code silently run (no exception raised, no warning printed):

$ python3 --version
Python 3.13.4
$ python3 -Werror ex.py 
successful fork; I am os.getpid()=275060, child is pid=275062; threading.active_count()=2
after fork, I am os.getpid()=275062; threading.active_count()=1
successful forkpty; I am os.getpid()=275060, child is pid=275063; threading.active_count()=2

CPython versions tested on:

3.13

Operating systems tested on:

Linux

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.13bugs and security fixes3.14bugs and security fixes3.15new features, bugs and security fixesextension-modulesC modules in the Modules dirtype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions