You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
System.Diagnostics.Process.Kill(entireProcessTree: true) doesn't kill entire process tree on dotnet Debian images #45017
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
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.
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
Inside of the first app, call Process.Kill(true) to kill the entire process tree.
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.
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
usingSystem;usingSystem.Diagnostics;usingSystem.IO;usingSystem.Linq;namespaceProcessKIllTree{classProgram{staticvoidMain(string[]args){stringcurrentExePath=Process.GetCurrentProcess().MainModule.FileName;intGetChildCount()=>Process.GetProcessesByName(Path.GetFileName(currentExePath)).Count(p =>p.Id!=Process.GetCurrentProcess().Id);if(args.Length==0){varfirstChild=Process.Start(currentExePath,"1");while(GetChildCount()!=2){}// wait till both child processes are startedfirstChild.Kill(entireProcessTree:true);Console.WriteLine($"{GetChildCount()} remained alive.");}elseif(int.TryParse(args[0],outintindex)){if(index==1)Process.Start(currentExePath,"2");// the first child starts one more and that's itwhile(true){}// make sure the child process never quits}}}}
@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?
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.
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.
ghost added
in-prThere is an active PR which will close this issue when it is merged
Activity
eiriktsarpalis commentedon Nov 23, 2020
Out of curiosity, does the issue not reproduce on non-Debian distros? (e.g. when using the Ubuntu or Alpine images?)
jkotalik commentedon Nov 23, 2020
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
adamsitnik commentedon Nov 26, 2020
Triage notes:
adamsitnik commentedon Nov 26, 2020
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
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.jkotalik commentedon Dec 1, 2020
@adamsitnik are those logs inside of a docker-container or no?
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?
jkotalik commentedon Dec 1, 2020
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 runwhile 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.
danmoseley commentedon Dec 2, 2020
cc @bgribaudo (who kindly implemented this originally) in case he has thoughts about that.
eiriktsarpalis commentedon Dec 2, 2020
@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?
jkotalik commentedon Dec 2, 2020
@eiriktsarpalis we setup a meeting tomorrow show the repro to Adam, I believe you were just forwarded the invitation 😄
bgribaudo commentedon Dec 4, 2020
I don't, off the top of my head. I am curious what yesterday's repro meeting found.
eiriktsarpalis commentedon Dec 9, 2020
When running the reproducing repo from an Ubuntu host I can occassionally reproduce the issue with the 5.0 sdk image:
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.
danmoseley commentedon Jan 12, 2021
Presumably investigation here would mean adding some logging and then running until a repro happens.
tmds commentedon Feb 7, 2022
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, otherwisepgrepwould 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
pgrepis that that implementation usesSIGTERM, which means the processes are requested to terminate, and the parent processes are alive to reap their direct children.Process.KillusesSIGKILLand because the parent processes are dead, pid 1 becomes responsible for reaping the zombies.