Skip to content

[Python] Segmentation fault on program exit (GC) with Python 3.13 #22067

@mgorny

Description

@mgorny

What version of protobuf and what language are you using?
Version: v6.31.1
Language: Python

What operating system (Linux, Windows, ...) and version?
Gentoo Linux amd64

What runtime / compiler are you using (e.g., python version or gcc version)
Python 3.13.3
GCC 14.3.0

What did you do?
For example, run the following test case:

$ pytest google/protobuf/internal/timestamp_test.py::TimestampTest::test_timestamp_datetime
========================================================= test session starts =========================================================
platform linux -- Python 3.13.3, pytest-8.3.5, pluggy-1.6.0
rootdir: /tmp/portage/dev-python/protobuf-6.31.1/work/protobuf-6.31.1-python3_13/test/usr/lib/python3.13/site-packages
collected 1 item                                                                                                                      

google/protobuf/internal/timestamp_test.py .                                                                                    [100%]

========================================================== 1 passed in 0.08s ==========================================================
Segmentation fault (core dumped)

There are a few other cases in the test suite causing the package to segfault on exit. I've also seen it with a third-party package before, but I don't recall which one.

What did you expect to see
No segfault.

What did you see instead?

(gdb) bt
#0  0x00007f417c66106c in _Py_hashtable_foreach (ht=0x0, func=func@entry=0x7f417c661316 <hashtable_next_match_def>, 
    user_data=user_data@entry=0x7ffd8121cad0) at Python/hashtable.c:272
#1  0x00007f417c661e85 in _find_cached_def (def=def@entry=0x7f417a75d180 <module_def>) at Python/import.c:1231
#2  0x00007f417c661eae in _get_module_index_from_def (def=def@entry=0x7f417a75d180 <module_def>) at Python/import.c:404
#3  0x00007f417c66557c in PyState_FindModule (module=module@entry=0x7f417a75d180 <module_def>) at Python/import.c:497
#4  0x00007f417a6f9eff in PyUpb_ModuleState_MaybeGet () at python/protobuf.c:61
#5  0x00007f417a6f57d2 in PyUpb_GetMessageMeta (
    cls=<google._upb._message.MessageMeta(DESCRIPTOR=<google._upb._message.Descriptor at remote 0x7f417a6cecf0>, __module__='google.protobuf.timestamp_pb2', __slots__=(), __doc__=None) at remote 0x55bc305d3c30>) at python/message.c:1833
#6  0x00007f417a6f5884 in PyUpb_MessageMeta_Traverse (
    self=<google._upb._message.MessageMeta(DESCRIPTOR=<google._upb._message.Descriptor at remote 0x7f417a6cecf0>, __module__='google.protobuf.timestamp_pb2', __slots__=(), __doc__=None) at remote 0x55bc305d3c30>, visit=0x7f417c654309 <visit_decref>, arg=0x55bc305d3c30)
    at python/message.c:2032
#7  0x00007f417c6532a6 in subtract_refs (containers=containers@entry=0x7f417c987160 <_PyRuntime+95872>) at Python/gc.c:464
#8  0x00007f417c653d94 in deduce_unreachable (base=base@entry=0x7f417c987160 <_PyRuntime+95872>, 
    unreachable=unreachable@entry=0x7ffd8121cbe0) at Python/gc.c:1087
#9  0x00007f417c654b91 in gc_collect_main (tstate=tstate@entry=0x7f417c9b4c70 <_PyRuntime+283024>, generation=generation@entry=2, 
    reason=reason@entry=_Py_GC_REASON_SHUTDOWN) at Python/gc.c:1360
#10 0x00007f417c65533f in _PyGC_CollectNoFail (tstate=tstate@entry=0x7f417c9b4c70 <_PyRuntime+283024>) at Python/gc.c:1657
#11 0x00007f417c68dd09 in interpreter_clear (interp=0x7f417c985430 <_PyRuntime+88400>, 
    tstate=tstate@entry=0x7f417c9b4c70 <_PyRuntime+283024>) at Python/pystate.c:873
#12 0x00007f417c68e11a in _PyInterpreterState_Clear (tstate=tstate@entry=0x7f417c9b4c70 <_PyRuntime+283024>) at Python/pystate.c:931
#13 0x00007f417c687153 in finalize_interp_clear (tstate=tstate@entry=0x7f417c9b4c70 <_PyRuntime+283024>) at Python/pylifecycle.c:1916
#14 0x00007f417c68949b in _Py_Finalize (runtime=runtime@entry=0x7f417c96fae0 <_PyRuntime>) at Python/pylifecycle.c:2217
#15 0x00007f417c689522 in Py_FinalizeEx () at Python/pylifecycle.c:2259
#16 0x00007f417c6b86ba in Py_RunMain () at Modules/main.c:777
#17 0x00007f417c6b8736 in pymain_main (args=args@entry=0x7ffd8121cd70) at Modules/main.c:805
#18 0x00007f417c6b880d in Py_BytesMain (argc=<optimized out>, argv=<optimized out>) at Modules/main.c:829
#19 0x000055bc0ca2e186 in main (argc=<optimized out>, argv=<optimized out>) at ./Programs/python.c:15

Anything else we should know about your project / environment
I can't reproduce the problem with Python 3.12, so supposedly something changed there.

Metadata

Metadata

Assignees

Labels

inactiveDenotes the issue/PR has not seen activity in the last 90 days.python

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions