Skip to content

V2: kill child processes on Windows when the parent dies #556

@db4

Description

@db4

@klemens-morgenstern, we already discussed this in #454, but you probably missed my last comment left after the issue was closed. I can't reopen it, so I have opened this new issue to continue the conversation.

Your solution

#include <windows.h> 
#include <boost/process/windows/creation_flag.hpp>

auto h = ::CreateJobObjectW(nullptr, nullptr); // don't forget the CloseHandle


bp::process proc(..., boost::process::windows::create_breakaway_from_job);

AssignProcessToJobObject(h, proc.native_handle()); // error handling...

// now do with the job object `h` what you have above

seems to have a flaw. If the child process manages to create a grandchild process before AssignProcessToJobObject is called, the grandchild won't be killed be the OS on the main process crash. V1 allowed to include the child process into the main process' group before it runs; for v2 it's not possible. Another option is to create the child process suspended, as recommended here: https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433, but to resume it one have to know the main thread handle, that cannot be fetched from bp::process object. So currently there is no solution that works with v2.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions