Skip to content

Fix OS-level thread priority degradation in InProcess executors - #3282

Merged
timcassell merged 1 commit into
dotnet:masterfrom
cloudsealed:feature/issue-2706
Oct 5, 2026
Merged

timcassell merged 1 commit into
dotnet:masterfrom
cloudsealed:feature/issue-2706

Conversation

@cloudsealed

Copy link
Copy Markdown
Contributor

Fixes #2706.

When running InProcess benchmarks (with InProcessEmitExecutor or InProcessNoEmitExecutor), the benchmark elevates process and thread priority via TrySetPriority and then aggressively restores the previous priority in a finally block.
However, TrySetPriority suppresses Win32Exception when the elevation fails (which is common on Linux/Unix systems without CAP_SYS_NICE where ProcessPriorityClass.High maps to -11 nice).
As a result, if the user starts the benchmark with a slightly elevated priority (e.g. nice -n -1), the elevation silently fails, but the restoration to ProcessPriorityClass.Normal (mapped to 0 nice) succeeds, permanently degrading the process and thread priority for all subsequent benchmark runs.

This PR fixes the issue by capturing the boolean success result of TrySetPriority and ensuring we only attempt to restore the priority if the initial elevation actually succeeded.

Comment thread src/BenchmarkDotNet/Environments/PhysicalMemoryInfo.cs
@cloudsealed

Copy link
Copy Markdown
Contributor Author

Good catch @timcassell! Rebased onto master and removed the unrelated PhysicalMemoryInfo changes. The PR now contains only the thread priority fix.

@timcassell timcassell left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks @cloudsealed.

@timcassell
timcassell merged commit 6cbcdda into dotnet:master Oct 5, 2026
11 checks passed
@timcassell timcassell added this to the v0.16.0 milestone Oct 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Linux thread priority/nice set to lower-priority than normal starting from the second benchmark.

2 participants