Skip to content
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

Add _at_fork_reinit() method to locks #84270

Closed
vstinner opened this issue Mar 27, 2020 · 8 comments
Closed

Add _at_fork_reinit() method to locks #84270

vstinner opened this issue Mar 27, 2020 · 8 comments
Labels
3.9 only security fixes stdlib Python modules in the Lib dir

Comments

@vstinner
Copy link
Member

BPO 40089
Nosy @vstinner, @miss-islington
PRs
  • bpo-40089: Fix threading._after_fork() #19191
  • [3.7] bpo-40089: Fix threading._after_fork() (GH-19191) #19193
  • [3.8] bpo-40089: Fix threading._after_fork() (GH-19191) #19194
  • bpo-40089: Add _at_fork_reinit() method to locks #19195
  • [WIP] bpo-40091: Fix a crash in logging after fork #19196
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = <Date 2020-04-21.01:07:34.430>
    created_at = <Date 2020-03-27.15:51:37.958>
    labels = ['library', '3.9']
    title = 'Add _at_fork_reinit() method to locks'
    updated_at = <Date 2020-04-21.01:07:34.430>
    user = 'https://github.com/vstinner'

    bugs.python.org fields:

    activity = <Date 2020-04-21.01:07:34.430>
    actor = 'vstinner'
    assignee = 'none'
    closed = True
    closed_date = <Date 2020-04-21.01:07:34.430>
    closer = 'vstinner'
    components = ['Library (Lib)']
    creation = <Date 2020-03-27.15:51:37.958>
    creator = 'vstinner'
    dependencies = []
    files = []
    hgrepos = []
    issue_num = 40089
    keywords = ['patch']
    message_count = 8.0
    messages = ['365157', '365165', '365168', '365171', '365176', '365945', '365947', '365948']
    nosy_count = 3.0
    nosy_names = ['vstinner', 'miss-islington', 'Batuhan Taskaya']
    pr_nums = ['19191', '19193', '19194', '19195', '19196']
    priority = 'normal'
    resolution = 'fixed'
    stage = 'resolved'
    status = 'closed'
    superseder = None
    type = None
    url = 'https://bugs.python.org/issue40089'
    versions = ['Python 3.9']

    @vstinner
    Copy link
    Member Author

    Using a lock after fork() is unsafe and can crash.

    Example of a crash in logging after a fork on AIX:
    https://bugs.python.org/issue40068#msg365028

    This problem is explained in length in bpo-6721: "Locks in the standard library should be sanitized on fork".

    The threading module registers an "at fork" callback: Thread._reset_internal_locks() is called to reset self._started (threading.Event) and self._tstate_lock. The current implementation creates new Python lock objects and forgets about the old ones.

    I propose to add a new _at_fork_reinit() method to Python lock objects which reinitializes the native lock internally without having to create a new Python object.

    Currently, my implementation basically creates a new native lock object and forgets about the old new (don't call PyThread_free_lock()).

    Tomorrow, we can imagine a more efficient impementation using platform specific function to handle this case without having to forget about the old lock.

    @vstinner vstinner added 3.9 only security fixes stdlib Python modules in the Lib dir labels Mar 27, 2020
    @vstinner
    Copy link
    Member Author

    _at_fork() has a bug: it creates a _DummyThread instead of a _MainThread. Example:
    ---

    import os, _thread, threading, time
    
    def fork_in_thread():
        pid = os.fork()
        if pid:
            # parent
            os._exit(0)
    # child process
    print(f"child process: {threading.current_thread()=}")
    print(f"child process: {threading._main_thread=}")
    

    print(f"parent process: {threading.current_thread()=}")
    print(f"parent process: {threading._main_thread=}")

    _thread.start_new_thread(fork_in_thread, ())
    # block the main thread: fork_in_thread() exit the process
    time.sleep(60)

    Output:
    ---
    parent process: threading.current_thread()=<_MainThread(MainThread, started 139879200077632)>
    parent process: threading._main_thread=<_MainThread(MainThread, started 139879200077632)>
    child process: threading.current_thread()=<_DummyThread(Dummy-1, started daemon 139878980245248)>
    child process: threading._main_thread=<_DummyThread(Dummy-1, started daemon 139878980245248)>
    ---

    My PR 19191 fix the issue:
    ---
    parent process: threading.current_thread()=<_MainThread(MainThread, started 140583665170240)>
    parent process: threading._main_thread=<_MainThread(MainThread, started 140583665170240)>
    child process: threading.current_thread()=<_MainThread(MainThread, started 140583445075712)>
    child process: threading._main_thread=<_MainThread(MainThread, started 140583445075712)>
    ---

    @vstinner
    Copy link
    Member Author

    New changeset d8ff44c by Victor Stinner in branch 'master':
    bpo-40089: Fix threading._after_fork() (GH-19191)
    d8ff44c

    @vstinner
    Copy link
    Member Author

    See also bpo-40091 "Crash in logging._after_at_fork_child_reinit_locks()" that I just created.

    @vstinner
    Copy link
    Member Author

    See also bpo-40092: "Crash in _PyThreadState_DeleteExcept() at fork in the process child".

    @vstinner
    Copy link
    Member Author

    vstinner commented Apr 7, 2020

    New changeset 87255be by Victor Stinner in branch 'master':
    bpo-40089: Add _at_fork_reinit() method to locks (GH-19195)
    87255be

    @vstinner
    Copy link
    Member Author

    vstinner commented Apr 7, 2020

    New changeset 6318e45 by Miss Islington (bot) in branch '3.8':
    bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19194)
    6318e45

    @vstinner
    Copy link
    Member Author

    vstinner commented Apr 7, 2020

    New changeset a514ccb by Miss Islington (bot) in branch '3.7':
    bpo-40089: Fix threading._after_fork() (GH-19191) (GH-19193)
    a514ccb

    @ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
    Labels
    3.9 only security fixes stdlib Python modules in the Lib dir
    Projects
    None yet
    Development

    No branches or pull requests

    1 participant