Skip to content

Processes started on Windows with use_process_jobs=True may be terminated #167

Closed
@bgamari

Description

@bgamari

The current implementation of the use_process_jobs codepath on Windows (introduced in #80) has a few issues, the most significant of which can result in the process being unexpectedly terminated. Specifically, this issue can occur as follows:

  1. the user spawns a process with use_process_jobs=True
  2. the user waits on the ProcessHandle with waitForProcess but holds no other reference to the ProcessHandle
  3. waitForProcess safe-foreign-calls into waitForJobCompletion
  4. waitForJobCompletion blocks with GetQueuedCompletionStatus on the IOCP associated with the job
  5. a major GC occurs, the ProcessHandle is collected since there are no references to it
  6. the finalizer attached to the ProcessHandle's MVar is run, closing the process handles
  7. waitForJobCompletion is unblocked by some event, handles it, and tries blocking again
  8. GetQueuedCompletionStatus fails with an "invalid handle" error since the handle has been closed
  9. waitForJobCompletion sees the failure, breaks out of the loop, and returns with status code 0.
  10. the caller thinks that the process finished successfully and is later surprised when this isn't the case

The reason this happens is that the use_process_jobs codepath, unlike the other codepaths of waitForProcess, makes no attempt to do anything with the process handle (e.g. updating the MVar to be a ClosedHandle) after it finishes waiting.

In addition, the use_process_jobs codepath fails to call endDelegateControlC on process completion, unlike the "normal" codepath.

As an orthogonal matter, the documentation of use_process_jobs could be better; currently we don't adequately explain when a user should be using a job. In addition, we should note that exit codes may be incorrect on Windows when exec is used.

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