question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Add "ConsoleBouncer" option, to terminate stragglers (to fix ctrl+c)

See original GitHub issue

Description of the new feature/enhancement

Ctrl+c cancellation does not work really well on Windows. Each process attached to a console receives a ctrl+c signal (as opposed to just the active shell), and sometimes, when a shell has launched some large tree of child processes (imagine a build system, for example), some processes do not exit (perhaps due to races between process creation and console attachment), leaving multiple processes all concurrently trying to consume console input, which Does Not Work Well™. It’s usually not too bad when cmd.exe is your shell, because you can just keep mashing on <kbd>ctrl</kbd>+<kbd>c</kbd> and usually get back to a usable state. But it’s considerably worse in PowerShell, because PSReadLine temporarily disables ctrl+c signals when waiting at the prompt, which can lead to the console being completely unrecoverable.

In the ConsoleBouncer project, I sought to solve the problem with a PowerShell module. When loaded, the module installs a ctrl+c handler, and takes inventory of processes currently attached to the console; these are the “allowed processes”. When a ctrl+c signal is received, all processes not in the “allowed list” (if any) are terminated.

This approach has some weaknesses. For one, it assumes that <kbd>ctrl</kbd>+<kbd>c</kbd> means “kill everything and get back to the shell”, but that assumption is not always true. The Windows console debugger processes kd.exe and cdb.exe, for example, handle <kbd>ctrl</kbd>+<kbd>c</kbd> (interpreting it as a signal to break into the target). So ConsoleBouncer is perhaps mostly-good-enough for most people who need it, probably, but is definitely too kludgy for “everyone”. But PSReadLine could implement something similar that would actually solve the problem more directly.

The defining characteristic of a borked shell is that in its mind, it is just sitting at the prompt, waiting for user input, where at the same time, there are other processes attached to the console, also consuming input (leading to wild and unpredictable behavior, as different keystrokes go to different processes). The ConsoleBouncer module works by receiving a ctrl+c signal, and it does not know what PSReadLine in the shell (or anybody else) is up to. It assumes that ctrl+c means “everybody out!” (And it’s that assumption that is “mostly good enough”, but not really completely true.)

But if the solution were baked into PSReadLine (as an off-by-default option), it could be done differently: instead of triggering the “clear out the riff-raff” behavior upon receipt of a ctrl+c signal, it should only kick out loiterers right before displaying the next prompt. I.e. when it believes it is ready to just sit around and wait for user input (right before it calls SetConsoleMode to disable ctrl+c), it could just take steps to make sure that that user input is going to be able to come to it (whack the non-allowed PIDs).

Proposed technical implementation details

Add a new option (off by default), named “???” (TerminateStragglers?). When enabled, PSReadLine will capture the list of processes currently attached to the console (via GetConsoleProcessList). Before displaying the prompt, it will check the attached processes again, and terminate any that are not in the list of allowed processes.

Issue Analytics

  • State:closed
  • Created 2 months ago
  • Comments:16 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
jazzdelightsmecommented, Aug 1, 2023

@StevenBucher98 @daxian-dbw already done! 😄

1reaction
jazzdelightsmecommented, Jul 31, 2023

It will be a switch parameter on the Set-PSReadLineOption cmdlet. Since it’s off by default, I don’t think there’s a need for an experimental feature to gate it.

Yes, “stragglers” are processes… but I’m not sure if just adding “processes” into the name really makes things much clearer. “Stragglers” in this context are non-GUI, console-attached grandchildren processes; or put another way, non-GUI grandchild processes that are still attached to the console when it’s time for the shell to start reading input again (back at the prompt). I think “TerminateOrphanedConsoleApps” captures that pretty well… thanks, @sdwheeler!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Registering a Control Handler Function - Windows Console
When a CTRL+C signal is received, the control handler returns TRUE, ... handler will be used, resulting in the process being terminated.
Read more >
How do I trap Ctrl+C (SIGINT) in a C# console app?
This works but it doesn't trap the closing of the window with the X. See my complete solution below. works with kill as...
Read more >
What to do when Ctrl-C won't kill running job?
Ctrl + C : politely ask the process to shut down now; Ctrl + \ : mercilessly kill the process that is currently...
Read more >
change bash shortcut keys such as Ctrl-C? - linux
Ctrl - Shift - C (or even better, Super - C ) to terminate the command. Ctrl - Shift - Z (or Super...
Read more >
Ctrl+A CTRL+C and CTRL+V Not Working in Windows 10 (3 ...
The copy-paste feature using the Ctrl + C / Ctrl + V commands is one of the most ... to send the keystrokes...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found