-
Notifications
You must be signed in to change notification settings - Fork 4.8k
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
Terminal hardware is always left in application mode (keypad_xmit) after running a console program #27626
Comments
Thank you for the detailed investigation, @kkm000. Do you want to propose a fix in a PR? |
@danmosemsft, a fair question! I do not know, really. If I had the whole thing checked out and churning and able to test, I'd implement and send a fix in an hour. The missing piece looks simple to integrate, would touch only The big deal is I know practically nothing how the whole stack works. I just found this repo by searching for a type named "ConsolePal" that popped up in a stack backtrace that a c# program using Console.ReadKey() so helpfully puked when I invoked it with "</dev/null". It gave me a hint where to look for a low-level library responsible for tty I/O, that's it. The rest was just some common sense and a bit of reading the code. I mean, I could certainly try to check out and build the repo; I routinely develop in C++ on both Windows and Linux and C# on Windows. But I have no idea what would I do with the libraries that I build. I know little what is under the hood of the "dotnet" command. I quickly read the documentation on building an testing, but I do not understand how I could incorporate what I build into the rest of the stack. Do I also need to build the coreclr repo, or can I run an application with my own build of corefx but the existing RTM coreclr runtime installed from a .deb? If you could point me to any docs or explain that in a few words, I would certainly give it a try. |
@kkm000, thanks. Is there a way to detect whether the current setting is already keypad_xmit? It should be fairly straightforward to fix up the uninitialize routine to output keypad_local, but that could itself cause a potential issue if the terminal were already set as keypad_xmit when the process was launched, as we'd then be switching away from what the environment described. |
I do not believe there is, and most probably not without running into another timeout problem like #27034. But it's not really necessary. For all I know, no sane program would assume that the terminal is in the keypad mode when it starts. And I know for sure that bash (rather readline) assumes it is not :( My understanding is that the "local" mode is the default, and it's always ok to revert to it. vim does this unconditionally upon exit (here vim documentation has a little bit on its keypad mode control. |
@stephentoub wrote:
My research into terminfo shows no way to query the setting; I agree with @kkm000 that the standard thing to do in Unix world appears to be to assume that "normal mode" (keypad_local) is the default, and if you set "application mode" (keypad_xmit) because you're a full-screen program that wants the application-mode escapes for cursor keys et al, then you're expected to set "normal mode" (keypad_local) before you exit. I believe this behavior of vim and other similar software was responsible for #16300 (comment), which you fixed in dotnet/corefx#6488. If you want to be extra cautious, you could possibly save the value of |
Is there any progress towards fixing this? |
This bash script detects whether DECCKM is enabled in xterm. It doesn't work in Konsole, though.
|
In fact, if after a .NET console command I do this: The broken behavior is fixed (in my case customizations in Unfortunately I cannot just avoid this by Here's the capture of the default
In case I tried to return to local mode from within the program I would just get:
(1st sequence in 2nd line) Thus, no good. |
@fabriciomurta, no, it's likely not possible to reset the mode properly from within the application. My solution has been just to add the reset sequence to
It is important to add the
I advise against hardcoding handwritten CSI control sequences into PS1: it will come back at you sooner or later, e.g. in Emacs, in tmux or in other situations of double-emulation, where the actual terminal might not support them. The only caveat is that support for RGB terminals in terminfo ("direct" color in their parlance) is flaky, and, e.g, both |
Wherever .NET ( Maybe just signal if it output the "set" sequence, on program shutdown, output the "unset" one. In what I could find, it does not just to keypad mode, but also cursor key mode, so both should be reset: There should be a counter for this setKeypadXmit code. Something to play last when a console application is freeing resources to terminate... |
@fabriciomurta, if you read the whole discussion, you'll note that this has been said before. My impression is that @stephentoub generally agrees (#27626 (comment)), and it's likely that a good-written PR would be accepted. @stephentoub, what's your word on this? To recap, we've already established by eyeballing the sources that both less(1) and vim(1) blindly send terminfo keypad_local = |
Please excuse me if this is a stupid question, but I am new to Terminals: why do we need to enter this mode? What do we get by doing that? |
It allows distinguishing between keys on the main keyboard and keys on the numpad, which System.ConsoleKey does. |
Just linking dotnet/sdk#15243 here as I think these duplicate each other. |
Just bumped into this one after installing .NET 8 on Linux. TLDR; There seems to be a regression in .NET 8 as I never witnessed this behavior in .NET 6... My particular repro combination is:
Any NB:
Shoutout: the PS1 workaround by @kkm000 works in my case. Huge thanks to him as, before applying his trick, all I knew to do was |
For what it's worth, I have NOT experienced a regression under .NET 8 on a real Linux box (Linux Mint 21.3, which is basically Ubuntu 22.04 with a different set of window manager packages, none of which should affect terminal behavior). Running dotnet commands leaves my terminal in a working state, whether they exit normally or are interrupted by Ctrl-C. Here's the first few lines of
I know that .NET SDK 8.0.201 is out, but I don't have it installed yet. If it causes a regression, I'll post another comment. |
@rmunn Interesting, this would then be specific to Windows Terminal? I'll try this on my Linux machine when I have time as well! |
This problem has been annoying me for quite a while, and I worked around it by emitting the
rmkx
terminfo sequence in my bash prompt. But I still think it makes sense to report it. Basically, after (almost?) anydotnet
command that involves CLR console I/O, the terminal emulator is left in the application mode (keypad_xmit
), messing my command line handling (e. g. arrows move by word<ESC> O C
, not by character<ESC> C
, in readline bash prompt, etc. -- probably because of my customized.inputrc
, designed to work with both VT100 and rxvt style emulators). Other programs behave nice; e. g., just typingman man
, or invokingless
orvim
and then exiting them reverts keyboard to local keypad mode. Just for reference, this is a hopelessly headless Ubuntu 18.04 machine:I noticed by using a script(1) dump that the
smkx
akakeypad_xmit
code is emitted more than once, but then there is no revertingrmkx
akakeypad_local
emitted ever. Here is, for example, how captured prologue ofdotnet build
looks (not marking literal linefeeds), that emitskeypad_xmit
twice:I just attempted to trace the issue down by eyeballing the console handling code. My terminal emulator is pretending to be xterm-256color compatible (MobaXTerm), and infocmp(1) does show entries for both sequences for this term type:
The terminfo entries have manifest numeric IDs in `/usr/include/term.h
but the index 88 is not even in the
enum WellKnownStrings
in TermInfo.cs file that reads the terminfo file directly (butKeypadXmit = 89
is). Also, it seems that pal_console.c does a good job of restoring the application mode at a few points by sending thekeypad_xmit
sequence, apparently since the MR dotnet/corefx#6488 fixing the issue #16300, but unless I misunderstand it, never attempts to send a matchingkeypad_local
in any of its Uninitialize() functions, where tty driver attributes are restored.The text was updated successfully, but these errors were encountered: