Skip to content

Reset terminal at prompt to clean up after bad termination of certain scripts #4873

Closed

Description

@mqudsi

Not sure what exactly is the cause, but many cli apps that use escape codes to present a console ui (not sure if curses only or in general) can crash/exit at a point that will break input, for example, forcing fish_config and descendents to quit while w3m is running results in breakage of the up/down/right/left arrow keys (pressing up results in [A posted to the console, down in [B, etc.)

tput init does not help.

Activity

  1. krader1961 commented on Apr 2, 2018

    @krader1961
    Contributor

    The example you cite (arrow keys not working) is usually because the app enabled alternate keypad mode (sometimes known as application keypad mode). The tput init command typically includes the sequence \e> to enable numeric keypad mode. See https://vt100.net/docs/vt100-ug/chapter3.html. Whether fish should send the terminfo is or init sequence (which is what tput init emits) before every prompt is debatable. Neither bash or zsh do so. Of course you can argue that fish should do so because ensuring the terminal is in a known state is friendlier but the concern I would have involves possible unwanted side-effects and the cost of including that string with every prompt.

  2. mqudsi commented on Apr 3, 2018

    @mqudsi
    ContributorAuthor

    Hmmm, tput init didn't help for me in that case (and in the past) with xterm-256color as my TERM with the termcap from ncurses 5.9 (2014-07-26).

    The problem is that sometimes (esp. when breaking fish_prompt while testing) ctrl+c, del, and ctrl+h/bkspc becomes uninterpretable so that after a typo there is no option but to kill the process out-of-band (since I can no longer type in exit). Obviously that particular case (fish dev) is an edge case, but other such escaping issues exist.

  3. krader1961 commented on Apr 3, 2018

    @krader1961
    Contributor

    @mqudsi, I missed the "does not help" part of your original comment. Nonetheless, what you're describing in your second comment implies that fish is not setting the tty driver mode to a sane value after running an external command. Something that should be impossible (at least as of 2017-09-08 when I stopped tracking the master branch). Also, whatever escape sequences the application has sent to the terminal (e.g., to enable alternate keypad mode) can't possibly affect how fish handles things like [ctrl-C] or [backspace]. What the app does to the tty driver mode can if fish is no longer setting the tty driver mode to a sane value after an external command.

    Is this on a UNIX like platform? That is, not Cygwin or WSL? If so execute tty before recreating the problem to capture the tty name. Capture the tty mode by running stty -a < $tty where $tty is the tty name you captured. Then recreate the problem and run, in a different terminal on the same system, stty -a < $tty. Are they the same? They should be identical.

    Also, when you say

    ... so that after a typo there is no option but to kill the process...

    it seems to me the need to kill the fish process is irrelevant. The fact it no longer responds to something as basic as the interrupt character (normally [ctrl-c]) is all we need to know. So I'm wondering if I've misunderstood what you're saying.

  4. zanchey commented on Apr 3, 2018

    @zanchey
    Member

    Does stty sane help?

  5. krader1961 commented on Apr 3, 2018

    @krader1961
    Contributor

    Note that if the problem is such that stty sane fixes it then you'll probably need to press [ctrl-j] rather than [enter] to run the command. And if that does fix the problem it means fish is no longer ensuring the tty modes are sane after an external command is run.

  6. mqudsi commented on Apr 4, 2018

    @mqudsi
    ContributorAuthor

    @zanchey I'll have to work up a minimal reproduction case so I can test the stty sane approach, but essentially yes, fish is (no longer?) ensuring a sane tty after external commands.

    @krader1961 The reason I didn't feel that the lack of response to ctrl+c was enough is because it isn't so much fish ignoring sigint (which I should test via an external kill -SIGINT next time) as it was that ctrl+c was no longer mapped to sigint.

    Depending on the repro, sometimes enter still works and sometimes it doesn't (it did this last time, but when I broke fish_prompt it wouldn't any more). I'll try ctrl+j next time that happens, too, thanks for the tip, @krader1961 .

  7. mqudsi commented on Jun 1, 2018

    @mqudsi
    ContributorAuthor

    I'm able to reproduce this now on kernel vtty (not x11 or ssh).

    #include <SDL.h>
    int main(int argc, char * argv[]) {
        SDL_Init(SDL_INIT_VIDEO);
        SDL_SetVideoMode(500, 500, 32, SDL_DOUBLEBUF | SDL_OPENGL); // should fail
        return 1; //without calling SDL_Quit();
    }

    Executing the resulting binary causes the console to act up (input is no longer process correctly) under FreeBSD 12 from the login terminal. Cannot switch sessions, exit fish, or do anything useful.

    ssh into the machine and kill the fish login session and everything goes back to normal.

  8. faho commented on Jun 3, 2018

    @faho
    Member

    @mqudsi: Are you sure that's about terminal modes getting screwed up?

    There's a couple more things that could have happened there - did fish actually gain back control? Or did the display get frozen?

    I actually think this being a problem with tty modes is fairly unlikely because fish is already resetting them.

  9. mqudsi commented on Jun 4, 2018

    @mqudsi
    ContributorAuthor

    No, fish gets back control and I can interact with fish, it's just input is scrambled.

  10. faho commented on Jun 4, 2018

    @faho
    Member

    How is it scrambled? Does pressing ctrl-j or ctrl-m execute stufff? As in, did you try stty sane<ctrl-j>, as was mentioned earlier in this issue?

    The weird part here is that fish is already supposed to restore the modes (https://github.com/fish-shell/fish-shell/blob/master/src/reader.cpp#L349-L363)!

    Is that not happening? Is there something missing? Is there a race?

  11. faho commented on Nov 30, 2018

    @faho
    Member

    @mqudsi: Does this still happen for you?

    I've not actually ever seen this with fish.

  12. mqudsi commented on Dec 1, 2018

    @mqudsi
    ContributorAuthor

    I haven't experienced it in the past couple of months, but I haven't been working on anything that manipulates the terminal too much. Note that I already posted test code above which reproduces it 100% of the time, I could try that again if need be.

    I don't think it's a race condition because running a subsequent command should trigger the code again and make it work, and that wasn't the case for me.

    I imagine there's some state that we aren't saving/restoring? Or maybe it's an issue with the terminal (or terminal emulator) itself?

    @krader1961 I'm thinking now that there's no way stty sane could actually ever work because we deliberately break stty, don't we? #2315

  13. faho commented on Dec 1, 2018

    @faho
    Member

    I imagine there's some state that we aren't saving/restoring?

    stty -a before and after should show that.

    I'm thinking now that there's no way stty sane could actually ever work because we deliberately break stty, don't we?

    We restore the modes. So if we're missing any, stty sane should still affect those.

  14. faho commented on Jan 26, 2019

    @faho
    Member

    Nobody has experienced this in a couple of months, let's close it for now!

  15. 14 remaining items

  16. reopened this on Jan 18, 2021
  17. mqudsi commented on Jan 18, 2021

    @mqudsi
    ContributorAuthor

    I think so?

    (I re-opened because the issue described with neovim is different)

  18. faho commented on Jan 18, 2021

    @faho
    Member

    (I re-opened because the issue described with neovim is different)

    Okay, if I understand correctly your issue is:

    any mouse clicks put garbage to the screen

    Garbage like
    Screenshot_20210118_213128

    ?

    If so, neovim turned mouse reporting on and didn't turn it off. Fish does not do any mouse handling by itself, and won't currently turn mouse reporting on or off.

    The solution is echo -e "\e[?1000l" (echo -e "\e[?1000h" turns it on), but I don't know when that's accepted, I'm not particularly in the mood to do yet more terminal feature detection (can we get a better replacement for terminfo already), and this doesn't seem to be a common problem so I don't feel like handling it is worth it. If neovim crashed, just start neovim again and exit it cleanly, or start a new tab.

  19. mqudsi commented on Jan 18, 2021

    @mqudsi
    ContributorAuthor

    I don't think this needs any terminal feature detection? If fish doesn't use mouse detection, fish can (should?) just keep it off and any application that needs application-level mouse detection will be turn it back on as needed. I can't think of any apps that act differently (intentionally) based on whether or not the invoking shell has enabled mouse support. It would just have to be one of those states that is saved alongside a background process' state so that it is restored along with it but otherwise disabled.

  20. faho commented on Jan 18, 2021

    @faho
    Member

    f fish doesn't use mouse detection, fish can (should?) just keep it off and any application that needs application-level mouse detection will be turn it back on as needed

    You misunderstand. Fish doesn't need it, but neovim turned it on, and because it crashed did not turn it off!

    And because turning it off involves sending an escape sequence, it needs feature detection.

  21. faho commented on Jan 19, 2021

    @faho
    Member

    Okay, the original issue was fixed, so I am closing this. As for the secondary issue with the mouse reporting, I'll refer to #4918. Turning mouse reporting off requires detecting if mouse reporting is supported in the first place, which is only really something that makes sense in the context of adding support for mouse reporting.

    Any other ways to break the terminal (e.g. iTerm might be confused about which host the user is currently operating on) also require fish to know about these escapes. They should also be handled in separate issues.

  22. added 3 commits that reference this issue on Feb 6, 2021
    1fd73b2
    89f3324
    eecc223
  23. added this to the fish 3.2.0 milestone on Feb 7, 2021
  24. mqudsi commented on Feb 23, 2021

    @mqudsi
    ContributorAuthor

    All issues in this thread have now been resolved, with the mouse-related issues being addressed in mqudsi@eecc223 and subsequent commits.

  25. locked as resolved and limited conversation to collaborators on Aug 22, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions