Skip to content

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

Closed

Description

@SteveL-MSFT

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.

Activity

  1. added
    untriaged
    on Mar 17, 2020
  2. added
    os-linux
    on Mar 17, 2020
  3. tmds commented on Mar 17, 2020

    @tmds
    Member

    @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
    
  4. SteveL-MSFT commented on Mar 17, 2020

    @SteveL-MSFT
    ContributorAuthor

    So in PowerShell, the code is calling GetProcessById()

  5. tmds commented on Mar 17, 2020

    @tmds
    Member
            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
    
  6. SteveL-MSFT commented on Mar 17, 2020

    @SteveL-MSFT
    ContributorAuthor

    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

  7. SteveL-MSFT commented on Mar 17, 2020

    @SteveL-MSFT
    ContributorAuthor

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

    Thread.CurrentThread.Name = "foo"
    
  8. tmds commented on Mar 17, 2020

    @tmds
    Member

    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

  9. janvorli commented on Mar 17, 2020

    @janvorli
    Member

    @tmds I agree.

  10. lpereira commented on Mar 17, 2020

    @lpereira
    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.

  11. tmds commented on Mar 21, 2020

    @tmds
    Member

    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.

  12. removed
    untriaged
    on Sep 17, 2020
  13. ghost locked as resolved and limited conversation to collaborators on Dec 10, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-System.Diagnostics.Processos-linux

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions