Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Child process lingers after keyboard interrupt on Windows

by toughy (Initiate)
on Oct 10, 2018 at 23:01 UTC ( #1223837=perlquestion: print w/replies, xml ) Need Help??
toughy has asked for the wisdom of the Perl Monks concerning the following question:


I am trying to make a script to automate our project build process and I run into a surprising issue:

With either of open('-|'), IPC::Open3 or IPC::Cmd::run, the child process lingers still alive after a keyboard interrupt (^C) or a timeout alarm from IPC::Cmd. The main script receives the signal and exits as expected, but the build process is still running in the background and visible in Task Manager window.

Contrast this with a call to system() which does The Right Thing: keyboard interrupt will stop the child process, and only after that will the parent process by interrupted.

Can I fix this somehow please ?

I would like to run a couple of my build commands in parallel, and ensure they always terminate before the main script, especially in case of errors, abort, interrupt, other signals like TERM, HUP, QUIT...

Is this possible please ?

Issue not present on Linux or with perl6 Proc class

  • Comment on Child process lingers after keyboard interrupt on Windows

Replies are listed 'Best First'.
Re: Child process lingers after keyboard interrupt on Windows
by davido (Cardinal) on Oct 11, 2018 at 06:18 UTC

    Proc::Simple lets you instantiate forks that are managed by objects. You can set kill_on_destroy, causing a signal to be sent to the child when the managing object falls out of scope. It's only about 330 lines of code excluding comments and POD, and deals with tricky timing, zombies, and output redirection. So if you are unable to use external modules (that's another topic), you might just study it to determine what it does that you're not doing.

    It came in handy for me on a recent project where I didn't want to invest a lot of time into reinventing the functionality that is provided on CPAN already.


Re: Child process lingers after keyboard interrupt on Windows
by BrowserUk (Pope) on Oct 11, 2018 at 01:03 UTC

    The difference is the flags used on the CreateProcess() call underlying the different methods of creating a child process. You cannot change them from your application.

    The simplest way for you to ensure your child processes are cleaned up if your parent is interupted is to install a signal handler for each possible interupt signal and use kill to forward the interrupt before allowing the parent to die.

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority". The enemy of (IT) success is complexity.
    In the absence of evidence, opinion is indistinguishable from prejudice. Suck that fhit

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1223837]
Approved by Paladin
Front-paged by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (7)
As of 2018-10-17 15:43 GMT
Find Nodes?
    Voting Booth?
    When I need money for a bigger acquisition, I usually ...

    Results (96 votes). Check out past polls.