Title: Segfault by calling `repr(SimpleNamespace)` with `typing.Union` attributes in threads on a free-threading build · Issue #135878 · python/cpython · GitHub
Open Graph Title: Segfault by calling `repr(SimpleNamespace)` with `typing.Union` attributes in threads on a free-threading build · Issue #135878 · python/cpython
X Title: Segfault by calling `repr(SimpleNamespace)` with `typing.Union` attributes in threads on a free-threading build · Issue #135878 · python/cpython
Description: Crash report What happened? It's possible to segfault or abort the interpreter of a free-threading build by calling repr(SimpleNamespace) in threads when the instance has typing.Union attributes. This seems very similar to #132713. The M...
Open Graph Description: Crash report What happened? It's possible to segfault or abort the interpreter of a free-threading build by calling repr(SimpleNamespace) in threads when the instance has typing.Union attributes. T...
X Description: Crash report What happened? It's possible to segfault or abort the interpreter of a free-threading build by calling repr(SimpleNamespace) in threads when the instance has typing.Union attribute...
Opengraph URL: https://github.com/python/cpython/issues/135878
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Segfault by calling `repr(SimpleNamespace)` with `typing.Union` attributes in threads on a free-threading build","articleBody":"# Crash report\n\n### What happened?\n\nIt's possible to segfault or abort the interpreter of a free-threading build by calling `repr(SimpleNamespace)` in threads when the instance has `typing.Union` attributes. This seems very similar to https://github.com/python/cpython/issues/132713.\n\nThe MRE will rarely hang and is non-deterministic, but is the best I was able to come up with.\n\nMRE:\n```python\nfrom functools import reduce\nfrom operator import or_\nfrom threading import Thread\n\nfrom types import SimpleNamespace\n\nall_types = []\nfor x in range(400):\n class Dummy: pass\n all_types.append(Dummy)\nbig_union = reduce(or_, all_types, int)\n\nfor x in range(500):\n s = SimpleNamespace()\n\n def stress_simplenamespace():\n for x in range(20):\n varying_union = big_union | tuple[int, ...] | list[str, ...] | dict[str, int]\n repr(s)\n attr_name = f\"t_attr_{x}\"\n setattr(s, attr_name, varying_union)\n repr(s)\n repr(s)\n repr(s)\n\n alive = [Thread(target=stress_simplenamespace) for x in range(8)]\n for t in alive:\n t.start()\n for t in alive:\n t.join()\n```\n\nSegfault backtrace 1:\n```shell\nThread 7 \"Thread-6 (stres\" received signal SIGSEGV, Segmentation fault.\n[Switching to Thread 0x7ffff541b640 (LWP 348490)]\n0x000055555567cdcd in setup_ga (args=\u003cunknown at remote 0x7ffff5c20640>, origin=\u003ctype at remote 0x555555b11d40>, alias=0x2276a0e0140) at Objects/genericaliasobject.c:837\n837 if (!PyTuple_Check(args)) {\n\n#0 0x000055555567cdcd in setup_ga (args=\u003cunknown at remote 0x7ffff5c20640>, origin=\u003ctype at remote 0x555555b11d40>, alias=0x2276a0e0140)\n at Objects/genericaliasobject.c:837\n#1 Py_GenericAlias (origin=\u003ctype at remote 0x555555b11d40>, args=\u003cunknown at remote 0x7ffff5c20640>) at Objects/genericaliasobject.c:1015\n#2 0x0000555555658dd5 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=9223372036854775809, args=0x7ffff541a428,\n callable=\u003cbuilt-in method __class_getitem__ of type object at remote 0x555555b11d40>, tstate=0x555555c3c6e0)\n at ./Include/internal/pycore_call.h:169\n#3 PyObject_CallOneArg (func=\u003cbuilt-in method __class_getitem__ of type object at remote 0x555555b11d40>,\n arg=arg@entry=\u003cunknown at remote 0x7ffff5c20640>) at Objects/call.c:395\n#4 0x0000555555634781 in PyObject_GetItem (o=\u003ctype at remote 0x555555b11d40>, key=\u003cunknown at remote 0x7ffff5c20640>) at Objects/abstract.c:193\n#5 0x00005555555e2f02 in _PyEval_EvalFrameDefault (tstate=\u003coptimized out>, frame=\u003coptimized out>, throwflag=\u003coptimized out>)\n at Python/generated_cases.c.h:62\n#6 0x00005555557dd13e in _PyEval_EvalFrame (throwflag=0, frame=\u003coptimized out>, tstate=0x555555c3c6e0) at ./Include/internal/pycore_ceval.h:119\n#7 _PyEval_Vector (tstate=0x555555c3c6e0, func=0x2275e37fd80, locals=0x0, args=0x7ffff541a8a8, argcount=\u003coptimized out>, kwnames=\u003coptimized out>)\n at Python/ceval.c:1975\n```\n\nSegfault backtrace 2:\n```shell\nThread 3 \"Thread-2 (stres\" received signal SIGSEGV, Segmentation fault.\n[Switching to Thread 0x7ffff742f640 (LWP 343364)]\nclear_freelist (dofree=\u003coptimized out>, is_finalization=\u003coptimized out>, freelist=\u003coptimized out>) at Objects/object.c:904\n904 dofree(ptr);\n\n#0 clear_freelist (dofree=\u003coptimized out>, is_finalization=\u003coptimized out>, freelist=\u003coptimized out>) at Objects/object.c:904\n#1 _PyObject_ClearFreeLists (freelists=0x555555c30948, is_finalization=is_finalization@entry=1) at Objects/object.c:929\n#2 0x000055555585c521 in PyThreadState_Clear (tstate=tstate@entry=0x555555c2cf60) at Python/pystate.c:1703\n#3 0x0000555555904a10 in thread_run (boot_raw=0x555555c194a0) at ./Modules/_threadmodule.c:390\n#4 0x000055555587e27b in pythread_wrapper (arg=\u003coptimized out>) at Python/thread_pthread.h:232\n#5 0x00007ffff7d31ac3 in start_thread (arg=\u003coptimized out>) at ./nptl/pthread_create.c:442\n#6 0x00007ffff7dc3850 in clone3 () at ../sysdeps/unix/sysv/linux/x86_64/clone3.S:81\n```\n\nA backtrace for a segfault I got with a variant of this code, seems more informative but I cannot repro with the MRE:\n```shell\nThread 185 \"asyncio_4\" received signal SIGSEGV, Segmentation fault.\n[Switching to Thread 0xffffe31ff060 (LWP 1527140)]\n0x0000000000867ce8 in _Py_TYPE (ob=\u003cunknown at remote 0xdddddddddddddddd>) at ./Include/object.h:270\n270 return ob->ob_type;\n\n#0 0x0000000000867ce8 in _Py_TYPE (ob=\u003cunknown at remote 0xdddddddddddddddd>) at ./Include/object.h:270\n#1 union_repr (self=\u003cunknown at remote 0xffffa425a9d0>) at Objects/unionobject.c:296\n#2 0x00000000006e61a8 in PyObject_Repr (v=\u003cunknown at remote 0xffffa425a9d0>) at Objects/object.c:779\n#3 0x000000000082ada0 in unicode_fromformat_arg (writer=writer@entry=0xffffe31fb4e0, f=0xd88224 \"R\", f@entry=0xd88223 \"%R\", vargs=vargs@entry=0xffffe31fb410)\n at Objects/unicodeobject.c:3101\n#4 0x000000000082b33c in unicode_from_format (writer=writer@entry=0xffffe31fb4e0, format=format@entry=0xd88220 \"%U=%R\", \n vargs=\u003cerror reading variable: Cannot access memory at address 0x1>) at Objects/unicodeobject.c:3220\n#5 0x000000000082b68c in PyUnicode_FromFormatV (format=format@entry=0xd88220 \"%U=%R\", vargs=...) at Objects/unicodeobject.c:3254\n#6 0x000000000082b828 in PyUnicode_FromFormat (format=format@entry=0xd88220 \"%U=%R\") at Objects/unicodeobject.c:3268\n#7 0x00000000006e1038 in namespace_repr (ns=\u003ctypes.SimpleNamespace at remote 0xffffa48e3c60>) at Objects/namespaceobject.c:129\n#8 0x000000000075e894 in object_str (self=self@entry=\u003ctypes.SimpleNamespace at remote 0xffffa48e3c60>) at Objects/typeobject.c:7152\n#9 0x0000000000768d18 in wrap_unaryfunc (self=\u003ctypes.SimpleNamespace at remote 0xffffa48e3c60>, args=\u003coptimized out>, wrapped=0x75e854 \u003cobject_str>)\n at Objects/typeobject.c:9536\n#10 0x00000000005d8ee0 in wrapperdescr_raw_call (kwds=\u003coptimized out>, args=\u003coptimized out>, self=\u003coptimized out>, descr=\u003coptimized out>) at Objects/descrobject.c:532\n#11 wrapper_call (self=\u003coptimized out>, args=\u003coptimized out>, kwds=\u003coptimized out>) at Objects/descrobject.c:1437\n#12 0x00000000005ab38c in _PyObject_MakeTpCall (tstate=tstate@entry=0xffff48937210, \n callable=callable@entry=\u003cmethod-wrapper '__str__' of types.SimpleNamespace object at 0xffffa48e3c60>, args=args@entry=0xffffe31fbe58, nargs=nargs@entry=0, \n keywords=keywords@entry=0x0) at Objects/call.c:242\n#13 0x00000000005aba68 in _PyObject_VectorcallTstate (tstate=0xffff48937210, callable=\u003cmethod-wrapper '__str__' of types.SimpleNamespace object at 0xffffa48e3c60>, \n args=0xffffe31fbe58, nargsf=9223372036854775808, kwnames=0x0) at ./Include/internal/pycore_call.h:167\n#14 0x00000000005aba98 in PyObject_Vectorcall (callable=callable@entry=\u003cmethod-wrapper '__str__' of types.SimpleNamespace object at 0xffffa48e3c60>, \n args=args@entry=0x0, nargsf=nargsf@entry=9223372036854775808, kwnames=kwnames@entry=0x0) at Objects/call.c:327\n```\n\nAbort backtrace:\n```shell\n*** stack smashing detected ***: terminated\n\nThread 7 \"Thread-6 (stres\" received signal SIGABRT, Aborted.\n[Switching to Thread 0x7ffff541b640 (LWP 351627)]\n__pthread_kill_implementation (no_tid=0, signo=6, threadid=0) at ./nptl/pthread_kill.c:44\n\n#0 __pthread_kill_implementation (no_tid=0, signo=6, threadid=0) at ./nptl/pthread_kill.c:44\n#1 __pthread_kill_internal (signo=6, threadid=0) at ./nptl/pthread_kill.c:78\n#2 __GI___pthread_kill (threadid=0, signo=signo@entry=6) at ./nptl/pthread_kill.c:89\n#3 0x00007ffff7cdf476 in __GI_raise (sig=sig@entry=6) at ../sysdeps/posix/raise.c:26\n#4 0x00007ffff7cc57f3 in __GI_abort () at ./stdlib/abort.c:79\n#5 0x00007ffff7d26677 in __libc_message (action=action@entry=do_abort, fmt=fmt@entry=0x7ffff7e7892e \"*** %s ***: terminated\\n\")\n at ../sysdeps/posix/libc_fatal.c:156\n#6 0x00007ffff7dd359a in __GI___fortify_fail (msg=msg@entry=0x7ffff7e78916 \"stack smashing detected\") at ./debug/fortify_fail.c:26\n#7 0x00007ffff7dd3566 in __stack_chk_fail () at ./debug/stack_chk_fail.c:24\n#8 0x000055555572321d in _Py_type_getattro_impl (type=\u003coptimized out>, name=\u003coptimized out>,\n suppress_missing_attribute=suppress_missing_attribute@entry=0x7ffff541a194) at Objects/typeobject.c:6355\n#9 0x00005555556db432 in PyObject_GetOptionalAttr (v=v@entry=\u003ctype at remote 0x3b2b2e06800>, name=\u003coptimized out>,\n result=result@entry=0x7ffff541a1d0) at Objects/object.c:1345\n#10 0x000055555572ea38 in _Py_typing_type_repr (writer=writer@entry=0x3b2be090e80, p=\u003ctype at remote 0x3b2b2e06800>) at Objects/typevarobject.c:295\n#11 0x000055555577ad6e in union_repr (self=\u003ctyping.Union at remote 0x3b2c20e0820>) at Objects/unionobject.c:297\n#12 0x00005555556d80db in PyObject_Repr (v=\u003ctyping.Union at remote 0x3b2c20e0820>) at ./Include/object.h:277\n#13 PyObject_Repr (v=\u003ctyping.Union at remote 0x3b2c20e0820>) at Objects/object.c:754\n#14 0x0000555555765835 in unicode_fromformat_arg (vargs=0x7ffff541a278, f=0x555555937b12 \"R\", writer=0x7ffff541a300)\n at Objects/unicodeobject.c:3101\n#15 unicode_from_format (writer=writer@entry=0x7ffff541a300, format=format@entry=0x555555937b0e \"%U=%R\", vargs=vargs@entry=0x7ffff541a340)\n at Objects/unicodeobject.c:3220\n#16 0x000055555576fadf in PyUnicode_FromFormatV (vargs=0x7ffff541a340, format=0x555555937b0e \"%U=%R\") at Objects/unicodeobject.c:3254\n#17 PyUnicode_FromFormat (format=format@entry=0x555555937b0e \"%U=%R\") at Objects/unicodeobject.c:3268\n#18 0x00005555556d5802 in namespace_repr (ns=\u003ctypes.SimpleNamespace at remote 0x3b2b2152310>) at Objects/namespaceobject.c:129\n#19 0x00005555556d80db in PyObject_Repr (v=\u003ctypes.SimpleNamespace at remote 0x3b2b2152310>) at ./Include/object.h:277\n#20 PyObject_Repr (v=\u003ctypes.SimpleNamespace at remote 0x3b2b2152310>) at Objects/object.c:754\n#21 0x00005555555e4883 in _PyEval_EvalFrameDefault (tstate=\u003coptimized out>, frame=\u003coptimized out>, throwflag=\u003coptimized out>)\n at Python/generated_cases.c.h:2487\n#22 0x00005555557dd13e in _PyEval_EvalFrame (throwflag=0, frame=\u003coptimized out>, tstate=0x555555c3c6e0) at ./Include/internal/pycore_ceval.h:119\n#23 _PyEval_Vector (tstate=0x555555c3c6e0, func=0x3b2b237fd80, locals=0x0, args=0x7ffff541a8a8, argcount=\u003coptimized out>, kwnames=\u003coptimized out>)\n at Python/ceval.c:1975\n```\n\n\nFound using [fusil](https://github.com/devdanzin/fusil) by @vstinner.\n\n### CPython versions tested on:\n\nCPython main branch\n\n### Operating systems tested on:\n\nLinux\n\n### Output from running 'python -VV' on the command line:\n\nPython 3.15.0a0 experimental free-threading build (heads/main:b706ff003c5, Jun 11 2025, 19:34:04) [GCC 11.4.0]\n\n\u003c!-- gh-linked-prs -->\n### Linked PRs\n* gh-135889\n* gh-135895\n* gh-135896\n\u003c!-- /gh-linked-prs -->\n","author":{"url":"https://github.com/devdanzin","@type":"Person","name":"devdanzin"},"datePublished":"2025-06-24T10:01:11Z","url":"https://github.com/python/cpython/issues/135878"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:e7f56d0e-13df-8ebb-1ffc-5d174b178e6d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | B8A4:111FE:2E32DA:3D27A0:6B023358 |
| html-safe-nonce | f9b6f6d246596359a9a6e2b3640f2ee8d95b8d2aab4a7d7cacb669576c427e95 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJCOEE0OjExMUZFOjJFMzJEQTozRDI3QTA6NkIwMjMzNTgiLCJ2aXNpdG9yX2lkIjoiNTc4Mzk1ODUyMTQ1NjUwNTY4OCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | e61de65dbd95c1f2c644857bb91277948ec679ba8944427cf8e67592d6aae4a2 |
| hovercard-subject-tag | issue:3171299265 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/python/cpython/135878/issue_layout |
| twitter:image | https://opengraph.githubassets.com/08d171d83ae501fd462dea1caad0bc962b291bb3116d0402c26dca7d0180d41f/python/cpython/issues/135878 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/08d171d83ae501fd462dea1caad0bc962b291bb3116d0402c26dca7d0180d41f/python/cpython/issues/135878 |
| og:image:alt | Crash report What happened? It's possible to segfault or abort the interpreter of a free-threading build by calling repr(SimpleNamespace) in threads when the instance has typing.Union attributes. T... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | devdanzin |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/python/cpython git https://github.com/python/cpython.git |
| octolytics-dimension-user_id | 1525981 |
| octolytics-dimension-user_login | python |
| octolytics-dimension-repository_id | 81598961 |
| octolytics-dimension-repository_nwo | python/cpython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 81598961 |
| octolytics-dimension-repository_network_root_nwo | python/cpython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width