Skip to content

Process.ProcessName throws for services.exe when accessed from a non-admin user #70007

@schuettecarsten

Description

@schuettecarsten

Process.ProcessName throws when the process is an admin process (like "services.exe") and the current app is running as a low privilege user. This is a regression from .NET 6, and it breaks Microsoft.Extensions.Hosting.WindowsServices when running as a non-admin user.

This could possibly be a regression from #59672.

Repro Steps

  1. Build a console app as below:
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net7.0</TargetFramework>
    <ImplicitUsings>enable</ImplicitUsings>
    <Nullable>enable</Nullable>
  </PropertyGroup>
</Project>
using System.Diagnostics;

namespace ConsoleApp1
{
    internal class Program
    {
        const int ServicesProcessId = 756;

        static void Main(string[] args)
        {
            var services = Process.GetProcessById(ServicesProcessId);
            Console.WriteLine(services.ProcessName);
        }
    }
}
  1. Look in Task Manager for the services.exe process and replace the ServiceProcessId with the process ID for your current machine.

  2. Run the app as you, it prints

services
  1. Run the app as a non-admin, low privilege user
Unhandled exception. System.InvalidOperationException: Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at ConsoleApp1.Program.Main(String[] args) in C:\Users\eerhardt\source\repos\WindowsService1\ConsoleApp1\Program.cs:line 12
  1. Change the TargetFramework to net6.0, and run as the same non-admin, low privilege user:
services

Original Report

I come back to the issue #67093, which is closed. The error still occurs with preview.4 and todays nightly build, but only on Windows Server 2016, not on my local Windows 11 developer machine. The error is:

Application: CloudManagementTool.WorkerService.exe
CoreCLR Version: 7.0.22.27203
.NET Version: 7.0.0-preview.5.22272.3
Description: The process was terminated due to an unhandled exception.
Exception Info: System.TypeInitializationException: The type initializer for 'CloudManagementTool.WorkerService.WorkerService' threw an exception.
 ---> System.InvalidOperationException: Process has exited, so the requested information is not available.
   at System.Diagnostics.Process.get_ProcessName()
   at Microsoft.Extensions.Hosting.WindowsServices.WindowsServiceHelpers.IsWindowsService()
   at ConsoleApp1.Program.<Main>(String[] args) in D:\Test\Program.cs:line 13

A reproducing sample program is quite short, but it only crashes on Windows Server 2016 and when registered and executed as a service. Starting the same code directly from the console works fine.

public class Program
{
	public static async Task Main(string[] args)
	{
		var isService = WindowsServiceHelpers.IsWindowsService();
		if (isService)
		{
			// ...

This issue becomes critical now, because it will make the 7.0 version unsable for windows services on Windows Server 2016.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions