plasma-umass / scalene

Scalene: a high-performance, high-precision CPU, GPU, and memory profiler for Python with AI-powered optimization proposals
Apache License 2.0
11.59k stars 388 forks source link

PyBind11 Segmentation Fault #246

Open atw1020 opened 3 years ago

atw1020 commented 3 years ago

Describe the bug I'm writing a library using PyBind11 and benchmarking it with scalene and I've started getting a message from scalene saying I've got a Segfault (in particular I get "Scalene error: received signal SIGSEGV"). However when I run my code without scalene I don't get a segmentation fault and my code runs without error. I think that the segfault is caused by the GIL because my code releases the GIL and I've noticed that scalene does not get the segfault signal when I do not release the GIL.

To Reproduce

  1. Clone my repository at this commit.
  2. Build the extension using the setup.py and add the library to some python environment.
  3. The following code snippet should should produce a segfault when run in scalene and no segfault when run in an ordinary python interpreter.
# sente is a python library for the game of Go.
import sente

game = sente.Game()

# this code plays a sequence of 100 legal moves. 
for i in range(100):
    moves = game.get_legal_moves()
    game.play(moves[0])

Expected behavior The program runs without segmentation fault.

Screenshots

Screen Shot 2021-07-29 at 1 26 39 PM Screen Shot 2021-07-29 at 1 26 58 PM

Desktop (please complete the following information):

Additional context

I'm not sure if this is a bug with scalene or if scalene is revealing a problem with my code that I don't understand and I was wondering if someone could shed some light on this.

kezabelle commented 3 years ago

I'm seeing a similar thing here across a couple of Django applications I've run locally using sqlite3. Weirdly I was able to run scalene <file>.py the first time, and it spewed all the line information to the console, so I tried re-running it with --html and it errored with Scalene error: received signal SIGSEGV and all subsequent invocations with or without parameters crash in that way.

> system_profiler SPHardwareDataType
Hardware:

    Hardware Overview:

      Model Name: MacBook Air
      Model Identifier: MacBookAir9,1
      Processor Name: Quad-Core Intel Core i7
      Processor Speed: 1.2 GHz
      Number of Processors: 1
      Total Number of Cores: 4
      L2 Cache (per Core): 512 KB
      L3 Cache: 8 MB
      Hyper-Threading Technology: Enabled
      Memory: 16 GB
      Boot ROM Version: 1037.147.1.0.0 (iBridge: 17.16.16065.0.0,0)
      ...
> python3 -V
Python 3.9.5
> scalene --version
Scalene version 1.3.12

Here's the console info:

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   libscalene.dylib                0x0000000100fc209f xxmalloc + 159
1   libsqlite3.dylib                0x00007fff6884ef96 sqlite3MemMalloc + 38
2   libsqlite3.dylib                0x00007fff68935f31 dbMallocRawFinish + 17
3   libsqlite3.dylib                0x00007fff688601eb yy_reduce + 25451
4   libsqlite3.dylib                0x00007fff68858215 sqlite3RunParser + 1045
5   libsqlite3.dylib                0x00007fff6885671a sqlite3LockAndPrepare + 1290
6   libsqlite3.dylib                0x00007fff68878e89 sqlite3InitCallback + 969
7   libsqlite3.dylib                0x00007fff68855a36 sqlite3_exec + 934
8   libsqlite3.dylib                0x00007fff688788b0 sqlite3InitOne + 1664
9   libsqlite3.dylib                0x00007fff688c0750 sqlite3LocateTable + 240
10  libsqlite3.dylib                0x00007fff6889503d selectExpander + 733
11  libsqlite3.dylib                0x00007fff68894c61 sqlite3WalkSelect + 49
12  libsqlite3.dylib                0x00007fff68966e7a sqlite3SelectPrep + 138
13  libsqlite3.dylib                0x00007fff68888772 sqlite3Select + 194
14  libsqlite3.dylib                0x00007fff6885ab08 yy_reduce + 3208
15  libsqlite3.dylib                0x00007fff68858215 sqlite3RunParser + 1045
16  libsqlite3.dylib                0x00007fff6885671a sqlite3LockAndPrepare + 1290
17  libsqlite3.dylib                0x00007fff688dfba2 sqlite3_prepare_v2 + 50
18  _sqlite3.cpython-39-darwin.so   0x0000000101dade9c pysqlite_statement_create + 348
19  _sqlite3.cpython-39-darwin.so   0x0000000101da9cb1 pysqlite_connection_call + 513
20  python3                         0x0000000100c3b8a7 _PyObject_MakeTpCall + 375
21  python3                         0x0000000100c3c7cb _PyObject_CallFunctionVa + 683
22  python3                         0x0000000100c3ca06 _PyObject_CallFunction_SizeT + 166
23  _sqlite3.cpython-39-darwin.so   0x0000000101da7d28 pysqlite_cache_get + 312
24  _sqlite3.cpython-39-darwin.so   0x0000000101dab5fd _pysqlite_query_execute + 621
25  python3                         0x0000000100c44b45 method_vectorcall_VARARGS + 165
26  python3                         0x0000000100d2aa9b call_function + 411
27  python3                         0x0000000100d27650 _PyEval_EvalFrameDefault + 28032
28  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
29  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
30  python3                         0x0000000100d2aa9b call_function + 411
31  python3                         0x0000000100d27633 _PyEval_EvalFrameDefault + 28003
32  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
33  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
34  python3                         0x0000000100c3e66c method_vectorcall + 204
35  python3                         0x0000000100d2aa9b call_function + 411
36  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
37  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
38  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
39  python3                         0x0000000100c3e66c method_vectorcall + 204
40  python3                         0x0000000100d2aa9b call_function + 411
41  python3                         0x0000000100d277a0 _PyEval_EvalFrameDefault + 28368
42  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
43  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
44  python3                         0x0000000100c3e66c method_vectorcall + 204
45  python3                         0x0000000100d2aa9b call_function + 411
46  python3                         0x0000000100d27650 _PyEval_EvalFrameDefault + 28032
47  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
48  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
49  python3                         0x0000000100c3e66c method_vectorcall + 204
50  python3                         0x0000000100d2aa9b call_function + 411
51  python3                         0x0000000100d27650 _PyEval_EvalFrameDefault + 28032
52  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
53  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
54  python3                         0x0000000100c3e66c method_vectorcall + 204
55  python3                         0x0000000100d2aa9b call_function + 411
56  python3                         0x0000000100d277a0 _PyEval_EvalFrameDefault + 28368
57  python3                         0x0000000100c4e0e4 gen_send_ex + 468
58  python3                         0x0000000100c5a84a list_extend + 666
59  python3                         0x0000000100c5f722 list___init___impl + 242
60  python3                         0x0000000100c5c0c1 list_vectorcall + 129
61  python3                         0x0000000100d2aa9b call_function + 411
62  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
63  python3                         0x0000000100c3c125 function_code_fastcall + 229
64  python3                         0x0000000100d2aa9b call_function + 411
65  python3                         0x0000000100d27633 _PyEval_EvalFrameDefault + 28003
66  python3                         0x0000000100c3c125 function_code_fastcall + 229
67  python3                         0x0000000100c9e4c0 slot_tp_iter + 592
68  python3                         0x0000000100c24388 PyObject_GetIter + 24
69  python3                         0x0000000100c241d8 PySequence_Tuple + 72
70  python3                         0x0000000100d2aa9b call_function + 411
71  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
72  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
73  python3                         0x0000000100d20800 PyEval_EvalCode + 64
74  python3                         0x0000000100d1d545 builtin_exec + 661
75  python3                         0x0000000100c7e567 cfunction_vectorcall_FASTCALL + 215
76  python3                         0x0000000100d2aa9b call_function + 411
77  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
78  python3                         0x0000000100c3c125 function_code_fastcall + 229
79  python3                         0x0000000100d2aa9b call_function + 411
80  python3                         0x0000000100d27633 _PyEval_EvalFrameDefault + 28003
81  python3                         0x0000000100c3c125 function_code_fastcall + 229
82  python3                         0x0000000100d2aa9b call_function + 411
83  python3                         0x0000000100d27650 _PyEval_EvalFrameDefault + 28032
84  python3                         0x0000000100c3c125 function_code_fastcall + 229
85  python3                         0x0000000100d2aa9b call_function + 411
86  python3                         0x0000000100d27650 _PyEval_EvalFrameDefault + 28032
87  python3                         0x0000000100c3c125 function_code_fastcall + 229
88  python3                         0x0000000100d2aa9b call_function + 411
89  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
90  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
91  python3                         0x0000000100d20800 PyEval_EvalCode + 64
92  python3                         0x0000000100d1d545 builtin_exec + 661
93  python3                         0x0000000100c7e567 cfunction_vectorcall_FASTCALL + 215
94  python3                         0x0000000100d2aa9b call_function + 411
95  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
96  python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
97  python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
98  python3                         0x0000000100d2aa9b call_function + 411
99  python3                         0x0000000100d276ef _PyEval_EvalFrameDefault + 28191
100 python3                         0x0000000100d2bb3e _PyEval_EvalCode + 3166
101 python3                         0x0000000100c3c031 _PyFunction_Vectorcall + 257
102 python3                         0x0000000100d91a29 pymain_run_module + 377
103 python3                         0x0000000100d90efd Py_RunMain + 1341
104 python3                         0x0000000100d91846 pymain_main + 406
105 python3                         0x0000000100d9189b Py_BytesMain + 43
106 libdyld.dylib                   0x00007fff6988acc9 start + 1
...
Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x00000001017cb110  rbx: 0x0000000000000015  rcx: 0x0000000000000001  rdx: 0x0000000000000020
  rdi: 0x0000000000000102  rsi: 0x0000000000000010  rbp: 0x00007ffeef0056a0  rsp: 0x00007ffeef005680
   r8: 0x00007ffeef0058e8   r9: 0x00007ffeef0062c0  r10: 0x00007fff68a22780  r11: 0x0000000000000058
  r12: 0x615f6f676e616a64  r13: 0x0000000106d5a870  r14: 0x0000000100fcad00  r15: 0x0000000101846000
  rip: 0x0000000100fc209f  rfl: 0x0000000000010202  cr2: 0x0000000106e64006

Logical CPU:     0
Error Code:      0x00000000
Trap Number:     13
HansBambel commented 3 years ago

I am also getting the same error. I am also on MacOS Big Sur 11.5.1 on an Apple M1 Chip.

atw1020 commented 3 years ago

On the topic of hardware, I've been able to replicate this issue on both my old intel Mac and also my new M1 Mac so I'm pretty sure that if this issue is hardware independent.

HansBambel commented 3 years ago

On Debian it works for me. So maybe it's an OS issue?