Skip to content
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

Add CA2024: Do not use 'StreamReader.EndOfStream' in async methods #7390

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

mpidash
Copy link
Contributor

@mpidash mpidash commented Aug 25, 2024

Fixes dotnet/runtime#98834.

Rule ID CA2024
Title Do not use 'StreamReader.EndOfStream' in async methods
Message Do not use '{0}' in an async method
Description The property 'StreamReader.EndOfStream' can cause unintended synchronous blocking when no data is buffered. Instead, use 'StreamReader.ReadLineAsync' directly, which returns 'null' when reaching the end of the stream.
Category Reliability
Severity Warning

This analyzer detects when StreamReader.EndOfStream is used in an async method, which can prevent I/O from being done asynchronously.


I've tested against dotnet/runtime, dotnet/roslyn and dotnet/aspnetcore and found no violations.

I've also added a reference to EndOfStream in an async method in runtime for testing, which leads to the following message: warning CA2024: Do not use 'sr.EndOfStream' in an async method.

This analyzer detects when 'StreamReader.EndOfStream' is used in an
async method, which can prevent I/O from being done asynchronously.
@mpidash mpidash requested a review from a team as a code owner August 25, 2024 17:21
Copy link

codecov bot commented Aug 25, 2024

Codecov Report

Attention: Patch coverage is 98.36449% with 7 lines in your changes missing coverage. Please review.

Project coverage is 96.50%. Comparing base (8d4f432) to head (9c662fc).

Additional details and impacted files
@@           Coverage Diff            @@
##             main    #7390    +/-   ##
========================================
  Coverage   96.50%   96.50%            
========================================
  Files        1443     1445     +2     
  Lines      346376   346804   +428     
  Branches    11387    11393     +6     
========================================
+ Hits       334255   334682   +427     
+ Misses       9239     9237     -2     
- Partials     2882     2885     +3     

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

Successfully merging this pull request may close these issues.

[Analyzer Proposal]: Warn on use of StreamReader.EndOfStream in async methods
1 participant