Visitar URL original
bpo-43311: under building Python with --with-experimental-isolated-subinterpreters, PyInterpreterState_New use thread-specific data tstate before key create . by JunyiXie · Pull Request #24636 · python/cpython · GitHub
The Wayback Machine - https://web.archive.org/web/20211022002227/https://github.com/python/cpython/pull/24636
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

bpo-43311: under building Python with --with-experimental-isolated-subinterpreters, PyInterpreterState_New use thread-specific data tstate before key create . #24636

Closed
wants to merge 1 commit into from

Conversation

@JunyiXie
Copy link
Contributor

@JunyiXie JunyiXie commented Feb 24, 2021 •

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.

Note that the key has to be initialized before pthread_getspecific() or pthread_setspecific() can be used. The pthread_key_create() call could either be explicitly made in a module initialization routine, or it can be done implicitly by the first call to a module as in this example. Any attempt to use the key before it is initialized is a programming error, making the code below incorrect.https://linux.die.net/man/3/pthread_key_create

_PyRuntime.gilstate.autoTSSkey create in _PyGILState_Init. PyInterpreterState_New called before _PyGILState_Init.

use xcode to debug cpython
_PyRuntime.gilstate.autoTSSkey create
截屏2021-02-25 下午4 26 52

_PyRuntime.gilstate.autoTSSkey first use
截屏2021-02-25 下午4 26 18

to slove this problem, wo can create autoTSSkey after init runtime
截屏2021-02-25 下午4 37 34

https://bugs.python.org/issue43311

@the-knights-who-say-ni
Copy link

@the-knights-who-say-ni the-knights-who-say-ni commented Feb 24, 2021

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept this contribution by verifying everyone involved has signed the PSF contributor agreement (CLA).

CLA Missing

Our records indicate the following people have not signed the CLA:

@JunyiXie

For legal reasons we need all the people listed to sign the CLA before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

If you have recently signed the CLA, please wait at least one business day
before our records are updated.

You can check yourself to see if the CLA has been received.

Thanks again for the contribution, we look forward to reviewing it!

@JunyiXie
Copy link
Contributor Author

@JunyiXie JunyiXie commented Feb 25, 2021

@vstinner review

@JunyiXie JunyiXie changed the title bpo-43311: PyInterpreterState_New use thread tstate before set. bpo-43311: PyInterpreterState_New use thread tstate before create specific key. Feb 25, 2021
@JunyiXie JunyiXie changed the title bpo-43311: PyInterpreterState_New use thread tstate before create specific key. bpo-43311: PyInterpreterState_New use thread-specific data tstate before key create . Feb 25, 2021
…cific key.

create gilstate->autoTSSkey in pycore_init_runtime
@vstinner
Copy link
Member

@vstinner vstinner commented Mar 9, 2021

@vstinner review

I'm not a bot. Please ask more kindly :-)

@JunyiXie
Copy link
Contributor Author

@JunyiXie JunyiXie commented Mar 9, 2021

Can you review it, thank you!
sorry, I'm not good at English

@JunyiXie
Copy link
Contributor Author

@JunyiXie JunyiXie commented Mar 10, 2021

Thanks to STINNER Victor remind, this is an issue under building Python with --with-experimental-isolated-subinterpreters

@JunyiXie JunyiXie changed the title bpo-43311: PyInterpreterState_New use thread-specific data tstate before key create . bpo-43311: under building Python with --with-experimental-isolated-subinterpreters, PyInterpreterState_New use thread-specific data tstate before key create . Mar 10, 2021

struct _gilstate_runtime_state *gilstate = &runtime->gilstate;

if (PyThread_tss_create(&gilstate->autoTSSkey) != 0) {
Copy link
Member

@vstinner vstinner Mar 10, 2021

This wrong, since _PyGILState_Init() will create a new key.

I prefer to split _PyGILState_Init() in two parts: see my PR #24819 fix.

Copy link
Contributor Author

@JunyiXie JunyiXie Mar 11, 2021 •

This wrong, since _PyGILState_Init() will create a new key.
I think there is no problem.

after create PyThread_tss_create(&gilstate->autoTSSkey),
autoTSSkey will set flag key->_is_initialized = 1.

when PyThread_tss_create(&gilstate->autoTSSkey) again, just return 0. not create a new key.

    /* If the key has been created, function is silently skipped. */
    if (key->_is_initialized) {
        return 0;
    }
int
PyThread_tss_create(Py_tss_t *key)
{
    assert(key != NULL);
    /* If the key has been created, function is silently skipped. */
    if (key->_is_initialized) {
        return 0;
    }

    int fail = pthread_key_create(&(key->_key), NULL);
    if (fail) {
        return -1;
    }
    key->_is_initialized = 1;
    return 0;
}

Copy link
Member

@vstinner vstinner Mar 11, 2021 •

Oh. I didn't know that the Python _Py_tss_t structure has an _is_initialized member.

Well, I have another concern with your change. You are treating the gilstate as part of runtime. Well, technically, it's correct. But I'm trying to move the GIL into the interpreter somehow, so I prefer to only create the GIL in pycore_create_interpreter().

The gilstate API is still a weird thing which is currently incompatible with subinterpreters: https://bugs.python.org/issue15751

Maybe fixing https://bugs.python.org/issue40522 will allow remove gilstate and rewrite the GILState APIs using the regular functions to get the current interpreter and Python thread state. For now, I prefer to ignore the gilstate API, it's rarely used in Python ;-)

@vstinner
Copy link
Member

@vstinner vstinner commented Mar 10, 2021

Thanks to STINNER Victor remind, this is an issue under building Python with --with-experimental-isolated-subinterpreters

Ah ok, now it makes sense. I think that I already spotted this issue, but I was too lazy to reorganize _PyGILState_Init().

@vstinner
Copy link
Member

@vstinner vstinner commented Mar 10, 2021

I merged PR #24819 instead. Thanks for your PR anyway!

@vstinner vstinner closed this Mar 10, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants