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.

classification
Title: bpo-43311: PyInterpreterState_New use thread-specific data tstate before key create .
Type: crash Stage: resolved
Components: Subinterpreters Versions: Python 3.10
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: JunyiXie, vstinner
Priority: normal Keywords: patch

Created on 2021-02-24 08:30 by JunyiXie, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 24636 closed JunyiXie, 2021-02-24 08:33
PR 24819 merged vstinner, 2021-03-10 18:14
Messages (6)
msg387610 - (view) Author: junyixie (JunyiXie) * Date: 2021-02-24 08:30
PyInterpreterState_New use thread tstate before set.


PyInterpreterState_New use tstate. but tstate is not set.
tstate will set after PyInterpreterState_New.

    PyInterpreterState *interp = PyInterpreterState_New();
    if (interp == NULL) {
        *tstate_p = NULL;
        return _PyStatus_OK();
    }

    PyThreadState *tstate = PyThreadState_New(interp);
    if (tstate == NULL) {
        PyInterpreterState_Delete(interp);
        *tstate_p = NULL;
        return _PyStatus_OK();
    }

    PyThreadState *save_tstate = PyThreadState_Swap(tstate);
msg387653 - (view) Author: junyixie (JunyiXie) * Date: 2021-02-25 08:40
PyInterpreterState_New call and use PyThreadState *tstate = _PyThreadState_GET();

_PyRuntime.gilstate.autoTSSkey has to be initialized before pthread_getspecific() or pthread_setspecific() can be used.

_PyRuntime.gilstate.autoTSSkey create in _PyGILState_Init. PyInterpreterState_New called before _PyGILState_Init.
msg388386 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-03-09 21:13
> PyInterpreterState_New call and use PyThreadState *tstate = _PyThreadState_GET();

It is safe to call _PyThreadState_GET() before _PyGILState_Init().

_PyThreadState_GET() calls _Py_atomic_load_relaxed(&_PyRuntime.gilstate.tstate_current), it doesn't use autoTSSkey. Or did I miss something?

Are you building Python with --with-experimental-isolated-subinterpreters?
msg388441 - (view) Author: junyixie (JunyiXie) * Date: 2021-03-10 16:23
Yes, this is an issue under building Python with --with-experimental-isolated-subinterpreters
msg388453 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-03-10 19:00
New changeset 87f649a409da9d99682e78a55a83fc43225a8729 by Victor Stinner in branch 'master':
bpo-43311: Create GIL autoTSSkey ealier (GH-24819)
https://github.com/python/cpython/commit/87f649a409da9d99682e78a55a83fc43225a8729
msg388454 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2021-03-10 19:01
Thanks for your bug report junyixie, it should now be fixed. See bpo-40522 for the follow up.
History
Date User Action Args
2022-04-11 14:59:41adminsetgithub: 87477
2022-02-23 16:41:14vstinnersetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2021-03-10 19:01:44vstinnersetmessages: + msg388454
2021-03-10 19:00:59vstinnersetmessages: + msg388453
2021-03-10 18:14:13vstinnersetpull_requests: + pull_request23585
2021-03-10 16:23:51JunyiXiesetmessages: + msg388441
2021-03-09 21:13:45vstinnersetmessages: + msg388386
2021-03-09 11:45:07vstinnersetnosy: + vstinner
2021-02-25 08:40:48JunyiXiesetmessages: + msg387653
2021-02-25 08:15:21JunyiXiesettitle: PyInterpreterState_New use thread tstate before set. -> bpo-43311: PyInterpreterState_New use thread-specific data tstate before key create .
2021-02-24 08:33:41JunyiXiesetkeywords: + patch
stage: patch review
pull_requests: + pull_request23420
2021-02-24 08:30:05JunyiXiecreate