Win32: Process created in a worker thread, terminates when the worker thread returns. Is it something new or was it from the beginning?

asked 2022-06-04T07:06:13.013+00:00
Peter 1 Reputation point

Main process creates a worker thread. This thread calls CreateProcess() starting a background process. The thread does some work the process and when it finishes returns;
The app wants to keep the background process running to work with it again later.
But the process quits when its starter thread returns.
Is there any possibility of keeping the process running in this model or do I have to use a helper thread for starting the exe and keep it?

Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
1,890 questions
{count} votes

3 answers

Sort by: Most helpful
  1. answered 2022-06-04T07:22:48.827+00:00
    RLWA32 28,646 Reputation points

    If CreateProcess succeeds the new process executes independently of the parent process/thread. The new process should continue running after the creating process/thread terminate. However, it is possible for the new process to encounter problems that result in its termination even if CreateProcess has returned successfully.

    Share your code that reproduces your issue so that we can see what it is doing.

    No comments

  2. answered 2022-06-04T07:49:47.74+00:00
    Peter 1 Reputation point

    Thank you for your respond.
    That was our model too about reality. But something changed. These are pretty old parts of our program.
    The created process does a lot of work for the app worker thread before that thread returns. Worker thread and the background process communicate with each other using memory mapped file and two mutexes (no initial owner, security attributes NULL). It worked for ages, but some weeks ago something changed, there were VS2019 and Win10 updates.
    Sharing the code is difficult because it is large and embedded in the deep.

    Meanwhile I implemented the helper thread pattern. The helper thread creates the process (and waits for the quit signal), other worker threads feed the process with jobs through the mmf/mutexes. And when the main app wants to quit, it signals the helper thread what returns, and the background process terminates. Everything works as expected.
    But so far, the original pattern was working too.
    Is it possible somehow that a created HANDLE (CreateProcess()) life cycle is connected to its creator thread? And the thread returns, those handles close too and that terminates the process.


  3. answered 2022-06-04T11:44:47.61+00:00
    Peter 1 Reputation point

    Thank you. (Ofc the simple repro works as we expected the process does not quit)

    But there is some progress. I found in the old code, in the background/worker process, there is a WaitForMultipleObjects(2, aHandle, 0, infinite) call. It waits for a named Mutext object and a named Event object. Both were created in the main process and opened in the worker process.
    When the worker thread in the main process returns, the mutex becomes WAIT_ABANDONED_0 and the worker process actively exits. (MysteryCounter--) But there is no CloseHandle() call at that time anywhere in the worker process. It is so old that it is strictly single threaded exe. There is no CloseHandle() in the main process either, just the worker thread returns. The thread what has created all mutexes events handles and started/created the worker process.
    And we got the reason: It indicates that the thread that acquired the mutex terminated without explicitly releasing it by calling ReleaseMutex().

    Thanks for your guidance