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 — PYTHONMALLOCSTATS=1 fails with fatal error at Python exit · Issue #111499 · python/cpython

PYTHONMALLOCSTATS=1 fails with fatal error at Python exit #111499

New issue
Copy link
New issue
Copy link
Closed
Closed
PYTHONMALLOCSTATS=1 fails with fatal error at Python exit#111499
Copy link
Labels
3.12only security fixesonly security fixes3.13only security fixesonly security fixestopic-subinterpreterstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

@vstinner
vstinner
opened on Oct 30, 2023
Issue body actions

Py_FinalizeEx() calls _PyObject_DebugMallocStats() which calls indirectly _PyInterpreterState_GET(). Problem: at this point, there is no "interpreter" anymore, and so _PyInterpreterState_GET() fails with a fatal error.

It's a regression introduced by commit df3173d. Python 3.12 is also affected.

Reproducer:

$ PYTHONMALLOCSTATS=1 ./python -c pass
Small block threshold = 512, in 32 size classes.
(...)
Total                              =              262,144

Small block threshold = 512, in 32 size classes.
(...)
Total                              =              655,360

Fatal Python error: _PyInterpreterState_GET: the function must be called with the GIL held, after Python initialization and before Python finalization, but the GIL is released (the current Python thread state is NULL)
Python runtime state: finalizing (tstate=0x0000000000ae7550)

Abandon (core dumped)

cc @ericsnowcurrently

Linked PRs

  • gh-111499: Fix PYTHONMALLOCSTATS at Python exit #120021
  • [3.13] gh-111499: Fix PYTHONMALLOCSTATS at Python exit (GH-120021) #120022
  • [3.12] gh-111499: Fix PYTHONMALLOCSTATS at Python exit (#120021) #120023

Activity

  1. vstinner
    added
    3.12only security fixesonly security fixes
    3.13only security fixesonly security fixes
    on Oct 30, 2023
  2. vstinner commented on Dec 20, 2023

    @vstinner
    vstinner
    on Dec 20, 2023
    MemberAuthor
    More actions

    Apparently, nobody cares nor uses this feature which has no test. I just close the issue. I don't have the bandwidth to work on a fix.

  3. vstinner
    closed this as completedon Dec 20, 2023
  4. ShahriyarR commented on Dec 26, 2023

    @ShahriyarR
    ShahriyarR
    on Dec 26, 2023
    More actions

    Not sure if this is the related information or not, added here for future reference.
    I got the same error with Python 3.12 while doing some tests.
    Compiled Python as described here:
    https://devguide.python.org/

    Test:

    $ gdb python
    
    (gdb) break main
    (gdb) call PyLong_FromLong(22222)
    Fatal Python error: _PyInterpreterState_GET: the function must be called with the GIL held, after Python initialization and before Python finalization, but the GIL is released (the current Python thread state is NULL)
    Python runtime state: unknown
    

    Basically, the fix is calling Py_Initialize() before:

    (gdb) call Py_Initialize()
    

    The difference is, in Python 3.9 I got sissegv instead of this Fatal error.

  5. ericsnowcurrently
    added
    type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
    topic-subinterpreters
    on Feb 27, 2024
  6. devdanzin commented on Jun 4, 2024

    @devdanzin
    devdanzin
    on Jun 4, 2024
    Member
    More actions

    Apparently, nobody cares nor uses this feature which has no test. I just close the issue. I don't have the bandwidth to work on a fix.

    I'm trying to add a test as part of #120006, but having segfaults (Linux) or non-zero process exit codes (Windows) is kinda getting in the way. Could this issue be reopened so other devs can attempt to solve it?

    It seems the error has changed between versions:

    Python 3.12.3 (Linux), non-debug build:

    Program received signal SIGSEGV, Segmentation fault.
    0x00007ffff7ac632a in _PyInterpreterState_GET () at ./Include/internal/pycore_pystate.h:133
    133         return tstate->interp;

    Python 3.14.0a0 (heads/main:6e012ced6c) (Linux), non-debug build:

    Program received signal SIGSEGV, Segmentation fault.
    0x00005555556b0e3c in get_state () at Objects/obmalloc.c:1368
    1368        return interp->obmalloc;

    The fix might be as simple as (pardon my C):

    diff --git a/Objects/obmalloc.c b/Objects/obmalloc.c
    index 4fe195b631..04e24bd407 100644
    --- a/Objects/obmalloc.c
    +++ b/Objects/obmalloc.c
    @@ -3266,6 +3266,15 @@ py_mimalloc_print_stats(FILE *out)
     static void
     pymalloc_print_stats(FILE *out)
     {
    +    PyThreadState *tstate = _PyThreadState_GET();
    +    if (tstate == NULL || tstate->interp == NULL) {
    +        return;
    +    }
    +
    +    PyInterpreterState *interp = _PyInterpreterState_GET();
    +    if (interp == NULL || interp->obmalloc == NULL) {
    +        return;
    +    }
         OMState *state = get_state();
    
         uint i;
  7. vstinner
    added 2 commits that reference this issue on Jun 4, 2024

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit

    a27a47e

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit

    49750ac
  8. vstinner commented on Jun 4, 2024

    @vstinner
    vstinner
    on Jun 4, 2024
    MemberAuthor
    More actions

    I wrote PR gh-120021 to fix the issue.

  9. vstinner
    added a commit that references this issue on Jun 4, 2024

    gh-111499: Fix PYTHONMALLOCSTATS at Python exit (#120021)

    5a1205b
  10. miss-islington
    added a commit that references this issue on Jun 4, 2024

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit (pythonGH-120021)

    3cc4671
  11. vstinner
    added 3 commits that reference this issue on Jun 4, 2024

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit (python#120021)

    8a09656

    [3.12] gh-111499: Fix PYTHONMALLOCSTATS at Python exit (#120021) (#12…

    e3e7607

    [3.13] gh-111499: Fix PYTHONMALLOCSTATS at Python exit (GH-120021) (#…

    6e67fd5
  12. barneygale
    added a commit that references this issue on Jun 5, 2024

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit (python#120021)

    2313a8f
  13. estyxx
    added a commit that references this issue on Jul 17, 2024

    pythongh-111499: Fix PYTHONMALLOCSTATS at Python exit (python#120021)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixesonly security fixes3.13only security fixesonly security fixestopic-subinterpreterstype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

    Projects

    No projects

      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.