This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: signalmodule.c: reuse runtime->main_thread
Type: Stage: resolved
Components: Interpreter Core Versions: Python 3.8
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: mcepl, vstinner
Priority: normal Keywords: patch

Created on 2019-05-24 08:08 by vstinner, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 13537 merged vstinner, 2019-05-24 08:12
PR 13538 merged vstinner, 2019-05-24 08:19
Messages (3)
msg343350 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-24 08:08
Currently, signalmodule.c tracks the "main thread" and the "main interpreter":

    main_thread = PyThread_get_thread_ident();
    main_interp = _PyInterpreterState_Get();

This information is already tracked in the global _PyRuntime variable which is updated at fork by _PyRuntimeState_ReInitThreads() (called by PyOS_AfterFork_Child).

Attached PR removes main_thread and main_interp from signalmodule.c, to reuse _PyRuntime. It should help to ensure that Python remains consistent (less risk of inconsistency if one variable is updated, but not the other).

By the way, _PyInterpreterState_DeleteExceptMain() is called before _PyRuntimeState_ReInitThreads(). _PyInterpreterState_DeleteExceptMain() acquires runtime->interpreters.mutex, whereas this lock is reset by _PyRuntimeState_ReInitThreads(). Maybe I introduced this bug recently when I refactored code, I don't know. But it sounds like a bug as well.

Note: PyOS_AfterFork_Child() doesn't update runtime->interpreters->main, but os.fork() raises a RuntimeError if it's not called from the main interpreter. So it's fine.
msg343379 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-24 11:43
New changeset d8613dc86f4c7acd3e2598095c466fe9dc0ad27c by Victor Stinner in branch 'master':
bpo-37031: Reuse _PyRuntime.main_thread in signalmodule.c (GH-13538)
msg343385 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2019-05-24 13:20
New changeset b49858b4b7b4c9d85ef6946ad020f83e4fa1caa7 by Victor Stinner in branch 'master':
bpo-37031: Fix PyOS_AfterFork_Child() (GH-13537)
Date User Action Args
2022-04-11 14:59:15adminsetgithub: 81212
2019-05-24 13:20:42vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2019-05-24 13:20:27vstinnersetmessages: + msg343385
2019-05-24 11:43:59vstinnersetmessages: + msg343379
2019-05-24 09:27:22mceplsetnosy: + mcepl
2019-05-24 08:19:55vstinnersetpull_requests: + pull_request13453
2019-05-24 08:12:44vstinnersetkeywords: + patch
stage: patch review
pull_requests: + pull_request13452
2019-05-24 08:08:57vstinnercreate