Skip to content

Report "finalizing" in Py_FatalError() during Py_Finalize() - #163059

Merged
vstinner merged 1 commit into
python:mainfrom
vstinner:fatal_error_finalizing
Dec 3, 2026
Merged

vstinner merged 1 commit into
python:mainfrom
vstinner:fatal_error_finalizing

Conversation

@vstinner

@vstinner vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member

If Py_FatalError() is called in an early state of Py_Finalize(), report the state "finalizing" from the interpreter state.

If Py_FatalError() is called in an early state of Py_Finalize(),
report the state "finalizing" from the interpreter state.
@vstinner

vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

I wrote this change to try to get more info about this bug on RISC-V 64-bit Ubuntu Clang 3.x buildbot: https://buildbot.python.org/#/builders/2255/builds/886

Total duration: 52 min 18 sec
Total tests: run=54,022 skipped=3,313
Total test files: run=560/564 failed=1 skipped=81 resource_denied=5 rerun=1
Result: FAILURE
Debug memory block at address p=0x3fd4c00220: API 'o'
    48 bytes originally requested
    The 7 pad bytes at p-7 are FORBIDDENBYTE, as expected.
    The 8 pad bytes at tail=0x3fd4c00250 are not all FORBIDDENBYTE (0xfd):
        at tail+0: 0x64 *** OUCH
        at tail+1: 0x00 *** OUCH
        at tail+2: 0xfd
        at tail+3: 0xfd
        at tail+4: 0xfd
        at tail+5: 0xfd
        at tail+6: 0xfd
        at tail+7: 0xfd
    Data at p: 00 00 00 00 00 00 00 00 ... 63 6f 6d 70 6c 65 74 65

Enable tracemalloc to get the memory block allocation traceback

Fatal Python error: _PyMem_DebugRawFree: bad trailing pad byte
Python runtime state: initialized

Current thread 0x0000003fdda2b020 [python] (most recent call first):

Extension modules: _testcapi (total: 1)
Aborted (core dumped)

The error says Python runtime state: initialized. I'm curious to know if the bug occurred during Python finalization since the Current thread traceback is empty.

@vstinner

vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Example to test this change:

import atexit
import _testcapi
def bug():
    _testcapi.fatal_error(b'xyz')
atexit.register(bug)

Before, it logs Python runtime state: initialized.

With this change, it logs Python runtime state: finalizing. So we better know the current Python "runtime state".

@vstinner
vstinner merged commit 79e357a into python:main Dec 3, 2026
60 checks passed
@vstinner
vstinner deleted the fatal_error_finalizing branch December 3, 2026 09:56
@vstinner vstinner added the needs backport to 3.15 bugs and security fixes label Dec 3, 2026
@miss-islington-app

Copy link
Copy Markdown

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

@bedevere-app

bedevere-app Bot commented Dec 3, 2026

Copy link
Copy Markdown

GH-163072 is a backport of this pull request to the 3.15 branch.

@bedevere-app bedevere-app Bot removed the needs backport to 3.15 bugs and security fixes label Dec 3, 2026
@bedevere-app

bedevere-app Bot commented Dec 3, 2026

Copy link
Copy Markdown

GH-163073 is a backport of this pull request to the 3.14 branch.

@vstinner

vstinner commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Fatal Python error: _PyMem_DebugRawFree: bad trailing pad byte

I created #163068 to track this bug.

vstinner added a commit that referenced this pull request Dec 3, 2026
…H-163059) (#163072)

Report "finalizing" in Py_FatalError() during Py_Finalize() (GH-163059)

If Py_FatalError() is called in an early state of Py_Finalize(),
report the state "finalizing" from the interpreter state.
(cherry picked from commit 79e357a)

Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Dec 3, 2026
…163059) (#163073)

Report "finalizing" in Py_FatalError() during Py_Finalize() (#163059)

If Py_FatalError() is called in an early state of Py_Finalize(),
report the state "finalizing" from the interpreter state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant