Tribler / tribler

Privacy enhanced BitTorrent client with P2P content discovery
https://www.tribler.org
GNU General Public License v3.0
4.74k stars 445 forks source link

Show Tribler source lines in traceback when running Tribler binary #7968

Closed kozlovsky closed 2 months ago

kozlovsky commented 2 months ago

When Tribler binary encounters an error, it currently displays traceback with file names and line numbers only, without the actual source lines, like that:

  File "C:\dev\tribler\src\run_tribler.py", line 124, in <module>
  File "C:\dev\tribler\src\run_tribler.py", line 120, in main
  File "C:\dev\tribler\src\tribler\gui\start_gui.py", line 77, in run_gui
  File "C:\dev\tribler\src\tribler\gui\tribler_window.py", line 236, in __init__

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\dev\tribler\src\tribler\gui\utilities.py", line 455, in trackback_wrapper
  File "C:\dev\tribler\src\tribler\gui\utilities.py", line 452, in trackback_wrapper
  File "C:\dev\tribler\src\tribler\gui\tribler_window.py", line 361, in on_test_tribler_gui_exception
tribler.gui.exceptions.TriblerGuiTestException: Tribler GUI Test Exception

The traceback misses source lines because the linecache module cannot find source files to display the actual source lines. Usually, a binary compiled with PyInstaller or cx_freeze includes only .pyc files, so finding corresponding source lines is impossible. However, Tribler includes Python files inside the tribler_source subfolder. This allows source lines to be displayed for files in the tribler_source subfolder. For that, we need a small patch for the linecache module that fixes relative file names so it becomes possible to find the corresponding file.

With this patch applied, tracebacks in Sentry reports show actual source lines, which makes it easier to understand the reason for the error:

  File "C:\dev\tribler\src\run_tribler.py", line 124, in <module>
    main()
  File "C:\dev\tribler\src\run_tribler.py", line 120, in main
    run_gui(api_port, api_key, root_state_dir, parsed_args)
  File "C:\dev\tribler\src\tribler\gui\start_gui.py", line 77, in run_gui
    window = TriblerWindow(process_manager, app_manager, settings, root_state_dir,
  File "C:\dev\tribler\src\tribler\gui\tribler_window.py", line 236, in __init__
    connect(self.tribler_gui_test_exception_shortcut.activated, self.on_test_tribler_gui_exception)

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "C:\dev\tribler\src\tribler\gui\utilities.py", line 455, in trackback_wrapper
    raise exc from CreationTraceback(traceback_str)
  File "C:\dev\tribler\src\tribler\gui\utilities.py", line 452, in trackback_wrapper
    callback(*args, **kwargs)
  File "C:\dev\tribler\src\tribler\gui\tribler_window.py", line 361, in on_test_tribler_gui_exception
    raise TriblerGuiTestException("Tribler GUI Test Exception")
tribler.gui.exceptions.TriblerGuiTestException: Tribler GUI Test Exception

This patch is not necessary, but it makes analyzing tracebacks easier.