Segmentation fault in python C extension
What version of protobuf and what language are you using? Version: v3.6.1 Language: Python
$ pip3 show protobuf
Name: protobuf
Version: 3.6.1
What operating system (Linux, Windows, ...) and version?
$ lsb_release -a
Distributor ID: Ubuntu
Description: Ubuntu 18.04.1 LTS
Release: 18.04
Codename: bionic
What runtime / compiler are you using (e.g., python version or gcc version)
$ python3 --version
Python 3.6.7
What did you do?
I'm using tf.train.Example from example.proto in tensorflow to produce training data, so processing quite a few records. When I added code to produce some stats from the code I started seeing occasional segmentation faults. They are not entirely predictable and require some 10m of crunching records on my machine.
The backtrace from python core dump:
(gdb) bt
#0 0x0000000000000041 in ?? ()
#1 0x00007f91e5d02e14 in ?? ()
from /home/siim/.local/lib/python3.6/site-packages/google/protobuf/pyext/_message.cpython-36m-x86_64-linux-gnu.so
#2 0x0000000000583694 in PySequence_GetItem (i=<optimised out>,
s=<google.protobuf.pyext._message.RepeatedScalarContainer at remote 0x7f91c6fc37a0>)
at ../Objects/abstract.c:1641
#3 iter_iternext.lto_priv () at ../Objects/iterobject.c:63
#4 0x0000000000506ac1 in _PyEval_EvalFrameDefault () at ../Python/ceval.c:3111
#5 0x0000000000504c28 in PyEval_EvalFrameEx (throwflag=0,
f=Frame 0x7f91ca9fe048, for file ./convert.py, line 516, in <listcomp> (.0=<iterator at remote 0x7f91c835ff60>)) at ../Python/ceval.c:4166
#6 _PyEval_EvalCodeWithName.lto_priv.1761 () at ../Python/ceval.c:4166
#7 0x0000000000502540 in fast_function.lto_priv () at ../Python/ceval.c:4978
#8 0x0000000000502f3d in call_function.lto_priv () at ../Python/ceval.c:4858
#9 0x0000000000506859 in _PyEval_EvalFrameDefault () at ../Python/ceval.c:3335
#10 0x0000000000502209 in PyEval_EvalFrameEx (throwflag=0,
f=Frame 0x7f91ca9fc3c8, for file ./convert.py, line 516, in int_value (record=<Example at remote 0x7f91c8a51ce8>, key='counted', v=<google.protobuf.pyext._message.RepeatedScalarContainer at remote 0x7f91c6fc37a0>)) at ../Python/ceval.c:754
#11 _PyFunction_FastCall (globals=<optimised out>, nargs=140264146453448, args=<optimised out>,
co=<optimised out>) at ../Python/ceval.c:4919
#12 fast_function.lto_priv () at ../Python/ceval.c:4954
#13 0x0000000000502f3d in call_function.lto_priv () at ../Python/ceval.c:4858
#14 0x0000000000506859 in _PyEval_EvalFrameDefault () at ../Python/ceval.c:3335
#15 0x000000000058c5d8 in PyEval_EvalFrameEx (throwflag=<optimised out>,
f=Frame 0x2fa7918, for file ./convert.py, line 577, in stats (count=1170454, missing_score=0, finished_counting=4956, matched_counting=4956, seen_hashes={"b'\\xb8\\xf7PD\\xab\\xecy\\xedV\\xdb\\xaf\\xb5\\x1a\\x08+)W\\x92n\\xcb'": 46, "b'\\x0fdS\\xe0<\\xf4\\xc97Tek\\xf5\\xd1\\x93Y\\x82\\xbf\\xa0Y@'": 104, 'b\'\\xbd\\x18\\xca4O\\x01\\xd9\\xf7R<E\\x85O\\x92\\xb4\\x12\\xdb\\xdc"\\x0f\'': 91, "b'\\x92\\x13.*\\xcc\\xf6\\xeb`\\xfb_\\xba\\x0e\\xc6N\\xafd\\xee\\xcc\\x87\\x19'": 101, "b'$\\xa3,t\\xa6b\\xf3\\x04uj(e\\x17U\\xde\\xf5o|\\xe1\\x96'": 66, "b'\\xcd\\xf0\\xe3\\xf9\\xbd\\xed\\xcca\\xa6\\xc4\\x8fz%\\xc9\\xea\\x14V1\\xd2\\xcc'": 61, "b'N\\xd2\\x847\\xf1m\\xd2\\xafh.\\xf9@\\xc5\\xa7\\xaf\\xc8!\\xf3\\x99\\xe2'": 36, "b'\\x9el\\x98\\xa9\\xfe\\xa6h&\\xb5\\xc3\\xe5\\x7f\\xa8\\x89Z\\x8e\\x82\\xdc\\x0b^'": 48, "b'K\\xa4\\x8e3]\\xc3{\\xb1\\x82_D\\x96\\xc4v\\xb7\\x06\\xd2\\xca\\xb6\\xc2'": 65, "b'\\xcb\\x88\\x8e\\x8f\\xa64\\xe9(\\xe5\\x81]1P!\\x98\\xad/\\x08\\xfbO'": 66, "b'|\\x087cd\\xda\\xf4\\x12\\xc0,\\x98\\xec\\xa0\\xb6G...(truncated))
at ../Python/ceval.c:754
#16 gen_send_ex.lto_priv () at ../Objects/genobject.c:189
#17 0x0000000000502e0c in _PyCFunction_FastCallDict (kwargs=<optimised out>, nargs=<optimised out>,
args=0x27badc0, func_obj=<built-in method send of generator object at remote 0x7f91cd65caf0>)
at ../Objects/methodobject.c:209
#18 _PyCFunction_FastCallKeywords (kwnames=<optimised out>, nargs=<optimised out>,
stack=<optimised out>, func=<optimised out>) at ../Objects/methodobject.c:294
#19 call_function.lto_priv () at ../Python/ceval.c:4837
#20 0x0000000000506859 in _PyEval_EvalFrameDefault () at ../Python/ceval.c:3335
#21 0x0000000000502209 in PyEval_EvalFrameEx (throwflag=0,
f=Frame 0x27babe8, for file ./convert.py, line 708, in main (p=<ArgumentParser(description=None, argument_default=None, prefix_chars='-', conflict_handler='error', _registries={'action': {None: <type at remote 0x12e0158>, 'store': <type at remote 0x12e0158>, 'store_const': <type at remote 0x12e0508>, 'store_true': <type at remote 0x12e08b8>, 'store_false': <type at remote 0x12e0c68>, 'append': <type at remote 0x12e1018>, 'append_const': <type at remote 0x12e13c8>, 'count': <type at remote 0x12e1778>, 'help': <type at remote 0x12e6b38>, 'version': <type at remote 0x12e23d8>, 'parsers': <type at remote 0x12e2b38>}, 'type': {None: <function at remote 0x7f91cd682950>}}, _actions=[<_HelpAction(option_strings=['-h', '--help'], dest='help', nargs=0, const=None, default='==SUPPRESS==', type=None, choices=None, required=False, help='show this help message and exit', metavar=None, container=<_ArgumentGroup(description=None, argument_default=None, prefix_chars='-', conflict_handler='error', _registries={...}, _actions=[.....(truncated))
at ../Python/ceval.c:754
#22 _PyFunction_FastCall (globals=<optimised out>, nargs=41659368, args=<optimised out>,
co=<optimised out>) at ../Python/ceval.c:4919
#23 fast_function.lto_priv () at ../Python/ceval.c:4954
#24 0x0000000000502f3d in call_function.lto_priv () at ../Python/ceval.c:4858
#25 0x0000000000506859 in _PyEval_EvalFrameDefault () at ../Python/ceval.c:3335
#26 0x0000000000504c28 in PyEval_EvalFrameEx (throwflag=0,
f=Frame 0x11f9c58, for file ./convert.py, line 733, in <module> ()) at ../Python/ceval.c:4166
#27 _PyEval_EvalCodeWithName.lto_priv.1761 () at ../Python/ceval.c:4166
#28 0x0000000000506393 in PyEval_EvalCodeEx (closure=0x0, kwdefs=0x0, defcount=0, defs=0x0, kwcount=0,
kws=0x0, argcount=0, args=0x0, locals=<optimised out>, globals=<optimised out>, _co=<optimised out>)
at ../Python/ceval.c:4187
#29 PyEval_EvalCode (co=<optimised out>, globals=<optimised out>, locals=<optimised out>)
at ../Python/ceval.c:731
#30 0x0000000000634d52 in run_mod () at ../Python/pythonrun.c:1025
#31 0x0000000000634e0a in PyRun_FileExFlags () at ../Python/pythonrun.c:978
#32 0x00000000006385c8 in PyRun_SimpleFileExFlags () at ../Python/pythonrun.c:419
#33 0x00000000006387a5 in PyRun_AnyFileExFlags () at ../Python/pythonrun.c:81
#34 0x000000000063915a in run_file (p_cf=0x7ffd626db50c, filename=<optimised out>, fp=<optimised out>)
at ../Modules/main.c:340
#35 Py_Main () at ../Modules/main.c:810
#36 0x00000000004a6f10 in main (argc=9, argv=0x7ffd626db708) at ../Programs/python.c:69
I don't understand what the extension is trying to do there without the debugging symbols, just in case someone can derive a hint out if it, here is what was going on:
(gdb) x/50i 0x7f91e5d02dee
0x7f91e5d02dee: xchg %ax,%ax
0x7f91e5d02df0: push %r14
0x7f91e5d02df2: push %r13
0x7f91e5d02df4: push %r12
0x7f91e5d02df6: mov %rsi,%r12
0x7f91e5d02df9: push %rbp
0x7f91e5d02dfa: push %rbx
0x7f91e5d02dfb: sub $0x30,%rsp
0x7f91e5d02dff: mov 0x20(%rdi),%rbp
0x7f91e5d02e03: mov 0x30(%rdi),%rbx
0x7f91e5d02e07: mov 0x0(%rbp),%rax
0x7f91e5d02e0b: mov %rbp,%rdi
0x7f91e5d02e0e: callq *0xc0(%rax)
=> 0x7f91e5d02e14: mov (%rdx),%rax
0x7f91e5d02e17: mov %rdx,%r13
0x7f91e5d02e1a: mov %rbp,%rsi
0x7f91e5d02e1d: mov %rbx,%rdx
0x7f91e5d02e20: mov %r13,%rdi
0x7f91e5d02e23: callq *0x30(%rax)
0x7f91e5d02e26: test %r12,%r12
0x7f91e5d02e29: cltq
(gdb) info regi
rax 0x16622138 375529784
rbx 0x2d7ec30 47705136
rcx 0x0 0
rdx 0x7f91e6067260 140264606167648
rsi 0x0 0
rdi 0xaf63430 183907376
rbp 0xaf63430 0xaf63430
rsp 0x7ffd626da840 0x7ffd626da840
r8 0x5d 93
r9 0xffffffff 4294967295
r10 0x7f91c835ff60 140264105967456
r11 0x5d 93
r12 0x0 0
r13 0x7f91ca9fe048 140264146460744
r14 0x0 0
r15 0xc 12
rip 0x7f91e5d02e14 0x7f91e5d02e14
eflags 0x10202 [ IF RF ]
cs 0x33 51
ss 0x2b 43
ds 0x0 0
es 0x0 0
fs 0x0 0
gs 0x0 0
(gdb) x/8a (0x16622138+0xc0-32)
0x166221d8: 0x0 0x1c626a70
0x166221e8: 0x7add910 0x30
0x166221f8: 0x41 0x7f91e605a510 <_ZTVN6google8protobuf14DynamicMessageE+16>
0x16622208: 0x2f1f7f0 0x0
The frame 5 convert.py:516 is the last line of this function:
def int_value(record: tf.train.Example, key: str) -> Optional[List[int]]:
v = record.features.feature[key].int64_list.value
if v is None:
return v
else:
return [int(i) for i in v]
I don't expect this matters, but just for context: the code around it is producing some number of millions of tf.train.Example messages from multiple python generators (for a first pass of data shuffling) so many are being produced at any given point in time but all in a single process and single thread. The messages are constructed from 3 levels: "base", "intermediate base" and "sample" levels that are mushed together with a MergeFrom before being emitted to the writing (SerializeToString) and statistics consumers (statistics is the part that sees the crash).
This is reproducible on my side (I can't finish a data transformation run any more without it crashing sooner or later in the process) but I don't have a minimal example that crashes at this point. I tried just producing the protobuf according to the pattern I describe in the previous paragraph but that did not seem to crash, at least not straight away. If there are any ideas WRT what part of the protobuf handling is more likely to be behind this, I can try to look through my code and see if I should try to recreate some part of it more faithfully.
What did you expect to see
No segmentation fault.
What did you see instead?
A segmentation fault.
Anything else we should know about your project / environment
I can't think of anything in particular - LMK if there are questions.
Another backtrace in the same place - this time %rax is 0 so the call address lookup fails alltogether:
(gdb) x/20i 0x7fdb1a9e6df0
0x7fdb1a9e6df0: push %r14
0x7fdb1a9e6df2: push %r13
0x7fdb1a9e6df4: push %r12
0x7fdb1a9e6df6: mov %rsi,%r12
0x7fdb1a9e6df9: push %rbp
0x7fdb1a9e6dfa: push %rbx
0x7fdb1a9e6dfb: sub $0x30,%rsp
0x7fdb1a9e6dff: mov 0x20(%rdi),%rbp
0x7fdb1a9e6e03: mov 0x30(%rdi),%rbx
0x7fdb1a9e6e07: mov 0x0(%rbp),%rax
0x7fdb1a9e6e0b: mov %rbp,%rdi
=> 0x7fdb1a9e6e0e: callq *0xc0(%rax)
0x7fdb1a9e6e14: mov (%rdx),%rax
0x7fdb1a9e6e17: mov %rdx,%r13
0x7fdb1a9e6e1a: mov %rbp,%rsi
0x7fdb1a9e6e1d: mov %rbx,%rdx
0x7fdb1a9e6e20: mov %r13,%rdi
0x7fdb1a9e6e23: callq *0x30(%rax)
0x7fdb1a9e6e26: test %r12,%r12
0x7fdb1a9e6e29: cltq
(gdb) info regi rax
rax 0x0 0
I've also occasionally gotten this:
[libprotobuf FATAL google/protobuf/generated_message_reflection.cc:102] Protocol Buffer reflection usage error:
Method : google::protobuf::Reflection::FieldSize
Message type: tensorflow.FloatList
Field : tensorflow.Int64List.value
Problem : Field does not match message type.
terminate called after throwing an instance of 'google::protobuf::FatalException'
what(): Protocol Buffer reflection usage error:
Method : google::protobuf::Reflection::FieldSize
Message type: tensorflow.FloatList
Field : tensorflow.Int64List.value
Problem : Field does not match message type.
Since there is (well - should be - there could be some subtle hard to notice bug) nothing dynamic about the way the message types are used from python, it sounds like something gets moved around incorrectly somewhere in protobuf code.
I've tried rebuilding from latest master and the segfault seems to have gone away.
There have been a few changes to pyext since the Aug 13 release so it sounds feasible the issues have been fixed in master. Or it could be that my local build is somehow different and less likely to tickle the bug.
I understand you have tried to create a minimal example to reproduce but failed. But it is really hard for us to fix the issue without it...
Understood. Maybe there is a clue in recent changes to the python extension? Does it make sense that one of those changes may have fixed a segfault?
Closing since this seems to be inactionable and long-fixed in main. Please feel free to reopen if this is still an issue.