Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-119127: Fix _functools.Placeholder singleton #124601

Merged
merged 1 commit into from
Sep 26, 2024

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Sep 26, 2024

  • The module state now stores a strong reference to the Placeholder singleton.
  • Use a regular dealloc function.
  • Add Py_TPFLAGS_HAVE_GC flag and traverse function to help the GC to collect the type when _functools extension is unloaded.

* The module state now stores a strong reference to the Placeholder
  singleton.
* Use a regular dealloc function.
* Add Py_TPFLAGS_HAVE_GC flag and traverse function to help the GC to
  collect the type when _functools extension is unloaded.
@vstinner
Copy link
Member Author

cc @Eclips4 @serhiy-storchaka @dg-pb

Copy link
Member

@ZeroIntensity ZeroIntensity left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the right fix. I haven't manually confirmed that this fixes the refleak, but this is the proper way to implement singletons. (Originally, it was just an immortal object that never gets freed, and therefore causes a leak -- it gets worse as new subinterpreters are created and the module is reset.)

Thank you, Victor! Should this be targeting gh-124586 rather than gh-119127?

@vstinner
Copy link
Member Author

Thank you, Victor! Should this be targeting #124586 rather than #119127?

This PR is a follow-up of gh-119127.

@vstinner
Copy link
Member Author

This looks like the right fix. I haven't manually confirmed that this fixes the refleak

I checked manually that test_ast no longer leaks with this change.

@Eclips4
Copy link
Member

Eclips4 commented Sep 26, 2024

!buildbot refleaks

@bedevere-bot
Copy link

🤖 New build scheduled with the buildbot fleet by @Eclips4 for commit 37e994a 🤖

The command will test the builders whose names match following regular expression: refleaks

The builders matched are:

  • aarch64 CentOS9 Refleaks PR
  • AMD64 Fedora Rawhide Refleaks PR
  • AMD64 Fedora Rawhide NoGIL refleaks PR
  • aarch64 Fedora Rawhide Refleaks PR
  • AMD64 Windows11 Refleaks PR
  • PPC64LE Fedora Rawhide NoGIL refleaks PR
  • AMD64 Fedora Stable Refleaks PR
  • s390x RHEL8 Refleaks PR
  • aarch64 Fedora Stable Refleaks PR
  • PPC64LE Fedora Stable Refleaks PR
  • PPC64LE RHEL8 Refleaks PR
  • AMD64 RHEL8 Refleaks PR
  • AMD64 CentOS9 NoGIL Refleaks PR
  • AMD64 CentOS9 Refleaks PR
  • ARM64 MacOS M1 Refleaks NoGIL PR
  • s390x Fedora Refleaks PR
  • PPC64LE Fedora Rawhide Refleaks PR
  • aarch64 Fedora Rawhide NoGIL refleaks PR
  • s390x RHEL9 Refleaks PR
  • PPC64LE CentOS9 Refleaks PR
  • s390x Fedora Rawhide Refleaks PR
  • aarch64 RHEL8 Refleaks PR

Copy link
Member

@Eclips4 Eclips4 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. I've checked it locally and there's no leak in test_ast with this patch applied.
I run buildbots to make sure that everything is fine.

_Py_SetImmortal(placeholder);
PyObject_GC_UnTrack(self);
PyTypeObject *tp = Py_TYPE(self);
tp->tp_free((PyObject*)self);
Copy link
Member

@Eclips4 Eclips4 Sep 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
tp->tp_free((PyObject*)self);
tp->tp_free(self);

nit: there's no need to casting it to PyObject * since it's already a PyObject *

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@vstinner
Copy link
Member Author

I will wait for a few Refleaks buildbots to complete before merging this change.

@vstinner
Copy link
Member Author

Four Refleaks workers completed: test_datetime still leaks, but that's unrelated to this fix. test_ast no longer leaks on these workers. I consider that the fix works as expected and merge my PR.

@vstinner vstinner merged commit 257a20a into python:main Sep 26, 2024
43 of 50 checks passed
@vstinner vstinner deleted the placeholder branch September 26, 2024 14:50
@vstinner
Copy link
Member Author

Four Refleaks workers completed: test_datetime still leaks, but that's unrelated to this fix.

See #124606 for test_datetime leak.

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.

6 participants