-
Notifications
You must be signed in to change notification settings - Fork 5.2k
Enforce Async-only methods in StreamConformanceTests #121842
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
|
Tagging subscribers to this area: @dotnet/area-meta |
There was a problem hiding this 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 enforces async-only methods in StreamConformanceTests to ensure that async operations in wrapper streams (SslStream, compression streams, crypto streams) don't perform blocking IO calls. The changes introduce an AsyncOnlyStream wrapper that throws when synchronous methods are called, and update tests to use async operations when testing async code paths.
Key changes:
- Introduced
AsyncOnlyStreamwrapper to enforce async-only operations during testing - Updated test methods to use async disposal patterns and wrap streams appropriately based on async mode
- Converted synchronous IO operations to async in SslStream tests to avoid blocking
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 3 comments.
| File | Description |
|---|---|
| src/libraries/System.Net.Security/tests/FunctionalTests/SslStreamConformanceTests.cs | Converted sync Read/Write calls to async ReadAsync/WriteAsync to avoid blocking during TLS 1.3 handshake flush |
| src/libraries/Common/tests/StreamConformanceTests/System/IO/StreamConformanceTests.cs | Added AsyncOnlyStream wrapper class, CreateWrappedStreamsAsync helper, IsAsync helper, updated test disposal patterns to use async, and added IAsyncDisposable to StreamPair |
You can also share your feedback on Copilot code review for a chance to win a $100 gift card. Take the survey.
| return _innerStream.DisposeAsync(); | ||
| } | ||
|
|
||
| public override void Close() => _innerStream.Close(); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Close() method in AsyncOnlyStream may call synchronous Dispose() internally on the inner stream. This could result in blocking IO operations that this wrapper is designed to prevent. Consider throwing NotSupportedException for Close() similar to other sync methods, or override Close() to call Dispose(false) directly without calling the inner stream's Close(), since disposal is already handled in Dispose(bool) and DisposeAsync().
| public override void Close() => _innerStream.Close(); | |
| public override void Close() | |
| { | |
| throw new NotSupportedException("Synchronous operations are not supported."); | |
| } |
| if (_disposed) return; | ||
|
|
||
| _disposed = true; | ||
| _innerStream.Dispose(); |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The Dispose(bool) method unconditionally calls _innerStream.Dispose() even when disposing is false (finalizer path). The disposing parameter should be checked, and the inner stream should only be disposed when disposing is true. Additionally, calling _innerStream.Dispose() is a synchronous operation that could perform blocking IO, which contradicts the purpose of AsyncOnlyStream. Consider either: (1) not disposing the inner stream in synchronous Dispose and requiring callers to use DisposeAsync, or (2) checking the disposing parameter properly.
| _innerStream.Dispose(); | |
| if (disposing) | |
| { | |
| _innerStream.Dispose(); | |
| } |
| { | ||
| if (_disposed) return default; | ||
|
|
||
| _disposed = true; |
Copilot
AI
Nov 25, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The DisposeAsync() method should call GC.SuppressFinalize(this) to prevent the finalizer from running after async disposal completes. This is a standard pattern for types implementing both IDisposable and IAsyncDisposable.
| _disposed = true; | |
| _disposed = true; | |
| GC.SuppressFinalize(this); |
For stream which wrap another stream (SslStream, compression streams, crypto streams, ....), we can test if async methods are calling only async methods. This should root out cases where we may be mistakenly doing blocking IO calls as part of async stack.