dotnet/runtime

`process.MainModule` throws `Win32Exception` instead of `InvalidOperationException` when process has exited in some cases

Open

#105,090 创建于 2024年7月18日

在 GitHub 查看
 (3 评论) (0 反应) (0 负责人)C# (5,445 fork)batch import
area-System.Diagnostics.Processdocumentationhelp wanted

仓库指标

Star
 (17,886 star)
PR 合并指标
 (平均合并 12天 11小时) (30 天内合并 661 个 PR)

描述

Description

Accessing MainModule after process exit throws an exception that doesn't match the documentation. The code should either be changed to detect that case or the docs should be changed.

Reproduction Steps

using Process process = Process.GetProcessById(someId);
_ = process.Handle;
process.WaitForExit();
Console.WriteLine(process.ExitCode);
Console.WriteLine(process.MainModule?.FileName ?? "null");

Expected behavior

Throws InvalidOperationException as per docs.

Actual behavior

Throws:

Unhandled exception. System.ComponentModel.Win32Exception (299): Only part of a ReadProcessMemory or WriteProcessMemory request was completed.
   at System.Diagnostics.NtProcessManager.EnumProcessModulesUntilSuccess(SafeProcessHandle, IntPtr[], Int32, Int32&, Int32) + 0x91
   at System.Diagnostics.NtProcessManager.GetModules(Int32, Boolean) + 0x110
   at System.Diagnostics.NtProcessManager.GetFirstModule(Int32) + 0xf

Regression?

Not sure.

Known Workarounds

No response

Configuration

Windows 10.0.19045 X64 .NET 9

Other information

No response

贡献者指南