Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

C API:PyGC_Disable() not respected #116604

Closed
Labels
type-bugAn unexpected behavior, bug, or error
@wjakob

Description

@wjakob

Bug report

Bug description:

The Python C API provides the functionPyGC_Disable() to temporarily disable garbage collection. Calling it causesPyGC_Collect() to become a no-op. So far so good.

Unfortunately, CPython >= 3.12 no longer respects this flag beyond direct calls toPyGC_Collect(). Let's consider, for example, the operationPyErr_CheckSignals() atline 1773. This calls_Py_RunGC, which ignores the GCenabled flag. And thisindeed happens! In fact, I just spent quite a bit of time debugging an issue where the GC was supposed to be disabled for a brief moment, and yet it runs on Python 3.12. (Whether disabling the GC for a brief period of time is a good design pattern is another discussion. I would like to steer the discussion away from this and focus on documented API behavior.)

ThePyErr_CheckSignals() function is called from 135 locations in the CPython codebase including very common ones likePyObject_Str(), so making any fixes in callers of this API does not look feasible.

The flag-ignoring_Py_RunGC() function is only called by two places: besides the mentionedPyErr_CheckSignals(), there is also_Py_HandlePending() inPython/ceval_gil.c.

To restore the documented behavior, I see three options:

  • _Py_RunGC() could be modified to exit immediately if theenabled flag is set to zero.
  • The implementation of_Py_HandlePending() andPyErr_CheckSignals() could be modified to checkPyGC_IsEnabled() before calling_Py_RunGC().
  • Something is setting_PY_GC_SCHEDULED_BIT, and that causes the implementation to enter_Py_RunGC(). I'm not really sure about how that works, but perhaps this flag could be cleared inPyGC_Disable(), and Python could then avoid setting the flag.

(Tagging@pablogsal and@swtaarrs, who commited changes to the relevant code.)

CPython versions tested on:

3.12

Operating systems tested on:

Linux

Linked PRs

Metadata

Metadata

Assignees

No one assigned

    Labels

    type-bugAn unexpected behavior, bug, or error

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions


      [8]ページ先頭

      ©2009-2025 Movatter.jp