GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — gh-101659: Isolate "obmalloc" State to Each Interpreter by ericsnowcurrently · Pull Request #101660 · python/cpython

Conversation

@ericsnowcurrently

@ericsnowcurrently ericsnowcurrently commented Feb 7, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member

This is strictly about moving the "obmalloc" runtime state from _PyRuntimeState to PyInterpreterState. Doing so improves isolation between interpreters, specifically most of the memory (incl. objects) allocated for each interpreter's use. This is important for a per-interpreter GIL, but such isolation is valuable even without it.

FWIW, a per-interpreter obmalloc is the proverbial canary-in-the-coalmine when it comes to the isolation of objects between interpreters. Any object that leaks (unintentionally) to another interpreter is highly likely to cause a crash (on debug builds at least). That's a useful thing to know, relative to interpreter isolation.

Benchmarking indicates the performance impact is negligible. (I'll re-run soon to double-check.)

  • Issue: Isolate the Default Object Allocator between Interpreters #101659

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

mahdithejedi reacted with thumbs up emoji
All reactions
  • 👍 1 reaction
ericsnowcurrently added 2 commits February 7, 2023 10:02
@ericsnowcurrently
Pass PyInterpreterState to pymalloc_*().
07a09d4
@ericsnowcurrently
Move the object arenas to the interpreter state.
ca75048
@ericsnowcurrently ericsnowcurrently added the skip news label Feb 7, 2023
@bedevere-bot bedevere-bot added the awaiting core review label Feb 7, 2023
@bedevere-bot bedevere-bot mentioned this pull request Feb 7, 2023
Isolate the Default Object Allocator between Interpreters #101659
Closed
ericsnowcurrently added 2 commits February 7, 2023 12:17
@ericsnowcurrently
Drop an errant #define.
4ee199b
@ericsnowcurrently
Leave dump_debug_stats in the global state.
2768fa4
erlend-aasland
erlend-aasland previously approved these changes Feb 7, 2023
View reviewed changes
@bedevere-bot bedevere-bot added awaiting merge and removed awaiting core review labels Feb 7, 2023
@ericsnowcurrently
Dynamically initialize obmalloc for subinterpreters.
bf9425f
@ericsnowcurrently

ericsnowcurrently commented Feb 9, 2023 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Member Author

FYI, the current CI failures are due to existing code. Basically, that code (in _PyImport_FixupExtensionObject()) tries to delete a [currently] global object that was created by a different interpreter than the "current" one. (There may be a few other similarly problematic global objects. I'll have to check.) As to why only some of the jobs are failing: the failure is a check by the debug allocator, which is only used in Py_DEBUG builds.

FWIW, the failure is exactly what we should expect to happen when per-interpreter data breaks isolation. I actually spent a while trying to figure out what I was doing wrong in my branch before realizing that everything was working as it should. 😄

UPDATE: I've opened gh-101758 to address this.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently ericsnowcurrently mentioned this pull request Feb 14, 2023
gh-101758: Add a Test For Single-Phase Init Modules in Multiple Interpreters #101920
Merged
@ericsnowcurrently

ericsnowcurrently commented Feb 21, 2023

Copy link
Copy Markdown
Member Author

Given what I've determined via gh-101758, I'll probably need to make non-isolated interpreters share the obmalloc state with the main interpreter.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently

ericsnowcurrently commented Feb 27, 2023

Copy link
Copy Markdown
Member Author

I'm tabling this until we've isolated all non-static objects.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ericsnowcurrently
ericsnowcurrently marked this pull request as draft February 27, 2023 16:33
ericsnowcurrently added 14 commits March 8, 2023 17:44
@ericsnowcurrently
Merge branch 'main' into per-interpreter-alloc
d5da34b
@ericsnowcurrently
Pass around struct _obmalloc_state* instead of PyInterpeterState*.
6c3111c
@ericsnowcurrently
Add _PyInterpreterConfig.use_main_obmalloc.
4dc087d
@ericsnowcurrently
Add a comment about why per-interpreter obmalloc requires multi-phase…
1ae33a0
… init extensions.
@ericsnowcurrently
Add a TODO comment.
5b54d63
@ericsnowcurrently
Optionally use the main interpreter's obmalloc state.
9f4f8f3
@ericsnowcurrently
Pass use_main_obmalloc to run_in_subinterp() in test_import.
aa10204
@ericsnowcurrently
_Py_GetAllocatedBlocks() -> _Py_GetGlobalAllocatedBlocks().
69d9a2d
@ericsnowcurrently
Errors from _Py_NewInterpreterFromConfig() are no longer fatal.
25378f8
@ericsnowcurrently
Chain the exceptions.
1c5b109
@ericsnowcurrently
Swap out the failed tstate.
f36426b
@ericsnowcurrently
Remaining static builtin types must be fixed.
54b9f09
@ericsnowcurrently
Add PyInterpreterState.sysdict_copy.
2358a42
@ericsnowcurrently
Set m_copy to None for sys and builtins.
b6502e1
@ericsnowcurrently
Merge branch 'atexit-c-callback' into per-interpreter-alloc
df77a64
@ericsnowcurrently
ericsnowcurrently requested a review from a team as a code owner April 6, 2023 00:09
ericsnowcurrently added a commit that referenced this pull request Apr 6, 2023
@ericsnowcurrently
gh-101659: Add _Py_AtExit() (gh-103298)
03089fd
The function is like Py_AtExit() but for a single interpreter.  This is a companion to the atexit module's register() function, taking a C callback instead of a Python one.

We also update the _xxinterpchannels module to use _Py_AtExit(), which is the motivating case.  (This is inspired by pain points felt while working on gh-101660.)
@ericsnowcurrently
Merge branch 'main' into per-interpreter-alloc
22758a3
@ericsnowcurrently ericsnowcurrently added the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 6, 2023
@bedevere-bot

bedevere-bot commented Apr 6, 2023

Copy link
Copy Markdown

🤖 New build scheduled with the buildbot fleet by @ericsnowcurrently for commit 22758a3 🤖

If you want to schedule another build, you need to add the 🔨 test-with-refleak-buildbots label again.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@bedevere-bot bedevere-bot removed the 🔨 test-with-refleak-buildbots Test PR w/ refleak buildbots; report in status section label Apr 6, 2023
gaogaotiantian pushed a commit to gaogaotiantian/cpython that referenced this pull request Apr 8, 2023
@ericsnowcurrently @gaogaotiantian
pythongh-102660: Fix is_core_module() (pythongh-103257)
f7e15af
In pythongh-102744 we added is_core_module() (in Python/import.c), which relies on get_core_module_dict() (also added in that PR).  The problem is that_PyImport_FixupBuiltin(), which ultimately calls is_core_module(), is called on the builtins module before interp->builtins_copyis set.  Consequently, the builtins module isn't considered a "core" module while it is getting "fixed up" and its module def m_copy erroneously gets set.  Under isolated interpreters this causes problems since sys and builtins are allowed even though they are still single-phase init modules.  (This was discovered while working on pythongh-101660.)

The solution is to stop relying on get_core_module_dict() in is_core_module().
gaogaotiantian pushed a commit to gaogaotiantian/cpython that referenced this pull request Apr 8, 2023
@ericsnowcurrently @gaogaotiantian
pythongh-101659: Use the Raw Allocator in the _xxinterpchannels Module (
6c04775
pythongh-103287)

Using the raw allocator for any of the global state makes sense, especially as we move to a per-interpreter obmalloc state (pythongh-101660).
gaogaotiantian pushed a commit to gaogaotiantian/cpython that referenced this pull request Apr 8, 2023
@ericsnowcurrently @gaogaotiantian
pythongh-101659: Add _Py_AtExit() (pythongh-103298)
ebae223
The function is like Py_AtExit() but for a single interpreter.  This is a companion to the atexit module's register() function, taking a C callback instead of a Python one.

We also update the _xxinterpchannels module to use _Py_AtExit(), which is the motivating case.  (This is inspired by pain points felt while working on pythongh-101660.)
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-102660: Handle m_copy Specially for the sys and builtins Mod…
d2aeb2a
…ules (pythongh-102661)

It doesn't make sense to use multi-phase init for these modules. Using a per-interpreter "m_copy" (instead of PyModuleDef.m_base.m_copy) makes this work okay. (This came up while working on pythongh-101660.)

Note that we might instead end up disallowing re-load for sys/builtins since they are so special.

python#102660
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-98608: Fix Failure-handling in new_interpreter() (pythongh-1…
45f7d84
…02658)

The error-handling code in new_interpreter() has been broken for a while.  We hadn't noticed because those code mostly doesn't fail.  (I noticed while working on pythongh-101660.)  The problem is that we try to clear/delete the newly-created thread/interpreter using itself, which just failed.  The solution is to switch back to the calling thread state first.

python#98608
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-94673: Isolate the _io module to Each Interpreter (pythongh-…
9d7a022
…102663)

Aside from sys and builtins, _io is the only core builtin module that hasn't been ported to multi-phase init.  We may do so later (e.g. pythongh-101948), but in the meantime we must at least take care of the module's static types properly.  (This came up while working on pythongh-101660.)

python#94673
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-100227: Make the Global Interned Dict Safe for Isolated Inte…
6cffe84
…rpreters (pythongh-102925)

This is effectively two changes.  The first (the bulk of the change) is where we add _Py_AddToGlobalDict() (and _PyRuntime.cached_objects.main_tstate, etc.).  The second (much smaller) change is where we update PyUnicode_InternInPlace() to use _Py_AddToGlobalDict() instead of calling PyDict_SetDefault() directly.

Basically, _Py_AddToGlobalDict() is a wrapper around PyDict_SetDefault() that should be used whenever we need to add a value to a runtime-global dict object (in the few cases where we are leaving the container global rather than moving it to PyInterpreterState, e.g. the interned strings dict).  _Py_AddToGlobalDict() does all the necessary work to make sure the target global dict is shared safely between isolated interpreters.  This is especially important as we move the obmalloc state to each interpreter (pythongh-101660), as well as, potentially, the GIL (PEP 684).

python#100227
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-100227: Make the Global PyModuleDef Cache Safe for Isolated …
9f95f32
…Interpreters (pythongh-103084)

Sharing mutable (or non-immortal) objects between interpreters is generally not safe.  We can work around that but not easily. 
 There are two restrictions that are critical for objects that break interpreter isolation.

The first is that the object's state be guarded by a global lock.  For now the GIL meets this requirement, but a granular global lock is needed once we have a per-interpreter GIL.

The second restriction is that the object (and, for a container, its items) be deallocated/resized only when the interpreter in which it was allocated is the current one.  This is because every interpreter has (or will have, see pythongh-101660) its own object allocator.  Deallocating an object with a different allocator can cause crashes.

The dict for the cache of module defs is completely internal, which simplifies what we have to do to meet those requirements.  To do so, we do the following:

* add a mechanism for re-using a temporary thread state tied to the main interpreter in an arbitrary thread
   * add _PyRuntime.imports.extensions.main_tstate` 
   * add _PyThreadState_InitDetached() and _PyThreadState_ClearDetached() (pystate.c)
   * add _PyThreadState_BindDetached() and _PyThreadState_UnbindDetached() (pystate.c)
* make sure the cache dict (_PyRuntime.imports.extensions.dict) and its items are all owned by the main interpreter)
* add a placeholder using for a granular global lock

Note that the cache is only used for legacy extension modules and not for multi-phase init modules.

python#100227
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-100227: Fix Cleanup of the Extensions Cache (pythongh-103150)
7c7f00c
Decref the key in the right interpreter in _extensions_cache_set().

This is a follow-up to pythongh-103084. I found the bug while working on pythongh-101660.
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-102660: Fix is_core_module() (pythongh-103257)
2380473
In pythongh-102744 we added is_core_module() (in Python/import.c), which relies on get_core_module_dict() (also added in that PR).  The problem is that_PyImport_FixupBuiltin(), which ultimately calls is_core_module(), is called on the builtins module before interp->builtins_copyis set.  Consequently, the builtins module isn't considered a "core" module while it is getting "fixed up" and its module def m_copy erroneously gets set.  Under isolated interpreters this causes problems since sys and builtins are allowed even though they are still single-phase init modules.  (This was discovered while working on pythongh-101660.)

The solution is to stop relying on get_core_module_dict() in is_core_module().
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-101659: Use the Raw Allocator in the _xxinterpchannels Module (
449da7d
pythongh-103287)

Using the raw allocator for any of the global state makes sense, especially as we move to a per-interpreter obmalloc state (pythongh-101660).
warsaw pushed a commit to warsaw/cpython that referenced this pull request Apr 11, 2023
@ericsnowcurrently @warsaw
pythongh-101659: Add _Py_AtExit() (pythongh-103298)
6366fa0
The function is like Py_AtExit() but for a single interpreter.  This is a companion to the atexit module's register() function, taking a C callback instead of a Python one.

We also update the _xxinterpchannels module to use _Py_AtExit(), which is the motivating case.  (This is inspired by pain points felt while working on pythongh-101660.)
@ericsnowcurrently
Merge branch 'main' into per-interpreter-alloc
0fd74a9
@ericsnowcurrently
ericsnowcurrently requested review from brettcannon, encukou, ncoghlan and warsaw as code owners April 24, 2023 19:54
@ericsnowcurrently
ericsnowcurrently merged commit df3173d into python:main Apr 24, 2023
@bedevere-bot bedevere-bot removed the awaiting review label Apr 24, 2023
@ericsnowcurrently
ericsnowcurrently deleted the per-interpreter-alloc branch April 24, 2023 23:24
@vstinner

vstinner commented Oct 30, 2023

Copy link
Copy Markdown
Member

This change broke PYTHONMALLOCSTATS: see issue #111499.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ksunden ksunden mentioned this pull request Nov 8, 2023
[Bug]: Segmentation fault when resizing on Python 3.12 and MacOS 14 matplotlib/matplotlib#27262
Closed
@UTsweetyfish UTsweetyfish mentioned this pull request Jul 10, 2024
【deepin_V23_Release】【一般】【立即】【集成测试】【系统】onboard报错“段错误” linuxdeepin/developer-center#9622
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@erlend-aasland erlend-aasland erlend-aasland left review comments
@brettcannon brettcannon Awaiting requested review from brettcannon brettcannon is a code owner
@encukou encukou Awaiting requested review from encukou
@ncoghlan ncoghlan Awaiting requested review from ncoghlan ncoghlan is a code owner
@warsaw warsaw Awaiting requested review from warsaw warsaw is a code owner

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

skip news

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@ericsnowcurrently @bedevere-bot @vstinner @erlend-aasland
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.