Skip to content

Process.Unix: reap all processes when running as pid 1. - #65061

Merged
adamsitnik merged 2 commits into
dotnet:mainfrom
tmds:reap_pid1
Feb 17, 2022
Merged

adamsitnik merged 2 commits into
dotnet:mainfrom
tmds:reap_pid1

Conversation

@tmds

@tmds tmds commented Feb 9, 2022

Copy link
Copy Markdown
Member

pid 1 (the init daemon) is responsible for reaping orphaned children.
Because containers usually don't have an init daemon .NET may be pid 1.

Fixes #45017.

@adamsitnik ptal.

pid 1 (the init daemon) is responsible for reaping orphaned children.
Because containers usually don't have an init daemon .NET may be pid 1.
@ghost

ghost commented Feb 9, 2022

Copy link
Copy Markdown

I couldn't figure out the best area label to add to this PR. If you have write-permissions please help me learn by adding exactly one area label.

@ghost ghost added the community-contribution Indicates that the PR has been added by a community member label Feb 9, 2022
@tmds

tmds commented Feb 9, 2022

Copy link
Copy Markdown
Member Author

I'll manually verify this, and share the results here.

@ghost

ghost commented Feb 9, 2022

Copy link
Copy Markdown

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

Issue Details

pid 1 (the init daemon) is responsible for reaping orphaned children.
Because containers usually don't have an init daemon .NET may be pid 1.

Fixes #45017.

@adamsitnik ptal.

Author: tmds
Assignees: -
Labels:

area-System.Diagnostics.Process, community-contribution

Milestone: -

@tmds

tmds commented Feb 14, 2022

Copy link
Copy Markdown
Member Author

I used the following test app in a container where .NET runs as pid 1.
(ExecFunction is from https://github.com/tmds/Tmds.ExecFunction with small modification to support corerun.)

if (ExecFunction.IsExecFunctionCommand(args))
{
    return ExecFunction.Program.Main(args);
}
else
{
    while (true)
    {
        var process = ExecFunction.Start(
            () =>
            {
                for (int i = 0; i < 10; i++)
                {
                    ExecFunction.Start(() =>
                    {
                        Console.WriteLine("Grand-child launched.");
                        System.Threading.Thread.Sleep(500);
                    });
                }
            });
        process.WaitForExit();
        System.Threading.Thread.Sleep(1000);
        System.Console.WriteLine($"Nr of proc entries: {Directory.GetFileSystemEntries("/proc").Count()}");
    }
}

As shown by the output below .NET 6 doesn't cleanup the orphaned children. And with this change, it does.

Using .NET 6:

Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 73
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 83
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 93
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 103
...

With this patch:

Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 63
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 63
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 63
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Grand-child launched.
Nr of proc entries: 63
...

@adamsitnik adamsitnik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tmds thank you for the fix and description of testing steps! Overall it LGTM, I just wonder whether we could reuse the process id in one place (please see my comment).

@adamsitnik

Copy link
Copy Markdown
Member

image

I've tried to use a magic URL to re-trigger the check, but I've failed. I am going to close and re-open the PR.

@adamsitnik adamsitnik closed this Feb 17, 2022
@adamsitnik adamsitnik reopened this Feb 17, 2022
@adamsitnik
adamsitnik merged commit 8aafc45 into dotnet:main Feb 17, 2022
@adamsitnik adamsitnik added this to the 7.0.0 milestone Feb 17, 2022
@ghost ghost locked as resolved and limited conversation to collaborators Mar 19, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Diagnostics.Process community-contribution Indicates that the PR has been added by a community member

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

3 participants