Skip to content

Bad Process.StartTime in .NET Core 3.1 on macOS #66170

@mattjohnsonpint

Description

@mattjohnsonpint

Description

The value returned from System.Diagnostics.Process.StartTime is wildly incorrect when run on .NET Core 3.1 (runtime 3.1.22) on macOS 12.2.1 with M1 (Apple Silicon / arm64) processor. The problem does not occur on .NET 6.0 or 5.0, nor does it occur on .NET Core 3.1 running on macOS with an Intel (x64) processor.

Reproduction Steps

Following this guidance, install the following SDKs on macOS:

  • 6.0.200 arm64
  • 6.0.200 x64
  • 5.0.405 x64
  • 3.1.416 x64

Create a new console app:

test.csproj:

<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFrameworks>net6.0;net5.0;netcoreapp3.1</TargetFrameworks>
  </PropertyGroup>
</Project>

Program.cs:

using System;
using System.Diagnostics;
using System.Runtime.InteropServices;

namespace ConsoleApp1
{
    public static class Program
    {
        public static void Main()
        {
            var processStartTime = Process.GetCurrentProcess().StartTime.ToUniversalTime();
            var utcNow = DateTime.UtcNow;
            var delta = utcNow - processStartTime;

            Console.WriteLine($"OS Architecture: {RuntimeInformation.OSArchitecture}");
            Console.WriteLine($"Framework: {RuntimeInformation.FrameworkDescription}");
            Console.WriteLine($"Process.StartTime: {processStartTime:o}");
            Console.WriteLine($"DateTime.UtcNow: {utcNow:o}");
            Console.WriteLine($"Delta: {delta}");
            Console.WriteLine();
        }
    }
}

Run the program under the various runtimes and observe the results.

Expected behavior

The process start time should be very close to DateTime.UtcNow (delta near zero) in all cases.

Actual behavior

The output is correct on .NET 6 and 5, but wildly incorrect on .NET Core 3.1 (LTS).

% dotnet run -f net6.0
OS Architecture: Arm64
Framework: .NET 6.0.2
Process.StartTime: 2022-03-03T22:26:01.6263462Z
DateTime.UtcNow: 2022-03-03T22:26:01.7393750Z
Delta: 00:00:00.1130288

% dotnet run -f net6.0 -a x64
OS Architecture: X64
Framework: .NET 6.0.2
Process.StartTime: 2022-03-03T22:26:07.7978340Z
DateTime.UtcNow: 2022-03-03T22:26:07.9519200Z
Delta: 00:00:00.1540860

% dotnet run -f net5.0       
OS Architecture: X64
Framework: .NET 5.0.14
Process.StartTime: 2022-03-03T22:26:18.0786000Z
DateTime.UtcNow: 2022-03-03T22:26:18.2399030Z
Delta: 00:00:00.1613030

% dotnet run -f netcoreapp3.1
OS Architecture: X64
Framework: .NET Core 3.1.22
Process.StartTime: 2022-03-03T06:58:55.5796520Z
DateTime.UtcNow: 2022-03-03T22:26:51.5452700Z
Delta: 15:27:55.9656180

Regression?

The symptoms are similar to #19928, but that was resolved very long ago in .NET Core 2.0.

That it is working in .NET 5 and 6, might be related to #30241

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions