Issue: Segmentation fault (core dumped) when executing model_to_dict(recurse=False)
Author: ajpen
State: closed
Created: 2016-10-20 02:46:50

Here's the problematic method

```
@app.route('/get_records_by_group', methods=["POST"])
def get_records_by_group():

    # try:
    #     session["user"]
    # except KeyError:
    #     return jsonify(**{"code": "401", "message" : "Unauthorized"})

    parameters = request.get_json(force=True)

    try:
        group = parameters["group"]
        order = parameters["order"]

    except (KeyError, ValueError):
        return jsonify(**{"code":"400", "message" : "Bad parameters"})

    pdb.set_trace()
    try:
        if order == "desc":
            records = Record.select().where(
                Record.verified_by == "" &
                Record.customer_name.startswith(group)
            ).order_by(
                    Record.customer_name.desc()
            )

        else:
            records = Record.select().where(
                Record.verified_by == "" &
                Record.customer_name.startswith(group)
            ).order_by(
                Record.customer_name.asc()
            )

    except Record.DoesNotExist:
        return jsonify(**[])

    data = [model_to_dict(x, recurse=False) for x in records]
    return Response(
        json.dumps(data, default=Record.date_handler),
        mimetype='application/json',
    )

```

The Segmentation fault occurs at `data = [model_to_dict(x, recurse=False) for x in records]`. I'm assuming its model_to_dict but I could be wrong. To note, `records` variable/query may have a lot of records (at most over 100k, average 10k, but this amount is unlikely). I tried to get the core, but that didn't go well. 


It appears this could be due to low memory. I'll check that possibility.


Okay, after some digging, I got this. 

The segfault is a `SIGSEGV`. 

Here's the backtrace from gdb

```
#0  0x00000000005377c7 in _PyObject_GC_New ()
#1  0x00000000005379da in PyDict_New ()
#2  0x00000000004ad2fd in _PyObject_GenericSetAttrWithDict ()
#3  0x00000000004ad9dd in PyObject_SetAttr ()
#4  0x000000000049a356 in PyEval_EvalFrameEx ()
#5  0x00000000004a1c9a in ?? ()
#6  0x00000000004dfe94 in ?? ()
#7  0x0000000000505f96 in PyObject_Call ()
#8  0x00000000004de41a in ?? ()
#9  0x00000000005039eb in ?? ()
#10 0x0000000000499be5 in PyEval_EvalFrameEx ()
#11 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#12 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#13 0x00000000004a1c9a in ?? ()
#14 0x00000000004dfe94 in ?? ()
#15 0x0000000000505f96 in PyObject_Call ()
#16 0x00000000004cac9f in ?? ()
#17 0x000000000049990f in PyEval_EvalFrameEx ()
#18 0x00000000004a1c9a in ?? ()
#19 0x0000000000505f96 in PyObject_Call ()
#20 0x000000000049b07a in PyEval_EvalFrameEx ()
#21 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#22 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#23 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#24 0x00000000004a1c9a in ?? ()
#25 0x00000000004dfe94 in ?? ()
#26 0x0000000000505f96 in PyObject_Call ()
#27 0x00000000004dddad in ?? ()
#28 0x0000000000499be5 in PyEval_EvalFrameEx ()
#29 0x00000000004a090c in PyEval_EvalCodeEx ()
#30 0x000000000049ab45 in PyEval_EvalFrameEx ()
#31 0x00000000004a090c in PyEval_EvalCodeEx ()
#32 0x000000000049ab45 in PyEval_EvalFrameEx ()
#33 0x00000000004a090c in PyEval_EvalCodeEx ()
#34 0x000000000049ab45 in PyEval_EvalFrameEx ()
#35 0x00000000004a1c9a in ?? ()
#36 0x00000000004dfe94 in ?? ()
#37 0x0000000000499be5 in PyEval_EvalFrameEx ()
#38 0x00000000004a090c in PyEval_EvalCodeEx ()
#39 0x000000000049ab45 in PyEval_EvalFrameEx ()
#40 0x00000000004a1c9a in ?? ()
#41 0x00000000004dfe94 in ?? ()
#42 0x0000000000505f96 in PyObject_Call ()
#43 0x00000000004de41a in ?? ()
#44 0x00000000005039eb in ?? ()
#45 0x0000000000499be5 in PyEval_EvalFrameEx ()
#46 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#47 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#48 0x0000000000499ef2 in PyEval_EvalFrameEx ()
#49 0x00000000004a1c9a in ?? ()
#50 0x00000000004dfe94 in ?? ()
#51 0x0000000000499be5 in PyEval_EvalFrameEx ()
#52 0x00000000004a090c in PyEval_EvalCodeEx ()
---Type <return> to continue, or q <return> to quit---
#53 0x000000000049ab45 in PyEval_EvalFrameEx ()
#54 0x00000000004a090c in PyEval_EvalCodeEx ()
#55 0x000000000049ab45 in PyEval_EvalFrameEx ()
#56 0x00000000004a1c9a in ?? ()
#57 0x0000000000505f96 in PyObject_Call ()
#58 0x000000000049b07a in PyEval_EvalFrameEx ()
#59 0x00000000004a090c in PyEval_EvalCodeEx ()
#60 0x0000000000499a52 in PyEval_EvalFrameEx ()
#61 0x00000000004a1634 in ?? ()
#62 0x000000000044e4a5 in PyRun_FileExFlags ()
#63 0x000000000044ec9f in PyRun_SimpleFileExFlags ()
#64 0x000000000044f904 in Py_Main ()
#65 0x00002aaaaaf0ff45 in __libc_start_main (main=0x44f9c2 <main>, argc=2, argv=0x7fffffffe678, init=<optimized out>, fini=<optimized out>, rtld_fini=<optimized out>,
    stack_end=0x7fffffffe668) at libc-start.c:287

```

From the function call at the top, I decided to up the stack size with `ulimit -s unlimited`, which was not successful. So, I'm now doubting whether its memory.  I don't know if its due to the list comprehension or not, I'll try putting the list comprehension in a function and converting it to a for loop, to confirm if it is stack size related. 


Its memory related. Ran out of memory while loading the records. Closing