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

Cleaning up after tcell on panic #147

Closed
1egoman opened this issue Apr 26, 2017 · 3 comments
Closed

Cleaning up after tcell on panic #147

1egoman opened this issue Apr 26, 2017 · 3 comments

Comments

@1egoman
Copy link

1egoman commented Apr 26, 2017

Consider the below code:

package main

import (
	"time"
	"github.com/gdamore/tcell"
)

func main() {
	s, _ := tcell.NewScreen()
	s.Init()
	defer s.Fini()

	go func() {
		panic("foo")
	}()

	time.Sleep(1000 * time.Millisecond)
}

When run:

screen shot 2017-04-26 at 7 55 00 am

Because the panic is in a goroutine, the deferred s.Fini call on line 11 is never executed. However, if the call to s.Fini is never made, then the terminal is left in a funky state following the run of the program (see above).

Other than adding a defer s.Fini() in all goroutines in a whole application, how can I successfully clean up after tcell when any goroutine panics?

@gdamore
Copy link
Owner

gdamore commented May 1, 2017

I don't think there is a good solution to this. You have to run some code to send the terminal handling ioctls, and I have no idea how to do that other than a defer or explicitly running the Fini code.

@gdamore
Copy link
Owner

gdamore commented May 1, 2017

We need the logical equivalent of atexit() I guess, but with a panic(), you're already aborting in an unusual manner, so even atexit() is not necessarily gonna do the trick. I discourage the use of panic() for "ordinary" cases -- this function should only be used when some critical programming error has been found.

@gdamore
Copy link
Owner

gdamore commented May 24, 2017

I'm gonna close this. Its not really something we can fix. Restoring terminal settings requires us to have code that runs -- and really there isn't a better way to do this than to arrange for the defer s.Fini() call.

You should try hard not to panic(). If you're encountering panic(), then you need to fix whatever bug is causing a disorderly crash, instead of trying to make the panic() situation "nicer".

@gdamore gdamore closed this as completed May 24, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants