Skip to content

Process Property TotalProcessorTime throws InvalidOperationException exception on macOS #36777

Description

@DmitryKhryukin

Description

I'm trying to get information about current running processes. And I'm getting InvalidOperationException exception accessing TotalProcessorTime Process property.
Please see the configuration and the error message below.

Here is my example code (I've created a test console app here)

        static void Main(string[] args)
        {
            var processes = Process.GetProcesses();

            try
            {
                var timeInformationToBeUsed = processes.Select(x => x.TotalProcessorTime).ToList();
            }
            catch (Exception e)
            {
                Console.WriteLine($"Hi dear, I'm {e.GetType()}!");
                Console.WriteLine("Why am I happening? :P");
                Console.WriteLine($"Message: {e.Message}");
                Console.WriteLine($"StackTrace: {e.StackTrace}");
            }

            Console.ReadKey();
        }

Configuration

SDK: 3.1.101
Framework: .NETCoreApp v3.1 (I've tried v2.1 too - the same result)
OS: macOS Catalina Version 10.15.4

Not sure if it's possible to reproduce it on other configurations.

Regression?

I've tried .NETCoreApp v2.1 too - the same result

Other information

Message: Failed to set or retrieve rusage information. See the error code for OS-specific error information.

StackTrace:
at Interop.libproc.proc_pid_rusage(Int32 pid)
at System.Diagnostics.Process.get_TotalProcessorTime()
at TotalProcessorTimeOsxProblem.Program.<>c.

b__0_0(Process x) in /Projects/TotalProcessorTimeOsxProblem/TotalProcessorTimeOsxProblem/Program.cs:line 15
at System.Linq.Enumerable.SelectArrayIterator2.ToList() at System.Linq.Enumerable.ToList[TSource](IEnumerable1 source)
at TotalProcessorTimeOsxProblem.Program.Main(String[] args) in /Projects/TotalProcessorTimeOsxProblem/TotalProcessorTimeOsxProblem/Program.cs:line 15

So the exception is happening in Process.OSX.cs

        public TimeSpan TotalProcessorTime
        {
            get
            {
                EnsureState(State.HaveNonExitedId);
                Interop.libproc.rusage_info_v3 info = Interop.libproc.proc_pid_rusage(_processId);
                return new TimeSpan(Convert.ToInt64((info.ri_system_time + info.ri_user_time) / NanosecondsTo100NanosecondsFactor));
            }
        }

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions