Repository navigation
Description
I'm working on an application that launches powershell scripts (using powershell core) to setup and launch processes. I want to be able to send ctrl+break or ctrl+c signals to these powershell processes and their child processes to cleanly shut them down.
When I create the powershell process I set CREATE_NEW_PROCESS_GROUP which implicitly calls SetConsoleCtrlHandler(NULL,TRUE) which effectively turns off ctrl+c for the created process and its children. So when stopping my launched powershell processes, I call GenerateConsoleCtrlEvent and send the powershell's process group a ctrl+break. While this effectively closes child processes of the powershell process, it puts the powershell process into debug mode which is undesirable.
I have tried 2 strategies to work around this both fail in different ways:
-
In the powershell process I launch, I call
SetConsoleCtrlHandler(NULL,FALSE)to turn ctrl+c back on and have my application generate ctrl+c events. This works perfectly the first time. However, oddly, the parent console now no longer seems to propogate ctrl+c to child processes. For example, if I run my app and start some ps processes and succesfully stop them with ctrl+c events and then terminate my app and THEN runping 8.8.8.8 -t, ctrl+c does nothing. -
I register a new handler to capture BREAK events in the powershell process in hopes this will override the powershell behavior that enters debug mode. However, when this handler is called, even if I have it do absolutely nothing, via a ctrl+break signal, an access violation is thrown in the powershell process.
Is there a clean way to disable the debug behavior invoked from ctrl+break or is there any thoughts why ctrl+c signals do not get propogated if I turn that handler on?
Activity
mwrock commentedon Jul 17, 2017
I've done a bit more troubleshooting and research here.
I have nailed down why the first workaround above is failing. Watching process explorer while my ctrl+c was being swallowed, I found that conhost had some orphaned handles and closing those handles via process explorer magically "woke up" ctrl+c. Why those handles are not being closed is a mystery but I also discovered that firing
GenerateConsoleCtrlEventafter a user invoked ctrl+c is what puts things into this state. Of course if a user enters ctrl+c, the signal is sent to all processes in the console so the call toGenerateConsoleCtrlEventthat my application makes directed to a specific process group is unnecesary. I can't find any documentation verifying this but it certainly appears to be the case.Ideally, I'd still be able to send a ctrl+break to powershell's process group because that would save me from messing with
SetConsoleCtrlHandlerand altering powershell's ctrl+c handler state. I could override powershell'sConsoleControl.BreakHandlerbut I cant access the internalSpinUpBreakHandlerThreadwhich is the default ctrl+c behavior and what I ideally want.Would the powershell maintainers be open to a PR that performs "ctrl+c - like " behavior on ctrl + break IF
$host.DebuggerEnabledis $false or if powershell is in-NonInteractivemode?PaulHigin commentedon Jul 17, 2017
The Ctrl+Break -> drop into debugger function was added to make Ctrl+Break more useful with interactive sessions. So I am fine to revert back to the original the stop-process behavior for non-interactive modes or if the debugger is disabled.
SteveL-MSFT commentedon Jul 17, 2017
Matt Wrock (@mwrock) the change your proposing seems reasonable to me
mwrock commentedon Jul 18, 2017
Great! BTW: I can totally see how the debugger functionality is an awesome feature for interactive scripts.. I'll get a PR in soon for the non interactive experience.
lzybkr commentedon Jul 18, 2017
The design to break into the debugger is especially useful when a script is taking much longer than expected, and you typically have not turned on debugging in such situations.
executor: fix Windows blocking on pipe close