This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Crash in ModuleType subclass with __slots__
Type: crash Stage: resolved
Components: Interpreter Core Versions: Python 3.11
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Mark.Shannon Nosy List: Dennis Sweeney, Mark.Shannon, pablogsal, ronaldoussoren
Priority: release blocker Keywords: 3.11regression, patch

Created on 2022-03-01 21:25 by ronaldoussoren, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 31643 merged Mark.Shannon, 2022-03-02 15:06
Messages (10)
msg414294 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-01 21:25
The following crashes the interpreter in Python 3.11, and works fine in older versions:

# --- 
import types

class MyModule (types.ModuleType):
    __slots__ = (

    def __init__(self, name):

m = MyModule("name")
# -- end of file

The key in this is the ``__slots__`` definition: The script does not crash without ``__slots__``, or with a slots tuple with 1 item.

This is a reproducer based on code in PyObjC.
msg414296 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-01 21:31
This is with python 3.11 alpha 5 using the installer for macOS.
msg414300 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-01 22:13
The crash itself happens during garbage collection:

% python3.11 -Xdev                                                                                                                               (master)pyobjc-8
Fatal Python error: Segmentation fault

Current thread 0x0000000104ce0580 (most recent call first):
  <no Python frame>

And when using a debug build and -Xdev:

../Modules/gcmodule.c:2187: visit_validate: Assertion failed: PyObject_GC_Track() object is not valid
Enable tracemalloc to get the memory block allocation traceback

object address  : 0x101a5f690
object refcount : 1
object type     : 0x15503f230
object type name: MyModule
object repr     : <module '?'>

Fatal Python error: _PyObject_AssertFailed: _PyObject_AssertFailed
Python runtime state: initialized

Current thread 0x00000001019c0580 (most recent call first):
  File "/Users/ronald/Projects/Forks/cpython/build/", line 12 in <module>
zsh: abort      ./python.exe -Xdev
msg414304 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-01 22:16
The debug build seems to indicate that slot memory is not initiated properly, a stack trace (note the argument to visit_validate at frame #7):

  * frame #0: 0x000000019b60d9b8 libsystem_kernel.dylib`__pthread_kill + 8
    frame #1: 0x000000019b640eb0 libsystem_pthread.dylib`pthread_kill + 288
    frame #2: 0x000000019b57e314 libsystem_c.dylib`abort + 164
    frame #3: 0x000000010030c4f4 python.exe`fatal_error_exit(status=-1) at pylifecycle.c:2624:9
    frame #4: 0x000000010030c3cc python.exe`fatal_error(fd=2, header=1, prefix="_PyObject_AssertFailed", msg="_PyObject_AssertFailed", status=-1) at pylifecycle.c:2805:5
    frame #5: 0x000000010030be20 python.exe`_Py_FatalErrorFunc(func="_PyObject_AssertFailed", msg="_PyObject_AssertFailed") at pylifecycle.c:2821:5
    frame #6: 0x0000000100169fe0 python.exe`_PyObject_AssertFailed(obj=0x0000000100d63690, expr=0x0000000000000000, msg="PyObject_GC_Track() object is not valid", file="../Modules/gcmodule.c", line=2187, function="visit_validate") at object.c:2293:5
    frame #7: 0x0000000100351184 python.exe`visit_validate(op=0xcdcdcdcdcdcdcdcd, parent_raw=0x0000000100d63690) at gcmodule.c:2186:9
    frame #8: 0x000000010019845c python.exe`traverse_slots(type=0x0000000101813230, self=0x0000000100d63690, visit=(python.exe`visit_validate at gcmodule.c:2183), arg=0x0000000100d63690) at typeobject.c:1207:27
    frame #9: 0x0000000100197fe8 python.exe`subtype_traverse(self=0x0000000100d63690, visit=(python.exe`visit_validate at gcmodule.c:2183), arg=0x0000000100d63690) at typeobject.c:1228:23
    frame #10: 0x0000000100350fdc python.exe`PyObject_GC_Track(op_raw=0x0000000100d63690) at gcmodule.c:2212:11
msg414322 - (view) Author: Dennis Sweeney (Dennis Sweeney) * (Python committer) Date: 2022-03-02 03:06
Bisected to here:

c3f52b4d707a78eb342372a2be00f3eb846a05b9 is the first bad commit
commit c3f52b4d707a78eb342372a2be00f3eb846a05b9
Author: Mark Shannon <>
Date:   Wed Jun 23 10:00:43 2021 +0100

    bpo-44486: Make sure that modules always have a dictionary. (GH-26847)

    * Make sure that modules always have a dictionary.
msg414356 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-02 14:13
@Pablo: this is a possible release blocker, hard crash for code that works in earlier releases. 

That said, I have a workaround on my end and IMHO a fix could wait for the next alpha.
msg414357 - (view) Author: Pablo Galindo Salgado (pablogsal) * (Python committer) Date: 2022-03-02 14:15
Marking this as release blocker.
msg414360 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2022-03-02 14:32
Dennis, thanks for bisecting this.
msg414368 - (view) Author: Ronald Oussoren (ronaldoussoren) * (Python committer) Date: 2022-03-02 15:53
I don't feel qualified to review the PR, but can confirm that this PR fixes this issue as well as the crash in PyObjC that I extracted my reproducer from.
msg414418 - (view) Author: Mark Shannon (Mark.Shannon) * (Python committer) Date: 2022-03-03 10:38
New changeset 751c9ed801ad1189272ca10f0749bfc9d49b5038 by Mark Shannon in branch 'main':
bpo-46891: Fix creating a new instance of a module subclass with slots (GH-31643)
Date User Action Args
2022-04-11 14:59:56adminsetgithub: 91047
2022-03-03 10:48:59Mark.Shannonsetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2022-03-03 10:38:55Mark.Shannonsetmessages: + msg414418
2022-03-02 15:53:41ronaldoussorensetmessages: + msg414368
2022-03-02 15:06:15Mark.Shannonsetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request29763
2022-03-02 14:32:21Mark.Shannonsetmessages: + msg414360
2022-03-02 14:31:49Mark.Shannonsetassignee: Mark.Shannon
2022-03-02 14:15:32pablogsalsetpriority: normal -> release blocker

messages: + msg414357
2022-03-02 14:13:30ronaldoussorensetnosy: + pablogsal
messages: + msg414356
2022-03-02 03:08:10Dennis Sweeneysetnosy: + Mark.Shannon
2022-03-02 03:06:53Dennis Sweeneysetnosy: + Dennis Sweeney
messages: + msg414322
2022-03-01 22:16:50ronaldoussorensetmessages: + msg414304
2022-03-01 22:13:32ronaldoussorensetmessages: + msg414300
2022-03-01 21:31:49ronaldoussorensetmessages: + msg414296
2022-03-01 21:25:06ronaldoussorencreate