-
-
Notifications
You must be signed in to change notification settings - Fork 32.4k
Closed
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Description
Bug report
Checklist
- I am confident this is a bug in CPython, not a bug in a third-party projectI have searched the CPython issue tracker,
and am confident this bug has not been reported beforeTo pick up a draggable item, press the space bar. While dragging, use the arrow keys to move the item. Press space again to drop the item in its new position, or press escape to cancel.
CPython versions tested on:
CPython main branch
Operating systems tested on:
Linux
Output from running 'python -VV' on the command line:
Python 3.13.0a0 (heads/main:d63972e289, Aug 21 2023, 22:29:24) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)]
A clear and concise description of the bug:
The following script will never delete _socket.socket
type:
import _socket
# uncomment to workaround the bug
#_socket.CAPI = None
_socket = None
import sys
del sys.modules['_socket']
import gc
gc.collect()
print("exit")
Example on a Python debug build:
$ ./python -X showrefcount script.py
exit
[408 refs, 260 blocks]
The GC cannot visit the strong reference to the type stored in _socket.CAPI
capsule (C API). In Python, creating a heap type creates a reference cycle. For example, a type MRO contains the type. Methods also contain a strong reference to their type.
$ ./python
Python 3.13.0a0 (heads/main:d63972e289, Aug 21 2023, 22:29:24) [GCC 13.2.1 20230728 (Red Hat 13.2.1-1)] on linux
>>> import _socket
>>> t=_socket.socket
>>> t.__mro__[0] is t
True
See also:
- https://pythondev.readthedocs.io/garbage_collector.html#reference-cycles
- https://vstinner.github.io/debug-python-refleak.html
- https://vstinner.github.io/subinterpreter-leaks.html
Linked PRs
CharlieZhao95 and Eclips4
Metadata
Metadata
Assignees
Labels
type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
Projects
Milestone
Relationships
Development
Select code repository
Activity
pythongh-108240: Fix a reference cycle in _socket.CAPI capsule
vstinner commentedon Aug 21, 2023
I wrote PR #108241 to fix the issue for the _socket extension: avoid strong references in the capsule object.
vstinner commentedon Aug 21, 2023
_curses
extension is affected by the same bug:_curses._C_API
capsule stores a strong reference to_curses.window
heap type.Extensions not affected by the issue, using borrowed references:
_datetime.datetime_CAPI
stores borrowed references to_datetime
heap typespyexpat.expat_CAPI
does not store typesunicodedata._ucnhash_CAPI
does not store types_testcapi
capsules don't store typesvstinner commentedon Aug 21, 2023
By the way, commit 46366ca may have introduced a leak: see PR #107577.
vstinner commentedon Aug 21, 2023
I closed issue #107789 as a duplicate of this issue.
vstinner commentedon Aug 21, 2023
Oh, @Eclips4 found that the
_decimal
extension is also affected: #107577 (comment)Eclips4 commentedon Aug 21, 2023
Probably related: #107994
vstinner commentedon Aug 21, 2023
Oh right, the
_decimal
extension has no capsule, so the refleak is unrelated to this issue.vstinner commentedon Aug 21, 2023
cc @erlend-aasland @corona10 @pablogsal: yet another complex GC issue involving extension multi-phase initialization and the garbage collector.
vstinner commentedon Aug 21, 2023
The
_curses
extension was not ported to multi-phase init API (PEP 489) yet. It still uses static types.Eclips4 commentedon Aug 21, 2023
You mean leak in the
_curses
is a known issue?vstinner commentedon Aug 21, 2023
Yes. It should be ported to multi-phase init and its static types should be converted to heap types.
25 remaining items