Skip to content

raise a Warning when os.fork() is called and the process has multiple threads #100228

Description

@gpshead

os.fork() in a multi-threaded application is a likely source of deadlocks on many platforms. We should raise a warning when people call os.fork() from a process that we know has other threads running.

This came from discussion in https://discuss.python.org/t/switching-default-multiprocessing-context-to-spawn-on-posix-as-well/21868/, though I believe many of us have pondered doing it in the past.

Linked PRs

Activity

  1. added a commit that references this issue on Dec 29, 2022
  2. hauntsaninja commented on Jan 29, 2023

    @hauntsaninja
    Contributor

    Thanks for adding this!

  3. pitrou commented on Feb 11, 2023

    @pitrou
    Member

    Why a DeprecationWarning? This suggests that something is deprecated while it is not... ResourceWarning would sound more appropriate IMHO.

  4. gpshead commented on Feb 12, 2023

    @gpshead
    MemberAuthor

    Yeah, that question came up on the PR: https://github.com/python/cpython/pull/100229/files#r1048391023

    This is one of those cases where we worry about being too noisy if we use a warning that people see outside of their test environment (where deprecation warnings tend to be turned on, they're off by default otherwise).

    Lots of code in the world for better or worse "works fine" calling fork() from a threaded application because the software stacks that so many things build on top of don't care: glibc and it's malloc being the prime example. POSIX says it is never okay to do it, but because it worked on old glibc's and glibc malloc: that implementation gets maintained in such a super conservative manner that it does not as a general rule cause problems with threads+fork itself. Other things do, other libc implementations, many other malloc implementations (which often for much better performance specifically in the face of threading), other libraries code may use, etc.

    So we want it to be a soft nudge to start with. We can "increase" the verbosity to a different warning type in the future. Or if this proves too disruptive as is, reconsider. Unfortunately we don't have an existing Warning class for this kind of situation.

    A perhaps more appropriately named subclass (so that it is hidden by default) of DeprecationWarning could be created so that it at least doesn't sound like we're removing the ability for people to try and shoot themselves in the foot in the future?

    I'm expecting we'll hear from users about the noise level by the time we're in the beta phase.

  5. gpshead commented on Sep 12, 2023

    @gpshead
    MemberAuthor

    Relevant discussion about this warning in 3.12 release candidate end user testing: https://discuss.python.org/t/concerns-regarding-deprecation-of-fork-with-alive-threads/33555

  6. added a commit that references this issue on Sep 23, 2023
  7. added a commit that references this issue on Sep 23, 2023
  8. added a commit that references this issue on Sep 24, 2023
  9. added a commit that references this issue on Sep 28, 2023
  10. added a commit that references this issue on Sep 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixestype-featureA feature request or enhancement

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions