Bug report
Checklist
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=Noneimportsysdelsys.modules['_socket']
importgcgc.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.
$ ./pythonPython3.13.0a0 (heads/main:d63972e289, Aug212023, 22:29:24) [GCC13.2.120230728 (RedHat13.2.1-1)] onlinux>>>import_socket>>>t=_socket.socket>>>t.__mro__[0] istTrue
See also:
Linked PRs
Bug report
Checklist
and am confident this bug has not been reported before
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.sockettype: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.CAPIcapsule (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.See also:
Linked PRs