Kill child process on crash



  • Is there a way to ensure that child processes created with QProcess are killed if the main process dies?



  • You could subclass QProcess and give it a new destructor. In the destructor, you check if the process is still running, and if it is, invoke QProcess::kill() on it.

    It all depends on how your main process died though. If the process suddenly terminates, there is no way to guarantee that destructor code will run at all. I don't know if there are other ways.



  • I am trying to address the situation where my main process crashes.



  • I guess this is question about Windows platform. On Unixes if you kill QProcess then also all its children will die. On Windows if you kill QProcess - its children keep living. I had to implement custom code using WinAPI to kill also all nested children in such case for win32 platform to have same behavior as on Unixes.



  • yshurik, would you mind telling us how you did that? Did you integrate it into QProcess, or did you use some other method?



  • Well, I can paste such code, but it is up to you to find correct place for invoking it - I had little different case:

    Invoke:
    @
    @

    Code to kill nested children
    @
    @



  • [quote author="Andre" date="1305291854"]yshurik, would you mind telling us how you did that? Did you integrate it into QProcess, or did you use some other method?[/quote]

    It was in my class - inherited from QProcess



  • Thanks for sharing!



  • I am running windows, mac and linux and none of the platforms seems to kill the child process. I call it a "child process" only because it is started using QProcess from my main process. Could it be that I miss a flag or something to actually make it a "nested child" process?



  • The integration does not help, if the master process dies.

    It depends, how it dies, whether you can do some stuff or not.
    If it is by exception (perhaps structured exception), they can be caught and the cleanup can be done.



  • Well... you could of course create some kind of monitoring process to do the cleanup for you in case the main process dies, but I think that is overkill.

    It would be way better to avoid the crash in the first place ;-)



  • [quote author="Andre" date="1305296276"]Well... you could of course create some kind of monitoring process to do the cleanup for you in case the main process dies, but I think that is overkill. [/quote]

    You mean creating a process that check the processes state to cleanup children... ok, also a solution. Here in our software we such stuff but it's a huge product with partly customer apps also running as childs...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.