Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

System.Diagnostics.Process.ProcessName returning incorrect information on Linux with .NET 5 #33673

Closed
SteveL-MSFT opened this issue Mar 17, 2020 · 9 comments · Fixed by #34064
Labels
area-System.Diagnostics.Process os-linux Linux OS (any supported distro)

Comments

@SteveL-MSFT
Copy link
Contributor

SteveL-MSFT commented Mar 17, 2020

Moving PowerShell to .NET 5 via branch https://github.com/SteveL-MSFT/PowerShell/tree/dotnet-5

We have tests that expect the pwsh process to have a ProcessName of pwsh, but on Linux, the name is actually ConsoleHost mai (looks to be cut off). Works correctly on Windows and macOS.

Note that [System.AppDomain]::CurrentDomain.FriendlyName returns pwsh as expected.

@Dotnet-GitSync-Bot Dotnet-GitSync-Bot added area-System.Diagnostics.Process untriaged New issue has not been triaged by the area owner labels Mar 17, 2020
@wfurt wfurt added the os-linux Linux OS (any supported distro) label Mar 17, 2020
@tmds
Copy link
Member

tmds commented Mar 17, 2020

@SteveL-MSFT do you have some info on how to reproduce this?

$ dotnet new console -o pwsh

Program.cs:

        static void Main(string[] args)
        {
            Console.WriteLine(Process.GetCurrentProcess().ProcessName);
        }

outputs:

$ dotnet run
pwsh

using:

$ dotnet --version
5.0.100-preview.1.20155.7

@SteveL-MSFT
Copy link
Contributor Author

So in PowerShell, the code is calling GetProcessById()

@tmds
Copy link
Member

tmds commented Mar 17, 2020

        static void Main(string[] args)
        {
            int mypid = getpid();
            Console.WriteLine(Process.GetProcessById(mypid).ProcessName);
        }

        [DllImport("libc")]
        private static extern int getpid();

also outputs:

$ dotnet run
pwsh

@SteveL-MSFT
Copy link
Contributor Author

Let me investigate this a bit after my meeting, wrote a simple C# program and it doesn't repro. The only change on the PowerShell side was moving from .NET Core 3.1 to .NET 5 preview.1

@SteveL-MSFT
Copy link
Contributor Author

@tmds, ok, the problem is when Thread.CurrentThread.Name is set, then the process name uses that:

Thread.CurrentThread.Name = "foo"

@tmds
Copy link
Member

tmds commented Mar 17, 2020

In dotnet/coreclr#27182 support was added for naming Linux threads.
The implementation is using pthread_setname_np.

The name set for the main thread shows up in the comm field in /proc/<pid>/stat (see http://man7.org/linux/man-pages/man5/proc.5.html) which we use to determine the ProcessName.

Previously changing the thread name was a no-op.
Maybe we should continue to no-op for changing name of the main thread?

cc @janvorli @lpereira

@janvorli
Copy link
Member

@tmds I agree.

@lpereira
Copy link
Contributor

Seems reasonable, @tmds. Might be a good idea to also mention in the docs that in some platforms the name might be truncated due to platform limitations (as mentioned by @SteveL-MSFT) to avoid surprises.

Although, to determine the ProcessName, maybe we could try using getauxval(AT_EXECFN)? That shouldn't be affected by the thread name.

@tmds
Copy link
Member

tmds commented Mar 21, 2020

Maybe we should continue to no-op for changing name of the main thread?

I'll make a PR for this change.

Although, to determine the ProcessName, maybe we could try using getauxval(AT_EXECFN)?

I was not aware of this API. We want to get this info for any process on the system. This is available in /proc under an auxv file. Unfortunately, it includes the pointer to the string, which isn't directly usable.

monojenkins pushed a commit to monojenkins/mono that referenced this issue May 6, 2020
… Linux

Fixes dotnet/runtime#33673

This issue is a side-effect of adding support for setting thread names on Linux in dotnet/coreclr#27182.

cc @janvorli @lpereira @SteveL-MSFT
akoeplinger pushed a commit that referenced this issue May 12, 2020
… Linux (#34064)

Fixes #33673

This issue is a side-effect of adding support for setting thread names on Linux in dotnet/coreclr#27182.

Co-authored-by: Dan Moseley <danmose@microsoft.com>
Co-authored-by: Aleksey Kliger (λgeek) <akliger@gmail.com>
@jeffhandley jeffhandley removed the untriaged New issue has not been triaged by the area owner label Sep 17, 2020
@ghost ghost locked as resolved and limited conversation to collaborators Dec 10, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-System.Diagnostics.Process os-linux Linux OS (any supported distro)
Projects
None yet
7 participants