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 — Move _Py_RefTotal to PyInterpreterState · Issue #102304 · python/cpython

Move _Py_RefTotal to PyInterpreterState #102304

New issue
Copy link
New issue
Copy link
Closed
Closed
Move _Py_RefTotal to PyInterpreterState#102304
Copy link
Assignees
ericsnowcurrently
Labels
3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItopic-subinterpreterstype-featureA feature request or enhancementA feature request or enhancement

Description

@ericsnowcurrently
ericsnowcurrently
opened on Feb 27, 2023
Issue body actions

(See gh-100227.)

_Py_RefTotal holds the current global total number of refcounts. It only exists if Py_REF_DEBUG is defined (implied by Py_DEBUG). It is exposed by sys.gettotalrefcount() and set by Py_INCREF(), Py_DECREF(), etc. and _Py_NewReference().

Modications to _Py_RefTotal are currently protected by the GIL so it should be moved to PyInterpreterState. For various aspects of compatibility, it makes sense to keep the _Py_RefTotal symbol around (and correct) and keep returning the global total from sys.gettotalrefcount().

Also, _Py_RefTotal is used by stable ABI extensions only where Py_REF_DEBUG is defined (unlikely) and only where built against 3.9 or earlier. Just in case, though, we must still keep the global variable around, so any solution here must respect that.

Linked PRs

  • gh-102304: Consolidate Direct Usage of _Py_RefTotal #102514
  • gh-102304: Move _Py_RefTotal to _PyRuntimeState #102543
  • gh-102304: Move the Total Refcount to PyInterpreterState #102545
  • gh-102304: Add a What's New Entry About _Py_RefTotal #102845
  • gh-102304: Fix Non-Debug Builds #102846
  • gh-102304: Fix 2 New Stable ABI Functions #104762
  • gh-102304: Fix Py_INCREF() stable ABI in debug mode #104763
  • gh-102304: Temporarily Bump Py_LIMITED_API for 2 New Functions #104766
  • [3.12] gh-102304: Fix 2 New Stable ABI Functions (gh-104762) #105123
  • gh-102304: doc: Add links to Stable ABI and Limited C API #105345
  • [3.12] gh-102304: doc: Add links to Stable ABI and Limited C API (GH-105345) #105347
  • gh-102304: Enhance Simple ABI doc #105351
  • [3.12] gh-102304: Fix Py_INCREF() stable ABI in debug mode (#104763) #105352
  • [3.12] gh-102304: doc: Add links to Stable ABI and Limited C API (#105345) #105371
  • gh-102304: Document Py_INCREF() change in What's New in Python 3.12 #105389
  • gh-102304: Debug mode no longer supports Limited API 3.9 #105391
  • gh-102304: Fix Py_INCREF() for limited C API 3.9 #105550
  • [3.12] gh-102304: Fix Py_INCREF() for limited C API 3.9 (GH-105550) #105551
  • gh-102304: Remove Py_INCREF() doc change #105552
  • [3.12] gh-102304: Fix Py_INCREF() for limited C API 3.9 #105553
  • gh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() #107193
  • [3.12] gh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() (GH-107193) #107199
  • [3.11] gh-102304: doc: Add links to Stable ABI and Limited C API (#105345) (#105371) #109901

Activity

Next
  1. ericsnowcurrently
    added
    type-featureA feature request or enhancementA feature request or enhancement
    interpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)
    topic-subinterpreters
    topic-C-API
    3.12only security fixesonly security fixes
    on Feb 27, 2023
  2. ezio-melotti
    added this to Subinterpreterson Feb 27, 2023
  3. ericsnowcurrently commented on Feb 27, 2023

    @ericsnowcurrently
    ericsnowcurrently
    on Feb 27, 2023
    MemberAuthor
    More actions

    From #100227 (comment):

    FYI, I have a plan for _Py_RefTotal that should preserve stable ABI compatibility without breaking interpreter isolation (and without relying on granular locks). Basically:

    • deprecate _Py_Reftotal but keep exporting the symbol (for stable ABI compatibility)
    • add _PyRuntimeState.object_state.last_legacy_reftotal
    • add _PyRuntimeState.object_state.reftotal
    • update everywhere to use the reftotal field (for simplicity, add #define _Py_RefTotal _PyRuntime.object_state.reftotal)

    We would update _Py_GetRefTotal() to something like this:

    Py_ssize_t
    _Py_GetRefTotal(void)
    {
        // _Py_GetLegacyRefTotal() returns the value of the actual `_Py_RefTotal` global.
        Py_ssize_t legacy = _Py_GetLegacyRefTotal();
        _PyRuntimeState.object_state.reftotal += legacy - _PyRuntimeState.object_state.last_legacy_reftotal;
        _PyRuntimeState.object_state.last_legacy_reftotal = legacy;
        return _PyRuntimeState.object_state.reftotal;
    }

    All this assumes that folks are not using _Py_RefTotal directly.


    For a per-interpreter GIL, I see two options:

    1. add a granular lock around modifying _PyRuntimeState.object_state.reftotal, which would hurt the performance of incref/decref, but only on debug builds
    2. switch to PyInterpreterState.object_state.reftotal and aggregate the values from all interpreters in _Py_GetRefTotal()
  4. ericsnowcurrently
    moved this to In Progress in Fancy CPython Boardon Feb 27, 2023
  5. ericsnowcurrently
    added this to Fancy CPython Boardon Feb 27, 2023
  6. ericsnowcurrently
    self-assigned this
    on Feb 27, 2023
  7. ericsnowcurrently commented on Feb 27, 2023

    @ericsnowcurrently
    ericsnowcurrently
    on Feb 27, 2023
    MemberAuthor
    More actions

    Alternately, from #100227 (comment):

    _Py_RefTotal is only defined if Py_REF_DEBUG is defined.
    All updates to _Py_RefTotal can be made atomic (which will have terrible performance with multiple parallel interpreters) to preserve the API.

  8. ericsnowcurrently
    moved this from In Progress to Todo in Fancy CPython Boardon Feb 28, 2023
  9. kumaraditya303 commented on Mar 3, 2023

    @kumaraditya303
    kumaraditya303
    on Mar 3, 2023
    Contributor
    More actions

    I vote to make it atomic instead of per interpreter. I use this while debugging refleaks and making it per interp will effectively make it impossible to use. This is only used in debug builds so I am not too much concerned about performance.

    Reacted by Eric Snow
  10. ericsnowcurrently
    moved this from Todo to In Progress in Fancy CPython Boardon Mar 7, 2023
  11. ericsnowcurrently
    added a commit that references this issue on Mar 8, 2023

    gh-102304: Consolidate Direct Usage of _Py_RefTotal (gh-102514)

    cbb0aa7
  12. 62 remaining items

  13. vstinner
    added a commit that references this issue on Jun 9, 2023

    gh-102304: Fix Py_INCREF() for limited C API 3.9 (#105550)

    7ba0fd9
  14. miss-islington
    added a commit that references this issue on Jun 9, 2023

    pythongh-102304: Fix Py_INCREF() for limited C API 3.9 (pythonGH-105550)

    a183db8
  15. vstinner
    added 5 commits that reference this issue on Jun 9, 2023

    pythongh-102304: Remove Py_INCREF() doc change

    f82a9c1

    gh-102304: Remove Py_INCREF() doc change (#105552)

    58e4b69

    pythongh-102304: Fix Py_INCREF() for limited C API 3.9 (python#105550)

    39e5ae9

    pythongh-102304: Remove Py_INCREF() doc change (python#105552)

    a9f0a25

    [3.12] gh-102304: Fix Py_INCREF() for limited C API 3.9 (#105553)

    d29e86b
  16. vstinner
    added 2 commits that reference this issue on Jul 24, 2023

    pythongh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS()

    93b8d6a

    gh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() (#107193)

    8ebc9fc
  17. miss-islington
    added a commit that references this issue on Jul 24, 2023

    pythongh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() (pythonGH-1…

    d7677f8
  18. vstinner
    added a commit that references this issue on Jul 24, 2023

    [3.12] gh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() (GH-107193) (

    3923639
  19. jtcave
    added a commit that references this issue on Jul 27, 2023

    pythongh-102304: Rename _Py_IncRefTotal_DO_NOT_USE_THIS() (python#107193

    616bbca
  20. AA-Turner
    added 2 commits that reference this issue on Sep 26, 2023

    [3.12] pythongh-102304: doc: Add links to Stable ABI and Limited C API (

    967be20

    [3.11] pythongh-102304: doc: Add links to Stable ABI and Limited C API (

    e4154d4
  21. erlend-aasland
    added a commit that references this issue on Sep 29, 2023

    [3.11] gh-102304: Add links to the Stable ABI and Limited C API docs (#…

    d81bcc2
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

  • @ericsnowcurrently
    ericsnowcurrently

Labels

3.12only security fixesonly security fixesinterpreter-core(Objects, Python, Grammar, and Parser dirs)(Objects, Python, Grammar, and Parser dirs)topic-C-APItopic-subinterpreterstype-featureA feature request or enhancementA feature request or enhancement

Projects

  • Subinterpreters
    Status
    Done
    Show more project fields

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions

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.