mirror of
https://github.com/rivo/tview.git
synced 2026-04-27 13:55:51 +03:00
[GH-ISSUE #1017] App becomes unresponsive sometimes if something inside panics #737
Labels
No milestone
No project
No assignees
1 participant
Notifications
Due date
No due date set.
Dependencies
No dependencies set.
Reference
starred/tview#737
Loading…
Add table
Add a link
Reference in a new issue
No description provided.
Delete branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @freak12techno on GitHub (Aug 11, 2024).
Original GitHub issue: https://github.com/rivo/tview/issues/1017
So I am building a tool called tmtop, and sometimes it may panic (that's another topic why). Problem is, once it panics, the terminal becomes unresponsive, like what you can see on a screen, and the terminal tab becomes that much messed up so it's easier to close it and open a new one.

On the other hand, if it panics before
app.Run()is called, its output is not messed up. I assume the app somehow fails to close properly or fails to render it back to usable state.Here's how I start it:
How can I fix this so the terminal tab would fallback to the usable state and the panic stacktrace would be displayed properly?
@kivattt commented on GitHub (Aug 12, 2024):
I work around this issue by piping the stderr output into a file, like
that way i can read the stacktrace by looking at the file
@freak12techno commented on GitHub (Aug 12, 2024):
@kivattt yeah, but it's ugly for me as an app developer to ask the users of my app to use this approach to avoid their terminal tab getting messed up. I'd rather do something in my app so it'd quit properly to avoid that, if it's possible.
@rivo commented on GitHub (Aug 13, 2024):
It's unfortunate that you didn't post any code that we can run ourselves to reproduce this. Generally, panicking while your application is running is not a problem. It doesn't lead to the effect you're seeing. This is because panics are recovered here:
github.com/rivo/tview@e4c497cc59/application.go (L287)Here's an example:
This will lead to a properly formatted stack trace:
If you panic after
Run()returns, as your code above suggests, that's also not a problem. See here:Again, properly formatted stack trace:
In both cases, the screen is "finalized" (see here for details) before the stack trace is printed.
So I'm guessing that you're panicking in a separate goroutine, before
Run()returns. In that case, yes, the terminal will still be set up fortcell's screen rendering and simply printing out a stack trace to stdout/stderr will lead to the issues you're seeing.You don't want to do that. Be sure to exit your application first before causing a panic or ensure that the panic occurs in the main thread instead of a goroutine.
@freak12techno commented on GitHub (Aug 13, 2024):
@rivo okay, I managed to write a minimal example that reproduces it:
so basically yes. I need to do a bunch of queries in my app asynchronously, and sometimes it might panic, if for example the response is malformed or something similar. How can I make it panic properly so it won't mess up my terminal with this?
@rivo commented on GitHub (Aug 14, 2024):
What I wrote before:
Or don't panic at all. (Which I think is generally good advice for Go anyway.)
If it's absolutely unavoidable, catch the panic, then stop the application, then panic again. Basically, again, "...exit your application first before causing a panic".
In general, you cannot write to stdout or stderr while a
tviewapplication is running. Well, you can, but it will look ugly.@freak12techno commented on GitHub (Aug 15, 2024):
@rivo gotcha. I suggest adding this info you outlined above to the docs/wiki (unless I overlooked it if it's already present), so others would know about it in advance, and I basically got my answer so I think you can close this issue as there's no action points to be done (except documenting it). Thanks!
@freak12techno commented on GitHub (Aug 26, 2024):
@rivo one more case I found out, somewhat related to the one outlined in the issue. If I run the app in an ssh session, and somehow it disconnects, it doesn't "finalize" the screen so the terminal tab becomes a mess. Do you think there's something that can be done to avoid it?
@rivo commented on GitHub (Aug 28, 2024):
I don't think so. To my understanding, the terminal is set to some special mode which allows us to draw characters anywhere on screen. If the application doesn't terminate orderly, it can't reset the terminal to normal mode again.
In any case, this is all a topic for
tcell: https://github.com/gdamore/tcell If you want to know the details about how this works or what can be done, please ask there.