Skip to content

Some Runtime Finalization Constraints Are Not Enforced Nor Documented #120838

Open
@ericsnowcurrently

Description

@ericsnowcurrently

Bug report

Bug description:

There are a few technical constraints on calling Py_Finalize() currently aren't enforced, well-documented, or widely recognized. This is partly due to the relatively small number of embedders and the even smaller number of them that might violate the constraints through the use of threads or subinterpreters.

The constraints are:

  • should be called only in the main thread
  • should be called only with the main interpreter active
  • should be called only using the same thread state used for Py_Initialize()

(That last one is potentially less critical.)

There are several reasons these matter:

  • the main thread of the main interpreter has a special role in the runtime's lifecycle
  • the main thread of the main interpreter has unique responsibilities that must be synchronized with finalization
  • conceptually, runtime lifecycle is an app-level function, and the app is typically driven through the "main" thread

The constraints should be documented, tested, and (possibly) enforced.

CPython versions tested on:

CPython main branch

Operating systems tested on:

No response

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    3.12only security fixes3.13bugs and security fixes3.14bugs and security fixesdocsDocumentation in the Doc dirinterpreter-core(Objects, Python, Grammar, and Parser dirs)topic-C-APItype-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions