bloomberg / pystack

πŸ” 🐍 Like pstack but for Python!
https://bloomberg.github.io/pystack
Apache License 2.0
978 stars 45 forks source link

Jinja templating seems to be crashing PyStack with an `IndexError` #191

Open strugee opened 6 days ago

strugee commented 6 days ago

Is there an existing issue for this?

Current Behavior

<irrelevant stack trace output truncated>
    (Python) File "/opt/venvs/debtsy/lib/python3.9/site-packages/flask/templating.py", line 120, in _render
        rv = template.render(context)
    (Python) File "/opt/venvs/debtsy/lib/python3.9/site-packages/ddtrace/contrib/jinja2/patch.py", line 70, in _wrap_render
        return wrapped(*args, **kwargs)
    (Python) File "/opt/venvs/debtsy/lib/python3.9/site-packages/jinja2/environment.py", line 1043, in render
        return concat(self.root_render_func(self.new_context(vars)))
    (Python) File "/opt/venvs/debtsy/lib/python3.9/site-packages/flask_admin/templates/bootstrap3/admin/model/edit.html", line 24, in root
        {%- if admin_view.can_view_details -%}
    (Python) File "/opt/venvs/debtsy/lib/python3.9/site-packages/flask_admin/templates/bootstrap3/admin/master.html", line 16, in root
Traceback (most recent call last):
  File "/root/venv/bin/pystack", line 8, in <module>
    sys.exit(main())
  File "/root/venv/lib/python3.8/site-packages/pystack/__main__.py", line 254, in main
    args.func(parser, args)
  File "/root/venv/lib/python3.8/site-packages/pystack/__main__.py", line 381, in process_core
    print_thread(thread, native)
  File "/root/venv/lib/python3.8/site-packages/pystack/traceback_formatter.py", line 15, in print_thread
    for line in format_thread(thread, native):
  File "/root/venv/lib/python3.8/site-packages/pystack/traceback_formatter.py", line 81, in format_thread
    yield from format_frame(current_frame)
  File "/root/venv/lib/python3.8/site-packages/pystack/traceback_formatter.py", line 29, in format_frame
    source = lines[code.location.lineno - 1]
IndexError: list index out of range

Expected Behavior

PyStack does not crash when processing this process.

Steps To Reproduce

I can try and nail down a more constrained STR, but I'm not really sure what to actually try since that it's hard to reduce our entire app to a minimal example... given the traceback (see below), I figured I would file a bug and see if anyone had any ideas on what to try based on their knowledge of the way PyStack works.

PyStack was freshly installed from Pip FWIW, into a fresh virtual environment. This crash happened on both a coredump file and on a live process (processing a request for the same URL). We're running Ubuntu 20.04.6.

Pystack Version

1.3.0

Python Version

3.8

Linux distribution

Ubuntu

Anything else?

No response

pablogsal commented 6 days ago

Hi @strugee and thanks for the report! We can try to investigate but without a reproducer is very hard for us to do anything here. Can you try to get a small jinja-based example to see if it crashes?

if not, can you modify /root/venv/lib/python3.8/site-packages/pystack/traceback_formatter.py to print lines and code.location? Like so:

print(lines, code.location)
source = lines[code.location.lineno - 1]