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-111924: Fix data races when swapping allocators by colesbury · Pull Request #130287 · python/cpython

Conversation

@colesbury

@colesbury colesbury commented Feb 18, 2025 •
edited by bedevere-app Bot
Loading

Uh oh!

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

Copy link
Copy Markdown
Contributor

CPython current temporarily changes PYMEM_DOMAIN_RAW to the default allocator during initialization and shutdown. The motivation is to ensure that core runtime structures are allocated and freed using the same allocator. However, modifying the current allocator changes global state and is not thread-safe even with the GIL. Other threads may be allocating or freeing objects use PYMEM_DOMAIN_RAW; they are not required to hold the GIL to call PyMem_RawMalloc/PyMem_RawFree.

This adds new internal-only functions like _PyMem_DefaultRawMalloc that aren't affected by calls to PyMem_SetAllocator(), so they're appropriate for Python runtime initialization and finalization. Use these calls in places where we previously swapped to the default raw allocator.

  • Issue: Avoid changing the PYMEM_DOMAIN_RAW allocator during initialization and finalization #111924

Sorry, something went wrong.

Uh oh!

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

All reactions
@colesbury
pythongh-111924: Fix data races when swapping allocators
e435f5d
CPython current temporarily changes `PYMEM_DOMAIN_RAW` to the default
allocator during initialization and shutdown. The motivation is to
ensure that core runtime structures are allocated and freed using the
same allocator. However, modifying the current allocator changes global
state and is not thread-safe even with the GIL. Other threads may be
allocating or freeing objects use PYMEM_DOMAIN_RAW; they are not
required to hold the GIL to call PyMem_RawMalloc/PyMem_RawFree.

This adds new internal-only functions like `_PyMem_DefaultRawMalloc`
that aren't affected by calls to `PyMem_SetAllocator()`, so they're
appropriate for Python runtime initialization and finalization. Use
these calls in places where we previously swapped to the default raw
allocator.
@colesbury colesbury added skip news topic-free-threading labels Feb 18, 2025
@bedevere-app bedevere-app Bot mentioned this pull request Feb 18, 2025
Avoid changing the PYMEM_DOMAIN_RAW allocator during initialization and finalization #111924
Closed
@colesbury
Reorder header
64bf28c
@colesbury
colesbury marked this pull request as ready for review February 18, 2025 23:37
@colesbury
colesbury requested review from ericsnowcurrently and kumaraditya303 as code owners February 18, 2025 23:37
@bedevere-app bedevere-app Bot added the awaiting core review label Feb 18, 2025
@colesbury
colesbury requested review from mpage and vstinner February 18, 2025 23:38
@ZeroIntensity ZeroIntensity mentioned this pull request Feb 19, 2025
Creating an ungodly amount of sub interpreters in a short amount of time causes memory debug assertions. #123134
Closed
mpage
mpage approved these changes Feb 19, 2025
View reviewed changes

@mpage mpage left a comment

Copy link
Copy Markdown
Contributor

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM

Sorry, something went wrong.

Uh oh!

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

All reactions
@bedevere-app bedevere-app Bot added awaiting merge and removed awaiting core review labels Feb 19, 2025
kumaraditya303
kumaraditya303 approved these changes Feb 20, 2025
View reviewed changes
vstinner
vstinner approved these changes Feb 20, 2025
View reviewed changes

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

There was a problem hiding this comment.

Choose a reason for hiding this comment

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

LGTM.

Sorry, something went wrong.

Uh oh!

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

All reactions
@vstinner

vstinner commented Feb 20, 2025

Copy link
Copy Markdown
Member

You may remove _PyMem_SetDefaultAllocator() since it's no longer used.

colesbury reacted with thumbs up emoji
All reactions
  • 👍 1 reaction

Sorry, something went wrong.

Uh oh!

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

@colesbury
Remove _PyMem_SetDefaultAllocator
1771d74
@colesbury
colesbury merged commit ca22147 into python:main Feb 20, 2025
@bedevere-app bedevere-app Bot removed the awaiting merge label Feb 20, 2025
@colesbury
colesbury deleted the gh-111924-default-allocator branch February 20, 2025 16:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@vstinner vstinner vstinner approved these changes
@mpage mpage mpage approved these changes
@kumaraditya303 kumaraditya303 kumaraditya303 approved these changes
@ericsnowcurrently ericsnowcurrently Awaiting requested review from ericsnowcurrently ericsnowcurrently is a code owner

Uh oh!

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

Assignees

No one assigned

Labels

skip news topic-free-threading

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

@colesbury @vstinner @mpage @kumaraditya303
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 01402520 — 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.