Skip to content

gh-135427: Fix DeprecationWarning for os.fork when run in threads with -Werror #136796

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
wants to merge 24 commits into
base: main
Choose a base branch
from

Conversation

rani-pinchuk
Copy link

@rani-pinchuk rani-pinchuk commented Jul 19, 2025

This is a small fix as suggested in #135427 - using PyErr_WriteUnraisable() instead of PyErr_Clear().

The fix includes a test which checks that the DeprecationWarning is provided when using fork or forkpty within a thread, also when running with -Werror.

@encukou

@python-cla-bot
Copy link

python-cla-bot bot commented Jul 19, 2025

All commit authors signed the Contributor License Agreement.

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@bedevere-app
Copy link

bedevere-app bot commented Jul 19, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@rani-pinchuk
Copy link
Author

I have created a NEWS entry for this fix, but actually this change has indeed a little impact on Python users, so maybe skip news tag is more appropriate here.

@rani-pinchuk
Copy link
Author

Many tests are failing with a warning - as now the DeprecationWarning is no longer hidden (and as I unedrstand it, the tests run with the equivalent of -Werror).

Many of the failing tests do not fail where one would expect to have more than one thread running. For example, in test_uuid.testIssue8621.

When printing there the number of threads using threading.active_count() we get 1. However, when investigating further, the number of threads are actually 2 - just that one of the threads is not a python thread, and therefore not counted by threading.active_count().

Therefore, we should find a way to suppress the warning of running a fork within a thread in all the tests that fail. Petr suggests to consider using the sys.unraisablehook (see https://vstinner.github.io/sys-unraisablehook-python38.html) in all the places where the tests fail.

If anyone knows where this extra thread comes from, it will be appreciated if this is explained :-)

@gpshead
Copy link
Member

gpshead commented Jul 19, 2025

If anyone knows where this extra thread comes from, it will be appreciated if this is explained :-)

That is ironically exactly why we have this warning - threads in a process can come from everywhere including external libraries outside of our control.

I do think you're on the right track in terms of how to enable this to be something -Werror captures. As a potentially disruptive behavior change for some test suites, it isn't a bug fix - but it'd make sense in 3.15. Having a news entry is indeed appropriate.

using sys.unraisablehook in these parts of the test suites is an interesting idea.

@rhettinger rhettinger removed their request for review July 21, 2025 06:06
@rani-pinchuk rani-pinchuk requested a review from vsajip as a code owner July 21, 2025 15:50
@@ -1182,6 +1182,7 @@ async def runner():
@support.requires_fork()
class TestFork(unittest.IsolatedAsyncioTestCase):

@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-135427
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The whole class has fork tests, maybe add decorator to class itself?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. I am still learning... I tried to avoid putting decorator to the classes themselves, as I thought that it is more correct that if someone adds a new test, that developer should see the depracation warning.

But what you suggest will save many lines of code.

And also it seems that in some cases the test may run with forks or without them, so I am not sure that it is correct to decorate the functions themselves. For example, ProcessPoolExecutorTest extends ExecutorTest but also ThreadPoolExecutorTest extends ExecutorTest. So the tests in ExecutorTest must be decorated only if it is extended by ProcessPoolExecutorTest.

@@ -1182,6 +1182,7 @@ async def runner():
@support.requires_fork()
class TestFork(unittest.IsolatedAsyncioTestCase):

@warnings_helper.ignore_warnings(category=DeprecationWarning) # gh-135427
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still consider to create a dedicated ignore_fork_deprecated_warnings() decorator which will use

warnings.filterwarnings('ignore', 
                        message=".*fork.*may lead to deadlocks in the child.*", 
                        category=DeprecationWarning)

This will prevent that in the future all the ignore_warning on DeprecatedWarning will not affect other deprecated warnings.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense

@rani-pinchuk
Copy link
Author

@encukou, @gpshead - I am not sure, but maybe we should indeed create two PRs. The one that is here for 3.15. And in the second PR we will not raise exceptions for these deprecation warnings when using -Werror, but just show the warnings. This other PR maybe then can go in 3.14 - so at least the deprecation warnings are shown in that release when we have -Werror.

I think that the differences between the two PRs will only be the change in Modules/posixmodule.c but all the decorators to ignore the warnings should be the same. So in the other PR the change will be as shown in this earlier commit.

What do you think?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy