[release/6.0] Harden native gate thread against affinity mask growth #59132
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Port of #59129
Hardened ThreadpoolMgr::GateThreadStart against the possibility that the observed group-local affinity mask contains set bits at positions higher than the total group-local CPU count that was captured during earlier initialization.
This fixes customer-reported crashes which have occurred on multi-group machines with heterogenous CPU counts across groups (but the same crash can probably also occur on single-group VMs if CPUs are hot-added and are then manually added to the process affinity mask).
Customer Impact
When the thread pool is used after being idle for a short duration, the system's logical processor count and the process' affinity have increased since the start of the process, and the process is not configured to use all CPU groups, the process may crash. This can potentially happen on multi-CPU-group systems with heterogenous CPU counts across groups where the process may be moved to a different CPU group with more processors, or in systems where CPUs may be hot-added.
Testing
Verified in the debugger that increases to the affinity mask behave as expected after the change.
Risk
Low. The change may only increase and not decrease the size of an array, up to a max of 64 elements. I have not seen many of these crashes so far in .NET Core.