Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZeroDivisionError on TUI #143

Closed
adamchainz opened this issue Jun 14, 2022 · 2 comments · Fixed by #147
Closed

ZeroDivisionError on TUI #143

adamchainz opened this issue Jun 14, 2022 · 2 comments · Fixed by #147
Assignees
Labels
bug Something isn't working

Comments

@adamchainz
Copy link

Bug Report

Current Behavior TUI crashes with a ZeroDivisionError when pressing "right arrow" after program has completed.

Input Code

Run memray run --live -c 'print(1)'. It completes shortly, and the TUI opens saying "Thread 1 of 0". Pressing the right arrow key to cycle between threads crashes with:

Traceback (most recent call last):
  File "/.../venv/bin/memray", line 8, in <module>
    sys.exit(main())
  File "/.../venv/lib/python3.9/site-packages/memray/commands/__init__.py", line 124, in main
    arg_values.entrypoint(arg_values, parser)
  File "/.../venv/lib/python3.9/site-packages/memray/commands/run.py", line 308, in run
    _run_child_process_and_attach(args)
  File "/.../venv/lib/python3.9/site-packages/memray/commands/run.py", line 117, in _run_child_process_and_attach
    raise error from None
  File "/.../venv/lib/python3.9/site-packages/memray/commands/run.py", line 114, in _run_child_process_and_attach
    LiveCommand().start_live_interface(port)
  File "/.../venv/lib/python3.9/site-packages/memray/commands/live.py", line 110, in start_live_interface
    tui.previous_thread()
  File "/.../venv/lib/python3.9/site-packages/memray/reporters/tui.py", line 371, in previous_thread
    self._thread_idx = (self._thread_idx - 1) % len(self._threads)
ZeroDivisionError: integer division or modulo by zero

Expected behavior/code no crash

Environment

  • Python(s): 3.9

Possible Solution

n/a

Additional context/Screenshots n/a

@godlygeek
Copy link
Contributor

godlygeek commented Jun 14, 2022

Thanks for the great reproducer ❤️

The fix is simple enough - before we get our first sample from the tracked process, we don't know what threads exist, so we need to handle that case by just doing nothing if the next/prev thread button is pressed.

What's weird is that we're somehow not learning what threads exist(ed) even after the tracked process exits, so something more may be going wrong here... We should only be in that weird "before first sample" state for a few milliseconds, and certainly shouldn't stay in it once the tracked process has already completed...

@godlygeek godlygeek self-assigned this Jun 14, 2022
@godlygeek godlygeek added the bug Something isn't working label Jun 14, 2022
@godlygeek
Copy link
Contributor

godlygeek commented Jun 14, 2022

Ah, OK. So this isn't just the state before the first sample, it's also the state when no memory has been allocated - and that's what your reproducer is hitting. We start the TUI before any samples have been collected, so we're in the state where we have no map of allocations to thread, and by the time we collect the next sample (a few milliseconds later) the process has allocated a bunch of memory, but also already freed everything that it allocated and exited, and so we still don't have any allocations to display, since at all times what the --live view shows is what has been allocated but not yet freed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants