Skip to content

Implement SafeProcessHandle.Open API#126705

Draft
Copilot wants to merge 4 commits intomainfrom
copilot/add-safeproceshandle-open-api
Draft

Implement SafeProcessHandle.Open API#126705
Copilot wants to merge 4 commits intomainfrom
copilot/add-safeproceshandle-open-api

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

Description

Implements SafeProcessHandle.Open(int processId) — opens an existing process by PID and returns a SafeProcessHandle.

  • Windows: Uses OpenProcess with PROCESS_QUERY_LIMITED_INFORMATION | SYNCHRONIZE | PROCESS_TERMINATE.
  • Linux: Uses pidfd_open syscall when available, falls back to kill(pid, 0).
  • Other Unix: Uses kill(pid, 0) to verify process existence and signal permission.
using SafeProcessHandle handle = SafeProcessHandle.Open(pid);
handle.Kill();

Changes

  • Ref assembly (ref/System.Diagnostics.Process.cs): Added Open declaration with platform attributes
  • Shared (SafeProcessHandle.cs): Open method with argument validation, XML docs, [UnsupportedOSPlatform]/[SupportedOSPlatform] attributes
  • Windows (SafeProcessHandle.Windows.cs): OpenCore via Interop.Kernel32.OpenProcess
  • Unix (SafeProcessHandle.Unix.cs): OpenCore via Interop.Sys.OpenProcess, added _pidfd field with cleanup in ReleaseHandle, new private constructor for pidfd-backed handles. ForkAndExecProcess now also populates _pidfd so child processes started via Start() get a pidfd on Linux.
  • Native (pal_process.c/h): Extracted TryOpenPidfd as a reusable linux-specific helper for pidfd_open. Extended SystemNative_ForkAndExecProcess with int32_t* outPidfd parameter — populated via TryOpenPidfd on the fork path (Linux), set to -1 on the posix_spawn path (macOS). SystemNative_OpenProcess also reuses the same helper. Added SYS_pidfd_open define (matching __NR_close_range pattern), WASI stub, entrypoint registration.
  • Interop (Interop.OpenProcess.cs): New Unix P/Invoke for SystemNative_OpenProcess
  • Interop (Interop.ForkAndExecProcess.cs): Extended P/Invoke and managed wrapper with out int parameter (matching the out int lpChildPid pattern) to read pidfd from native layer
  • Tests (SafeProcessHandleTests.cs): Argument validation, non-existent PID, open running process, open-then-kill

Testing

  • All 405 process tests pass (0 failures, 5 skipped)

Copilot AI and others added 2 commits April 9, 2026 11:51
Copilot AI self-assigned this Apr 9, 2026
Copilot AI review requested due to automatic review settings April 9, 2026 12:38
Copilot AI review requested due to automatic review settings April 9, 2026 12:38
Copy link
Copy Markdown
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot give my feedback a try. Run all process tests and ensure they pass before you push any changes.

@dotnet-policy-service
Copy link
Copy Markdown
Contributor

Tagging subscribers to this area: @dotnet/area-system-diagnostics-process
See info in area-owners.md if you want to be subscribed.

…ss with outPidfd parameter

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/664957d4-993f-4f40-aa79-ce45ec55fd11

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 9, 2026 13:26
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot give my feedback a try. Run all process tests and ensure they pass before you push any changes.

Done in e2bc39d. All 405 process tests pass (0 failures, 5 skipped).

Copilot AI requested a review from adamsitnik April 9, 2026 13:44
Copy link
Copy Markdown
Member

@adamsitnik adamsitnik left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot address my feedback

…acOS posix_spawn path

Agent-Logs-Url: https://github.com/dotnet/runtime/sessions/ed9ebae7-b0ac-4a66-a222-8f7305c55a9b

Co-authored-by: adamsitnik <6011991+adamsitnik@users.noreply.github.com>
Copilot AI requested review from Copilot and removed request for Copilot April 9, 2026 14:36
Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 9, 2026

@copilot address my feedback

All three review items addressed in a114b09. All 405 process tests pass (0 failures, 5 skipped).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-System.Diagnostics.Process NO-REVIEW Experimental/testing PR, do NOT review it

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants