Skip to content

System.Diagnostics.Process.Kill(entireProcessTree: true) doesn't kill entire process tree on dotnet Debian images #45017

Closed

Description

@jkotalik

Description

When working on a fix for dotnet/aspnetcore#27950, we attempted to use Process.Kill(true) to kill an entire process tree on Unix to fix our dependency on pgrep in dotnet-watch. However, when we tried to use that API, from what we can tell, only the parent process was killed.

I'm currently able to reproduce this behavior with dotnet watch, but here are the steps necessary to try reproing standalone

  1. Create a .NET app which starts a process, which will also start a child process. This could be done through two .NET apps, one which starts the second app, which then calls some long running command line sleep 60.
  2. Run the apps on the debian sdk image. An example command for doing this could look like:
docker run -it --rm --workdir /app -v ${PWD}/:/app mcr.microsoft.com/dotnet/sdk:5.0 dotnet run
  1. Inside of the first app, call Process.Kill(true) to kill the entire process tree.
  2. Check if both processes were killed.

Right now, I only see the parent being killed.

Configuration

.NET 5, running on the mcr.microsoft.com/dotnet/sdk:5.0 image.

Regression?

Not sure.

Activity

  1. added
    untriaged
    on Nov 20, 2020
  2. eiriktsarpalis commented on Nov 23, 2020

    @eiriktsarpalis
    Member

    Out of curiosity, does the issue not reproduce on non-Debian distros? (e.g. when using the Ubuntu or Alpine images?)

  3. jkotalik commented on Nov 23, 2020

    @jkotalik
    ContributorAuthor

    This also reproduces on the Ubuntu and Alpine as well.

    For context, this is what we were doing before to kill all child processes, which worked: https://github.com/dotnet/aspnetcore/blob/master/src/Shared/Process/ProcessExtensions.cs#L34

  4. added and removed
    untriaged
    on Nov 26, 2020
  5. added this to the 6.0.0 milestone on Nov 26, 2020
  6. adamsitnik commented on Nov 26, 2020

    @adamsitnik
    Member

    Triage notes:

    • it's a bug and should definitely be fixed for 6.0
    • we need to check if it reproduces on docker only
    • we need to check if it's a regression compared to 3.1 (this might affect backporting)
  7. adamsitnik commented on Nov 26, 2020

    @adamsitnik
    Member

    I am unable to reproduce it on bare metal Ubuntu box (so it's most likely docker-related issue as @eiriktsarpalis has suggested today)

    Edit: the repro that I've provided below uses a single process (and it's killing it's own child processes) but I've also tried with two independent processes and failed to repro.

    Details
    using System;
    using System.Diagnostics;
    using System.IO;
    using System.Linq;
    
    namespace ProcessKIllTree
    {
        class Program
        {
            static void Main(string[] args)
            {
                string currentExePath = Process.GetCurrentProcess().MainModule.FileName;
                int GetChildCount() => Process.GetProcessesByName(Path.GetFileName(currentExePath)).Count(p => p.Id != Process.GetCurrentProcess().Id);
    
                if (args.Length == 0)
                {
                    var firstChild = Process.Start(currentExePath, "1");
    
                    while (GetChildCount() != 2) { } // wait till both child processes are started
    
                    firstChild.Kill(entireProcessTree: true);
    
                    Console.WriteLine($"{GetChildCount()} remained alive.");
                }
                else if (int.TryParse(args[0], out int index))
                {
                    if (index == 1) Process.Start(currentExePath, "2"); // the first child starts one more and that's it
    
                    while (true) { } // make sure the child process never quits
                }
            }
        }
    }
    dotnet publish -c Release --self-contained -r linux-x64 -f netcoreapp3.1
    bin/Release/netcoreapp3.1/ProcessKIllTree
    0 remained alive.
    dotnet publish -c Release --self-contained -r linux-x64 -f net5.0
    bin/Release/net5.0/ProcessKIllTree
    0 remained alive.
  8. jkotalik commented on Dec 1, 2020

    @jkotalik
    ContributorAuthor

    @adamsitnik are those logs inside of a docker-container or no?

    we need to check if it's a regression compared to 3.1 (this might affect backporting)

    If this isn't a regression and you wouldn't backport to 3.1 or 5.0, we'd need to come up with a custom solution on our side for dotnet-watch. Have we identified where the bug is, s.t. we can start working on a custom solution?

  9. jkotalik commented on Dec 1, 2020

    @jkotalik
    ContributorAuthor

    As an update, the difference in reproing this happens changes when we run dotnet watch with and without a shell.

    For example, the following command doesn't work:
    docker run -it --workdir /app -v ${PWD}/:/app mcr.microsoft.com/dotnet/sdk:5.0 dotnet watch --verbose run

    while this works:
    docker run -it --workdir /app -v ${PWD}/:/app mcr.microsoft.com/dotnet/sdk:5.0 /bin/sh -c "dotnet watch --verbose run"

    Wondering if there is some permission issue or environment variables that could be different when we run inside of a shell or not.

  10. danmoseley commented on Dec 2, 2020

    @danmoseley
    Contributor

    cc @bgribaudo (who kindly implemented this originally) in case he has thoughts about that.

  11. eiriktsarpalis commented on Dec 2, 2020

    @eiriktsarpalis
    Member

    @adamsitnik I was unable to reproduce using your code on docker. Here is the relevant infrastructure. So perhaps something is missing. @jkotalik would you be able to help us here?

  12. jkotalik commented on Dec 2, 2020

    @jkotalik
    ContributorAuthor

    @eiriktsarpalis we setup a meeting tomorrow show the repro to Adam, I believe you were just forwarded the invitation 😄

  13. bgribaudo commented on Dec 4, 2020

    @bgribaudo
    Contributor

    cc @bgribaudo (who kindly implemented this originally) in case he has thoughts about that.

    I don't, off the top of my head. I am curious what yesterday's repro meeting found.

  14. eiriktsarpalis commented on Dec 9, 2020

    @eiriktsarpalis
    Member

    When running the reproducing repo from an Ubuntu host I can occassionally reproduce the issue with the 5.0 sdk image:

    $ make SDK_VERSION=5.0
    /* docker build output */
    docker run process-kill-repro
    1 remained alive.
    

    However I was never able to reproduce using the 3.1 image.

    EDIT: Spoke too soon, I now see the same issue with 3.1. So at least we're not looking at a regression.

  15. danmoseley commented on Jan 12, 2021

    @danmoseley
    Contributor

    Presumably investigation here would mean adding some logging and then running until a repro happens.

  16. modified the milestones: 6.0.0, 7.0.0 on Jul 26, 2021
  17. tmds commented on Feb 7, 2022

    @tmds
    Member

    There are two causes for 'unkilled' children:

    One is that Kill(entireProcessTree: true) cannot identify a process as being part of the tree. This happens when a parent process dies before its children. Then pid 1 becomes the new parent of that child, and the tree relationship is lost.

    The second reason is that pid 1 is responsible for reaping any orphaned children, and .NET only reaps its direct children that were started using Process.Start.
    This is to allow other code in the process to start and reap its own child processes.
    The orphaned children remain as zombie processes.

    Kill(entireProcessTree: true) ordering kills from child to parent. I assume all parents remain alive, otherwise pgrep would also have an issue finding all children. So the processes that remain here are the unreaped zombies.

    We could extend the logic also reap unknown children when .NET runs as pid 1.

    The reason these zombies don't occur with pgrep is that that implementation uses SIGTERM, which means the processes are requested to terminate, and the parent processes are alive to reap their direct children.
    Process.Kill uses SIGKILL and because the parent processes are dead, pid 1 becomes responsible for reaping the zombies.

  18. ghost added
    in-pr
    on Feb 9, 2022
  19. ghost removed
    in-pr
    on Feb 17, 2022
  20. ghost added
    in-pr
    on Feb 17, 2022
  21. ghost removed
    in-pr
    on Feb 17, 2022
  22. ghost locked as resolved and limited conversation to collaborators on Mar 19, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions