Skip to content

Terminate child processes when killing a subprocesses #2080

@ahoppen

Description

@ahoppen

When we send a SIGKILL to a subprocess, it only terminates that process and not its child processes. Similarly, on Windows, calling TerminateProcess only terminates that process and not its children.

When we kill a subprocess from SourceKit-LSP, we want to terminate all of its children as well, to make sure that we don’t leave orphan processes behind. Changing this would require:

  • On Posix platforms: Ensuring that the process that we launch does not start new process groups for its child processes. In general, we can’t make this guarantee, but for SwiftPM, we should be able to achieve this by setting startNewProcessGroup: false for all processes that it launches (or changing the default in TSC). While at it, we should also change SourceKit-LSP to pass startNewProcessGroup: false for all subprocesses it launches, so that these subprocesses get killed if SourceKit-LSP is killed. This does not cover the processes launched through Foundation.Process, in particular the ones launched from JSONRPCConnection.
  • On Windows: We need to create a Job Object when creating the subprocesses so that we can terminate their job (which includes the process and all of its children). This would require breaking behavior changes in swift-corelibs-foundation or a custom Windows process implementation in TSC. Details in https://devblogs.microsoft.com/oldnewthing/20131209-00/?p=2433.

Alternatively, the Subprocess type proposed in Foundation seems to not start new process groups and might eventually be a solution for Posix platforms. However, it doesn’t have any mechanism to terminate an entire process tree on Windows. I raised this in the review thread.

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