Skip to content

termination on ctrl + break #4254

Closed
Closed

Description

@mwrock

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:

  1. 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 run ping 8.8.8.8 -t, ctrl+c does nothing.

  2. 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

  1. added
    Issue-Question
    on Jul 17, 2017
  2. mwrock commented on Jul 17, 2017

    @mwrock
    ContributorAuthor

    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 GenerateConsoleCtrlEvent after 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 to GenerateConsoleCtrlEvent that 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 SetConsoleCtrlHandler and altering powershell's ctrl+c handler state. I could override powershell's ConsoleControl.BreakHandler but I cant access the internal SpinUpBreakHandlerThread which 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.DebuggerEnabled is $false or if powershell is in -NonInteractive mode?

  3. PaulHigin commented on Jul 17, 2017

    @PaulHigin
    Contributor

    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.

  4. SteveL-MSFT commented on Jul 17, 2017

    @SteveL-MSFT
    Member

    Matt Wrock (@mwrock) the change your proposing seems reasonable to me

  5. mwrock commented on Jul 18, 2017

    @mwrock
    ContributorAuthor

    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.

  6. lzybkr commented on Jul 18, 2017

    @lzybkr
    Contributor

    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.

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

    Issue-QuestionResolution-FixedWG-Interactive-Console

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions