asomers / gstat-rs

An enhanced version of FreeBSD's gstat(8) utility
BSD 2-Clause "Simplified" License
14 stars 0 forks source link

TTY state not correctly restored on exit #28

Closed stesser closed 8 months ago

stesser commented 8 months ago

After exiting from gstat-rs by pressing "q", the TTY state is often not correctly restored:

The output of "stty" (command not echoed on entry) is:

speed 9600 baud;
                lflags: -icanon -isig -iexten -echo echoke echoctl
                                                                  iflags: -icrnl -ixon -imaxbel ignbrk -brkint
                                                                                                              oflags: -opost tab0
                                                                                                                                 cflags: cs8 -parenb

The normal stty outoput is:

# stty
speed 9600 baud;
lflags: echoe echoke echoctl
oflags: tab0
cflags: cs8 -parenb

I'm using bash in a SSH session ...

asomers commented 8 months ago

Somewhat surprisingly, this seems to be bash-related. I can reproduce it if my primary login shell is bash, but not if it's fish (which I usually use). If I login with fish and then invoke bash, I cannot reproduce it.

asomers commented 8 months ago

I've bisected the bug to 2da291acf8b41d5a96332b2450286872e444a787 .

stesser commented 8 months ago

I'd guess that fish performs a reset of TTY parameters to values suitable for interactive use, while bash does not?

Maybe using crossterm instead of termion needs some additional uninit() code?

And I'd think that output tty settings should not be modified at all when output is not to a TTY (but to a file or pipe). I can see that you may want "-icanon" to read single character commands, though (but then exit could also be caused by a signal or the output having been closed).

asomers commented 8 months ago

gstat-rs isn't intended for non-interactive use. For that, you should use iostat. And what does "-icanon" refer to? Are those command line flags for something?

stesser commented 8 months ago

The "-icanon" came from the stty output (ICANON bit is cleared in the c_lflags element of struct termios). This setting disables any input processing or buffering of TTY input.

With regard to only interactive use of gstat-rs: I'd expect most uses to be interactive, but "gstat -B" supports logging of performance data in batch mode, and I consider this a valid use case, too (and actually use it at times ...)