Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
fperrin opened this issue Jun 12, 2025 · 2 comments
Open

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

fperrin opened this issue Jun 12, 2025 · 2 comments
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error

Comments

@fperrin
Copy link

fperrin commented Jun 12, 2025

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

@fperrin fperrin added the type-bug An unexpected behavior, bug, or error label Jun 12, 2025
@ZeroIntensity
Copy link
Member

Well, -Werror does error out on DeprecationWarning:

$ python3 -Werror -c "import warnings; warnings.warn('test', DeprecationWarning)"
Traceback (most recent call last):
  File "<string>", line 1, in <module>
    import warnings; warnings.warn('test', DeprecationWarning)
                     ~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^
DeprecationWarning: test

But the problem here is that os.fork specifically isn't raising the exception, because the function that warns is explicitly clearing exceptions. I can't figure out if this is intentional; the relevant PR is #100229. At the very least I would expect that to be an unraisable exception rather than letting it pass silently.

cc @gpshead

@ZeroIntensity ZeroIntensity added extension-modules C modules in the Modules dir 3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes labels Jun 12, 2025
@fperrin fperrin changed the title -Werror doesn't error out on DeprecationWarning -Werror doesn't error out on DeprecationWarning for os.fork() Jun 12, 2025
@fperrin
Copy link
Author

fperrin commented Jun 12, 2025

Yes should be a bit more accurate in the title.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.13 bugs and security fixes 3.14 bugs and security fixes 3.15 new features, bugs and security fixes extension-modules C modules in the Modules dir type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

2 participants