courselab / snaskii22

Programming exercise based on snaskii (class of 2022)
GNU General Public License v3.0
8 stars 11 forks source link

Gracious ncurses exit #50

Open FulecoRafa opened 1 year ago

FulecoRafa commented 1 year ago

As of now, the ncurses library is not properly deactivated in case the program panics during runtime. This presents a problem because as soon is the program exits with the panic, the user terminal stays in a mode which breaks output and is really annoying.

Steps to replicate the issue

  1. Start program
  2. Create an uncaught runtime error (this could be for any motive, for testing recommend exit(1)
  3. Terminal breaks

What should happen

  1. Start program
  2. Create an uncaught runtime error (this could be for any motive, for testing recommend exit(1)
  3. Terminal works fine after program exits
Math-42 commented 1 year ago

I think we could create an exit function with all the code that must be executed before the program exits, and we can bind the exit signals to it, this could solve the problem and create an API in case it is necessary to add new functions along with the exit.

vrrusso commented 1 year ago

do you know any mechanism for binding the exit signals to a function call? if you give me the direction I may try to correct this today

FulecoRafa commented 1 year ago

This is compiler dependent. For the gnu compiler, which is what I believe to be the default for this project, that would be a quit or an exit named function. Do not remember the name from the top of my head, but I'm certain it exists.

vrrusso commented 1 year ago

I have replicated the bug with the exit(1) call, but still have doubts on how to handle so uncaught errors are directed to the terminate function.

FulecoRafa commented 1 year ago

Then curses library should have an unit function that prepares the library. On this function, it changes the terminal mode. It should also have a terminate function, which stops the library and reverts the terminal mode. This function should be called whether the program terminates or crashes.

vrrusso commented 1 year ago

https://stackoverflow.com/questions/11753909/clean-up-ncurses-mess-in-terminal-after-a-crash I think this question describes exactly what we need. Apparently one possible solution is to use curses signal handling to handle SIGTERM. I will try out this and open a PR for further tests if it is any good.

jj-marinho commented 1 year ago

It seems like we are mixing up different concepts.

There's already some work done related to signal handling, so for exits here's a brief guide.

These handlers can be simple wrappers that call a code defined finalize_game or ncurses' endwin directly.