Conversation
…des, s_usePortInSpn, s_getAddrInfoExSupported, s_disableTlsResume with NullableBool Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
|
Tagging subscribers to this area: @dotnet/area-meta |
…hread safety comment Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs
Outdated
Show resolved
Hide resolved
…andles/SafeFileHandle.Unix.cs
|
@copilot Could you please check for other places throughout the repo that can be refactored to use NullableBool? |
…ableBool - Environment.cs: volatile sbyte s_privilegedProcess → NullableBool - Interop.MemfdCreate.cs: volatile sbyte s_memfdSupported → NullableBool; removes Interlocked.CompareExchange (MemfdSupportedImpl is idempotent) - System.IO.MemoryMappedFiles.csproj: add NullableBool.cs to unix group Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Found and refactored two additional sites (commit 728a0db):
Other patterns checked and ruled out:
|
@copilot This is a good candidate for NullableBool. Please change it by remapping 0/1/2 to NullableBool equivalents. |
…ableBool Remap 0/1/2 (uninitialized/match/no-match) to NullableBool.Undefined/True/False. Add NullableBool.cs to the linux ItemGroup in System.Diagnostics.Process.csproj. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Done in cf04cca. Remapped 0→ |
|
@copilot Please fix this build break on Windows: |
There was a problem hiding this comment.
Pull request overview
This PR standardizes the repo’s “tri-state cached bool” pattern by introducing a shared System.NullableBool : sbyte in src/libraries/Common and migrating multiple call sites away from ad-hoc int/sbyte sentinel encodings and TextInfo’s local Tristate enum.
Changes:
- Added
internal enum NullableBool : sbyte(Undefined/False/True) insrc/libraries/Commonand linked it into affected projects. - Replaced local tri-state patterns in several libraries (CoreLib, Console, Http, NameResolution, Security, MemoryMappedFiles, Diagnostics.Process) with
NullableBool. - Removed the redundant local
NullableBoolenum fromSafeFileHandle.Unix.csandTristatefromTextInfo.
Reviewed changes
Copilot reviewed 19 out of 19 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| src/libraries/System.Private.CoreLib/src/System/Globalization/TextInfo.cs | Replaces Tristate with NullableBool for ASCII casing cache. |
| src/libraries/System.Private.CoreLib/src/System/Globalization/TextInfo.Icu.cs | Replaces Tristate with NullableBool for Turkish casing cache. |
| src/libraries/System.Private.CoreLib/src/System/Environment.cs | Converts privileged-process cached tri-state from sbyte to NullableBool. |
| src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems | Links Common\System\NullableBool.cs into CoreLib shared items. |
| src/libraries/System.Private.CoreLib/src/Microsoft/Win32/SafeHandles/SafeFileHandle.Unix.cs | Removes local NullableBool enum (now uses Common’s). |
| src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Protocol.cs | Converts TLS resume switch cache from int tri-state to NullableBool. |
| src/libraries/System.Net.Security/src/System.Net.Security.csproj | Links Common\System\NullableBool.cs into System.Net.Security. |
| src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs | Converts GetAddrInfoEx support cache from int tri-state to NullableBool. |
| src/libraries/System.Net.NameResolution/src/System.Net.NameResolution.csproj | Links Common\System\NullableBool.cs into System.Net.NameResolution. |
| src/libraries/System.Net.Http/src/System/Net/Http/SocketsHttpHandler/AuthenticationHelper.NtAuth.cs | Converts SPN port switch cache from int tri-state to NullableBool. |
| src/libraries/System.Net.Http/src/System.Net.Http.csproj | Links Common\System\NullableBool.cs into System.Net.Http. |
| src/libraries/System.IO.MemoryMappedFiles/src/System.IO.MemoryMappedFiles.csproj | Links Common\System\NullableBool.cs to support Common interop usage. |
| src/libraries/System.Diagnostics.Process/src/System/Diagnostics/ProcessManager.Linux.cs | Remaps _procMatchesPidNamespace tri-state encoding onto NullableBool. |
| src/libraries/System.Diagnostics.Process/src/System.Diagnostics.Process.csproj | Links Common\System\NullableBool.cs into System.Diagnostics.Process (Linux group). |
| src/libraries/System.Console/src/System.Console.csproj | Links Common\System\NullableBool.cs into System.Console. |
| src/libraries/Microsoft.Extensions.Logging.Console/src/Microsoft.Extensions.Logging.Console.csproj | Links Common\System\NullableBool.cs into Microsoft.Extensions.Logging.Console. |
| src/libraries/Common/src/System/NullableBool.cs | Adds the shared tri-state enum definition (sbyte-backed). |
| src/libraries/Common/src/System/Console/ConsoleUtils.cs | Converts ANSI color emission cache from int tri-state to NullableBool. |
| src/libraries/Common/src/Interop/Unix/System.Native/Interop.MemfdCreate.cs | Converts memfd support cache from sbyte tri-state to NullableBool and removes interlocked CAS. |
src/libraries/System.Net.NameResolution/src/System/Net/NameResolutionPal.Windows.cs
Show resolved
Hide resolved
….Tests.csproj Windows ItemGroup The test project directly includes NameResolutionPal.Windows.cs (which uses NullableBool) but was missing the NullableBool.cs include, causing CS0246 on Windows CI builds. Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Fixed in 54facbc. The |
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Adam Sitnik <adam.sitnik@gmail.com>
src/libraries/System.Net.Security/src/System/Net/Security/SslStream.Protocol.cs
Outdated
Show resolved
Hide resolved
…Protocol.cs Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
adamsitnik
left a comment
There was a problem hiding this comment.
LGTM! TBH I have not expected this pattern to be so common.
Description
Consolidates the common "lazy-initialized cached bool" pattern into a single shared
NullableBool : sbyteenum, replacing localvolatile int/volatile sbytetri-state fields and the privateTristateenum inTextInfo.Changes
New:
src/libraries/Common/src/System/NullableBool.cs—internal enum NullableBool : sbyte { Undefined = 0, False = -1, True = 1 }.sbytebacking fits in one byte (same asbool) and is valid forvolatile. Unlikebool?, it is safe to use from multiple threads (aNullable<T>is a two-field struct that can tear under concurrent access).Removed: private nested
NullableBoolenum fromSafeFileHandle.Unix.cs(fields already typedNullableBool, now reference Common).Replaced
TristateinTextInfo: The localTristate : byte { NotInitialized=0, False=1, True=2 }driving_isAsciiCasingSameAsInvariantand_needsTurkishCasingis replaced withNullableBool.Replaced
volatile inttri-state fields (-1= unset,0= false,1= true) in:ConsoleUtils.cs—s_emitAnsiColorCodesAuthenticationHelper.NtAuth.cs—s_usePortInSpnSslStream.Protocol.cs—s_disableTlsResumeands_enableServerAiaDownloadsNameResolutionPal.Windows.cs—s_getAddrInfoExSupported(also removes the now-unnecessaryInterlocked.CompareExchange; initialization is idempotent, consistent with all other patterns)Replaced
volatile sbytetri-state fields (same encoding:0= unset,1= true,-1= false) in:Environment.cs—s_privilegedProcessInterop.MemfdCreate.cs—s_memfdSupported(also removesInterlocked.CompareExchange;MemfdSupportedImpl()is idempotent — read-only kernel version check and syscall)Replaced
volatile intfield with non-standard encoding (remapped toNullableBoolequivalents) in:ProcessManager.Linux.cs—_procMatchesPidNamespace(was0= unset,1= match,2= no match; remapped toNullableBool.Undefined,NullableBool.True,NullableBool.False)Project file updates: Added
NullableBool.cstoSystem.Private.CoreLib.Shared.projitems,System.Console.csproj,Microsoft.Extensions.Logging.Console.csproj,System.Net.Http.csproj,System.Net.NameResolution.csproj,System.Net.Security.csproj,System.IO.MemoryMappedFiles.csproj(unix/browser/wasi group),System.Diagnostics.Process.csproj(linux group), andSystem.Net.NameResolution.Pal.Tests.csproj(Windows group — the test project directly compilesNameResolutionPal.Windows.csand also needs the type).Before / After
Testing
All affected libraries build successfully (
System.Private.CoreLib,System.Console,System.Net.Http,System.Net.NameResolution,System.Net.Security,System.IO.MemoryMappedFiles,System.Diagnostics.Process). Windows CI build break (CS0246inSystem.Net.NameResolution.Pal.Tests) resolved by addingNullableBool.csto the test project's WindowsItemGroup.💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.