sina-mansour / Cerebro_Viewer

A pythonic 3D viewer to visualize and plot brains and neuroimaging data
MIT License
31 stars 13 forks source link

Allow the user to close the viewer by clicking on the x close button on the window without producing a SystemExit error #10

Open dsleiter opened 1 year ago

dsleiter commented 1 year ago

Steps to reproduce:

  1. start the viewer from within a notebook
  2. click on the x in the viewer gui to close the window

The following error is printed inside the notebook:

An exception has occurred, use %tb to see the full traceback.

SystemExit
/Users/darin/miniconda3/envs/cerebro/lib/python3.9/site-packages/IPython/core/interactiveshell.py:3441: UserWarning: To exit: use 'exit', 'quit', or Ctrl-D.
  warn("To exit: use 'exit', 'quit', or Ctrl-D.", stacklevel=1)
Ln11211 commented 1 year ago

Hey there, I looked into this and I found that Panda uses sys.exit() so the error is inevitable. Here is the link to extended traceback https://pastebin.com/CyzifM4V

I guess we can handle the exception using a try block in the renderer.py like this, image

dsleiter commented 1 year ago

Thanks for the investigative work @Ln11211!

Catching the SystemExit seems like it would work as a short-term work-around, but I'd be worried about potential side effects. What should happen if the application was called via a script rather than from a notebook? What should happen if there was an error and sys.exit was called with a non-zero error from another part of the ShowBase?

Looking at pandas documentation, it seems like the function which calls sys.exit() is made to be overridden (since it says "the default implementation...") https://docs.panda3d.org/1.10/python/reference/direct.showbase.ShowBase#direct.showbase.ShowBase.ShowBase.finalizeExit So we could maybe override finalizeExit or another exit function it in Cerebro_window with some other behavior.

Does Panda3D documentation give any guidance on implementing exit functionality?

Ln11211 commented 1 year ago

Hey @dsleiter , after looking further into this issue, I understood that this behavior of the viewer is only specific to notebooks and there is a further breakdown of the viewer when we try to run the script right after we close it, Exception: Attempt to spawn multiple ShowBase instances! And the Kernel dies. This has nothing to do with Cerebro, but everything to do with the 'Sys.exit()'

Steps to reproduce

  1. Run the script (code cell) once in a notebook
  2. close it using the gui
  3. Run the script again(cell)

traceback I have attached the traceback and also a Stack overflow thread that address the same issue.

TLDR

This weird breakdown of the Panda is only due to the Sys.exit() and happens only in notebooks and not in Terminal. After the initial SystemExit exception, the Panda breaks down when tried to run again. Read the StackOverflow thread for detailed explanation.