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

GH-99205: Mark new interpreters and threads as non-static #99268

Merged
merged 4 commits into from Nov 9, 2022

Conversation

brandtbucher
Copy link
Member

@brandtbucher brandtbucher commented Nov 9, 2022

@brandtbucher brandtbucher added type-bug An unexpected behavior, bug, or error interpreter-core Interpreter core (Objects, Python, Grammar, and Parser dirs) needs backport to 3.11 labels Nov 9, 2022
@brandtbucher brandtbucher self-assigned this Nov 9, 2022
Copy link
Contributor

@kumaraditya303 kumaraditya303 left a comment

LGTM

Copy link
Member

@ericsnowcurrently ericsnowcurrently left a comment

LGTM

I've pointed out two places where a comment would be appropriate. I'm approving the PR under the assumption you'll take care of those.

Python/pystate.c Show resolved Hide resolved
@@ -848,6 +851,7 @@ new_threadstate(PyInterpreterState *interp)
memcpy(tstate,
&initial._main_interpreter._initial_thread,
sizeof(*tstate));
tstate->_static = false;
Copy link
Member

@ericsnowcurrently ericsnowcurrently Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a note pointing to PyThreadState_INIT() and indicating that fields set there should be adjusted here as appropriate.

@@ -370,6 +371,7 @@ PyInterpreterState_New(void)
// Set to _PyInterpreterState_INIT.
memcpy(interp, &initial._main_interpreter,
sizeof(*interp));
interp->_static = false;
Copy link
Member

@ericsnowcurrently ericsnowcurrently Nov 9, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Likewise, a note pointing to PyInterpreterState_INIT().

@ericsnowcurrently
Copy link
Member

ericsnowcurrently commented Nov 9, 2022

Thanks for fixing this, BTW.

@brandtbucher brandtbucher requested a review from pablogsal Nov 9, 2022
@brandtbucher
Copy link
Member Author

brandtbucher commented Nov 9, 2022

@pablogsal, leak tests didn't catch this because we use the "raw" domain to allocate PyThreadState and PyInterpreterState.

@pablogsal
Copy link
Member

pablogsal commented Nov 9, 2022

Oh interesting, can you confirm that changing the domain to memory makes the test fail?

@brandtbucher
Copy link
Member Author

brandtbucher commented Nov 9, 2022

Oh interesting, can you confirm that changing the domain to memory makes the test fail?

You can't. :)

Fatal Python error: _PyMem_DebugCalloc: Python memory allocator called without holding the GIL
Python runtime state: preinitialized

See this comment:

cpython/Python/pystate.c

Lines 818 to 821 in 58ee5d8

// We don't need to allocate a thread state for the main interpreter
// (the common case), but doing it later for the other case revealed a
// reentrancy problem (deadlock). So for now we always allocate before
// taking the interpreters lock. See GH-96071.

@brandtbucher brandtbucher merged commit 283ab0e into python:main Nov 9, 2022
15 checks passed
@miss-islington
Copy link
Contributor

miss-islington commented Nov 9, 2022

Thanks @brandtbucher for the PR 🌮🎉.. I'm working now to backport this PR to: 3.11.
🐍🍒🤖

@bedevere-bot
Copy link

bedevere-bot commented Nov 9, 2022

GH-99301 is a backport of this pull request to the 3.11 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Nov 9, 2022
…onGH-99268)

(cherry picked from commit 283ab0e1c002f2d7459d581df6b4b8599e7d1a4d)

Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
miss-islington added a commit that referenced this pull request Nov 9, 2022
(cherry picked from commit 283ab0e)

Co-authored-by: Brandt Bucher <brandtbucher@microsoft.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core Interpreter core (Objects, Python, Grammar, and Parser dirs) type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants