New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Process constructor from SafeProcessHandle #71595
Comments
Tagging subscribers to this area: @dotnet/area-system-diagnostics-process Issue DetailsBackground and motivationThere are plenty of ways to create a process where .NET does not support this or where there is already a process handle from native method. API Proposalnamespace System.Diagnostics;
public class Process
{
public Process(SafeProcessHandle process)
} API Usageusing SafeProcessHandle processHandle = Win32Api.CreateProcess(…);
Process process = new Process(processHandle); Alternative DesignsNo response RisksThere should be no risk.
|
Would this be a Windows-only API? Not clear to me how you'd get a SafeProcessHandle from a native method on Linux or Mac. |
I am sure there is a way yes since they too have process ID's. |
On non-Windows, |
Modern linux does have proper process file descriptors, but that feature may be too modern for dotnet support (I don't know?). They do solve many problems however - race conditions around PID recycling, etc. |
Thinking about this further, would it make more sense for this to be exposed as a using Microsoft.Win32.SafeHandles;
namespace System.Diagnostics;
public partial class Process
{
public static Process GetProcessByHandle(SafeProcessHandle handle);
public static Process GetProcessByHandle(IntPtr handle); // potential extra API
} Having a constructor would be consistent with |
For creating a Process from a native process handle (SafeProcessHandle or IntPtr) on Windows, I don't think caching would be a good idea. The handle might not have the same access rights as what the Process class specifies when it opens a process handle, and it might have access to a process that the current process is not normally authorised to open. If I created a Process object from such a handle, I would not want that to affect the behaviour of future Process.GetProcessById calls because such an effect would make my app more difficult to test. Also, I would not want Process.Dispose of a different process object to close my process handle prematurely. Re implementing this on Linux, pidfd_open seems to be the function referenced in #71595 (comment). If I read this correctly, a pidfd does not carry any access rights (if you receive a pidfd, you cannot use that to kill a process that you wouldn't otherwise be authorised to kill). |
Background and motivation
There are plenty of ways to create a process where .NET does not support this or where there is already a process handle from native method.
Currently there is no possibility to create a .NET Process object from the SafeProcessHandle.
API Proposal
API Usage
Alternative Designs
Adding per platform different process creation options.
Such as #71515
Risks
There should be no risk.
We already have a precedent with Socket constructor from SafeSockegHandle.
The text was updated successfully, but these errors were encountered: