Skip to content

WaitForSingleObject on Windows isn't sufficient to wait for Process termination. #77

@Mistuke

Description

@Mistuke

Usually when CreateProcess is used then WaitForSingleObject on the handle as is done here is enough to wait for the process (and it's child processes that it may spawn) to terminate.

The problems arise when inside the child process a new process is spawned using _execv or any of the other _exec functions. Inside msvcrt execv is implemented as spawnve (_P_OVERLAY, path, argv, cur_environ ())

_P_OVERLAY is crucial, as it makes the function behave sort of like execv on linux. the child process replaces the original process.

With one major difference because of the difference in process models on Windows: the original process signals the caller that it's done.

This is because a call to CreateProcess is made followed directly by an _exit(0) call. So control is returned to you and you get a success exit code but the child is still running.

This is what I believe is causing hsc2hs to fail on removing an output file:

Failed to remove file ./T3837_hsc_make; error= DeleteFile "./T3837_hsc_make": permission denied (Access is denied.)

And requiring this hack which sorta, sometimes works https://github.com/ghc/hsc2hs/blob/f5ae016e5a69ebf42d612805e51afd9227df9389/Common.hs#L53

The solution for this is to use an I/O Completion port and a Job objection to monitor the process tree and wait for all processes in the created job to finish. I can whip up a patch for this but I wanted to see how you guys wanted this to be done.

Do you want a new API, or modify the existing one to use completion ports to wait instead of WaitForSingleObject. I think the overhead would be very slight, but it would be more correct in the presence of applications that use the POSIX spawn functions.

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