Skip to content

Consolidate overlapped I/O non-Windows stubs; remove stale ILLink workaround; remove incorrect [RequiresUnsafe] annotations#125507

Merged
jkotas merged 31 commits intomainfrom
copilot/remove-duplicate-overlapped-implementations
Mar 20, 2026
Merged

Consolidate overlapped I/O non-Windows stubs; remove stale ILLink workaround; remove incorrect [RequiresUnsafe] annotations#125507
jkotas merged 31 commits intomainfrom
copilot/remove-duplicate-overlapped-implementations

Conversation

Copy link
Contributor

Copilot AI commented Mar 12, 2026

PreAllocatedOverlapped and ThreadPoolBoundHandle had separate implementations for non-Windows platforms: full portable/Unix partial-class implementations for Unix and Browser with managed threads, and stub files for Browser/Wasi without managed threads — despite overlapped I/O being unsupported (ThreadPoolBoundHandle.BindHandle always throws PlatformNotSupportedException) on all non-Windows targets.

Description

  • Add PreAllocatedOverlapped.PlatformNotSupported.cs — standalone class whose constructor and UnsafeCreate call ArgumentNullException.ThrowIfNull(callback) before throwing PlatformNotSupportedException(SR.PlatformNotSupported_OverlappedIO); replaces the old no-op Browser.cs stub and the portable/Unix partial-class pair for all non-Windows targets.
  • Rename ThreadPoolBoundHandle.Browser.csThreadPoolBoundHandle.PlatformNotSupported.cs — preserves the existing "all methods throw PlatformNotSupportedException" behavior under a platform-neutral name; private constructor now contains Debug.Fail("Unreachable") to document that the constructor can never legitimately be reached.
  • Delete PreAllocatedOverlapped.Unix.cs and ThreadPoolBoundHandle.Unix.cs — no longer needed; all non-Windows platforms now use the PlatformNotSupported.cs standalone classes.
  • Make PreAllocatedOverlapped.Portable.cs and ThreadPoolBoundHandle.Portable.cs Windows-only in the FeaturePortableThreadPool block — the portable partial-class cores are only required on Windows where the thread pool can be configured at runtime.
  • Consolidate PlatformNotSupported.cs files into a single Condition="'$(TargetsWindows)' != 'true'" ItemGroup outside the FeaturePortableThreadPool block — replaces both the per-entry non-Windows conditions inside FeaturePortableThreadPool and the old FeatureMultithreading != true block, covering all non-Windows targets (with or without managed threads) in one place.
  • Move ThreadPoolBoundHandleOverlapped.cs to the platform-neutral TargetsWindows == true section — moved out of the FeaturePortableThreadPool block and into the large Windows-only ItemGroup alongside ThreadPoolBoundHandle.WindowsThreadPool.cs and PreAllocatedOverlapped.WindowsThreadPool.cs, so it is available to all Windows thread pool configurations.
  • Move Win32ThreadPoolNativeOverlapped.cs, Win32ThreadPoolNativeOverlapped.ExecutionContextCallbackArgs.cs, and Win32ThreadPoolNativeOverlapped.OverlappedData.cs to the TargetsWindows == true section — these files are Windows-only and are now grouped with the other Windows-only thread pool files, in their correct alphabetical position between WaitHandle.Windows.cs and WindowsThreadPool.cs.
  • Move DeferredDisposableLifetime.cs to the Windows-specific section — this file is only needed on Windows and is now grouped with the other Windows-only files.
  • Simplify all .Unix.cs conditions in the FeaturePortableThreadPool block to '$(TargetsWindows)' != 'true' — replaces the previous verbose per-platform conditions on ThreadPool.Unix.cs, PortableThreadPool.Unix.cs, and LowLevelLifoSemaphore.Unix.cs.
  • Keep all entries alphabetically sorted within their blocksPreAllocatedOverlapped.* sorted PlatformNotSupported.csPortable.csWindows.cs; PreAllocatedOverlapped.WindowsThreadPool.cs placed in its correct alphabetical position in the Windows-only ItemGroup; all System\Threading entries in the Windows-only section sorted alphabetically.
  • Remove stale ThreadPoolBoundHandle ILLink descriptor — the workaround (preserving .interfaceimpl when the type is not instantiated on Unix) was fixed in the linker long ago (mono/linker#649).
  • PreAllocatedOverlapped_NullAsCallback_ThrowsArgumentNullException test runs on all platforms — argument validation (ArgumentNullException for null callback) is performed before PlatformNotSupportedException on non-Windows, following the pattern used in other places. Tests that require successful construction of PreAllocatedOverlapped remain [PlatformSpecific(TestPlatforms.Windows)].
  • Remove incorrect [RequiresUnsafe] annotations[RequiresUnsafe] was incorrectly applied (due to a merge conflict) to AllocateNativeOverlapped, UnsafeAllocateNativeOverlappedPortableCore, and AllocateNativeOverlappedPortableCore methods in ThreadPoolBoundHandle.Portable.cs, to the private AllocateNativeOverlapped in Overlapped.cs, and to Overlapped.Pack and Overlapped.UnsafePack. All these annotations are removed.

Behavioral note: On all non-Windows platforms, PreAllocatedOverlapped validates the callback argument (throwing ArgumentNullException if null) before throwing PlatformNotSupportedException to indicate overlapped I/O is not supported. This is technically a breaking change from the previous Unix behavior where the constructor succeeded (allocating a ThreadPoolBoundHandleOverlapped while leaking memory) even though the object could never be used.


💬 Send tasks to Copilot coding agent from Slack and Teams to turn conversations into code. Copilot posts an update in your thread when it's finished.

Copilot AI and others added 2 commits March 12, 2026 18:05
…pped.Unix.cs, remove ILLink workaround

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @agocke, @VSadov
See info in area-owners.md if you want to be subscribed.

@jkotas
Copy link
Member

jkotas commented Mar 18, 2026

@copilot Merge from main and resolve conflicts

Copy link
Contributor Author

Copilot AI commented Mar 18, 2026

@copilot Merge from main and resolve conflicts

Done in 2ef8f8f. Merged main and resolved the conflict in System.Private.CoreLib.Shared.projitems — adapted the Browser/Wasi-without-threading ItemGroup condition from FeatureWasmManagedThreads to FeatureMultithreading to match the rename that landed in main.

…PortableThreadPool block

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR simplifies ThreadPoolBoundHandle / PreAllocatedOverlapped on non-Windows by consolidating them into platform-not-supported implementations (since overlapped I/O is unsupported there), while also cleaning up CoreLib project item conditions and removing an outdated ILLink descriptor workaround.

Changes:

  • Replace non-Windows PreAllocatedOverlapped / ThreadPoolBoundHandle implementations with unified *.PlatformNotSupported.cs stubs; delete old Unix/Browser variants.
  • Reorganize System.Private.CoreLib.Shared.projitems to group Windows-only thread-pool/overlapped files under TargetsWindows == true, and simplify non-Windows conditions under TargetsWindows != true.
  • Remove stale ILLink descriptor for ThreadPoolBoundHandle and update tests to be Windows-specific where behavior is now PNSE on non-Windows.

Reviewed changes

Copilot reviewed 8 out of 8 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/libraries/System.Threading.Overlapped/tests/ThreadPoolBoundHandle_PreAllocatedOverlappedTests.cs Marks several PreAllocatedOverlapped argument-validation tests as Windows-only.
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.Unix.cs Deletes the non-Windows implementation.
src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.PlatformNotSupported.cs Adds Debug.Fail in private ctor; otherwise provides PNSE-based stub surface.
src/libraries/System.Private.CoreLib/src/System/Threading/PreAllocatedOverlapped.Unix.cs Deletes the Unix implementation.
src/libraries/System.Private.CoreLib/src/System/Threading/PreAllocatedOverlapped.PlatformNotSupported.cs Adds a new non-Windows PNSE-based implementation.
src/libraries/System.Private.CoreLib/src/System/Threading/PreAllocatedOverlapped.Browser.cs Removes the old Browser stub.
src/libraries/System.Private.CoreLib/src/System.Private.CoreLib.Shared.projitems Moves/retargets compile-item conditions to align Windows vs non-Windows grouping and portable threadpool layout.
src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.Shared.xml Removes outdated ILLink preservation descriptor for ThreadPoolBoundHandle.
Comments suppressed due to low confidence (1)

src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.PlatformNotSupported.cs:16

  • In the platform-not-supported implementation, Handle currently returns null! and the private ctor only contains Debug.Fail (which is removed in Release). This means an instance created via reflection/serialization could exist and then fail with NullReferenceException rather than a consistent PlatformNotSupportedException. Consider making the ctor and/or Handle getter throw PlatformNotSupportedException(SR.PlatformNotSupported_OverlappedIO) to keep all entry points consistently unsupported.

@jkotas
Copy link
Member

jkotas commented Mar 19, 2026

@copilot Merge from main and resolve conflicts

…ate-overlapped-implementations

# Conflicts:
#	src/libraries/System.Private.CoreLib/src/System/Threading/ThreadPoolBoundHandle.Unix.cs
Copy link
Contributor Author

Copilot AI commented Mar 19, 2026

@copilot Merge from main and resolve conflicts

Done in c9fc2bb. Merged main (2 new commits: RequiresUnsafe analyzer/codefix + WebHeaderEncoding cleanup). Resolved the single conflict: main had modified ThreadPoolBoundHandle.Unix.cs (adding [RequiresUnsafe] annotations), but our branch already deleted it — kept our deletion.

…pped* methods

Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI requested a review from jkotas March 19, 2026 18:14
Co-authored-by: jkotas <6668460+jkotas@users.noreply.github.com>
Copilot AI changed the title Consolidate overlapped I/O non-Windows stubs; remove stale ILLink workaround Consolidate overlapped I/O non-Windows stubs; remove stale ILLink workaround; remove incorrect [RequiresUnsafe] annotations Mar 19, 2026
Copy link
Member

@pavelsavara pavelsavara left a comment

Choose a reason for hiding this comment

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

LGTM

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

Labels

area-System.Threading linkable-framework Issues associated with delivering a linker friendly framework

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants