Skip to content

Commit cf92601

Browse files
authored
Add documentation for CA2024 (#44401)
1 parent 8e70c7e commit cf92601

File tree

4 files changed

+111
-0
lines changed

4 files changed

+111
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
---
2+
title: "CA2024: Do not use StreamReader.EndOfStream in async methods"
3+
description: "Learn about code analysis rule CA2024 - Do not use StreamReader.EndOfStream in async methods"
4+
ms.date: 01/16/2025
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA2024
8+
- DoNotUseEndOfStreamInAsyncMethodsAnalyzer
9+
helpviewer_keywords:
10+
- CA2024
11+
author: mpidash
12+
dev_langs:
13+
- CSharp
14+
- VB
15+
---
16+
17+
# CA2024: Do not use StreamReader.EndOfStream in async methods
18+
19+
| Property | Value |
20+
|-------------------------------------|------------------------------------------------------|
21+
| **Rule ID** | CA2024 |
22+
| **Title** | Do not use StreamReader.EndOfStream in async methods |
23+
| **Category** | [Reliability](reliability-warnings.md) |
24+
| **Fix is breaking or non-breaking** | Non-breaking |
25+
| **Enabled by default in .NET 10** | As warning |
26+
27+
## Cause
28+
29+
A call to <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> is made inside an async method.
30+
31+
## Rule description
32+
33+
The property <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> can cause unintended synchronous blocking when no data is buffered. Instead, use <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> directly, which returns `null` when reaching the end of the stream.
34+
35+
## How to fix violations
36+
37+
To fix a violation, directly call <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> and check the return value for `null`.
38+
39+
## Example
40+
41+
The following code snippet shows a violation of CA2024:
42+
43+
```csharp
44+
public async Task Example(StreamReader streamReader)
45+
{
46+
while (!streamReader.EndOfStream)
47+
{
48+
string? line = await streamReader.ReadLineAsync();
49+
// Do something with line.
50+
}
51+
}
52+
```
53+
54+
```vb
55+
Public Async Function Example(streamReader As StreamReader) As Task
56+
While Not streamReader.EndOfStream
57+
Dim line As String = Await streamReader.ReadLineAsync()
58+
' Do something with line.
59+
End While
60+
End Function
61+
```
62+
63+
The following code snippet fixes the violation:
64+
65+
```csharp
66+
public async Task Example(StreamReader streamReader)
67+
{
68+
string? line;
69+
while ((line = await streamReader.ReadLineAsync()) is not null)
70+
{
71+
// Do something with line.
72+
}
73+
}
74+
```
75+
76+
```vb
77+
Public Async Function Example(streamReader As StreamReader) As Task
78+
Dim line As String = Await streamReader.ReadLineAsync()
79+
While line IsNot Nothing
80+
' Do something with line.
81+
line = Await streamReader.ReadLineAsync()
82+
End While
83+
End Function
84+
```
85+
86+
## When to suppress warnings
87+
88+
You shouldn't suppress warnings from this rule, as your app might hang if you don't fix the violations.
89+
90+
## Suppress a warning
91+
92+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
93+
94+
```csharp
95+
#pragma warning disable CA2024
96+
// The code that's violating the rule is on this line.
97+
#pragma warning restore CA2024
98+
```
99+
100+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
101+
102+
```ini
103+
[*.{cs,vb}]
104+
dotnet_diagnostic.CA2024.severity = none
105+
```
106+
107+
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md).

docs/fundamentals/code-analysis/quality-rules/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ The following table lists code quality analysis rules.
189189
> | [CA2020: Prevent behavioral change caused by built-in operators of IntPtr/UIntPtr](ca2020.md) | Some built-in operators added in .NET 7 behave differently than the user-defined operators in .NET 6 and earlier versions. Some operators that used to throw in unchecked context while overflowing don't throw anymore unless wrapped within checked context. Some operators that previously didn't throw in checked context now throw unless wrapped within unchecked context. |
190190
> | [CA2021: Don't call Enumerable.Cast\<T> or Enumerable.OfType\<T> with incompatible types](ca2021.md) | A call to <xref:System.Linq.Enumerable.Cast%60%601(System.Collections.IEnumerable)?displayProperty=nameWithType> or <xref:System.Linq.Enumerable.OfType%60%601(System.Collections.IEnumerable)?displayProperty=nameWithType> specifies a type parameter that's incompatible with the type of the input collection. |
191191
> | [CA2022: Avoid inexact read with Stream.Read](ca2022.md) | A call to `Stream.Read` might return fewer bytes than requested, resulting in unreliable code if the return value isn't checked. |
192+
> | [CA2024: Do not use StreamReader.EndOfStream in async methods](ca2024.md) | The property <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> can cause unintended synchronous blocking when no data is buffered. Instead, use <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> directly, which returns `null` when reaching the end of the stream. |
192193
> | [CA2100: Review SQL queries for security vulnerabilities](ca2100.md) | A method sets the System.Data.IDbCommand.CommandText property by using a string that is built from a string argument to the method. This rule assumes that the string argument contains user input. A SQL command string that is built from user input is vulnerable to SQL injection attacks. |
193194
> | [CA2101: Specify marshalling for P/Invoke string arguments](ca2101.md) | A platform invoke member allows partially trusted callers, has a string parameter, and does not explicitly marshal the string. This can cause a potential security vulnerability. |
194195
> | [CA2109: Review visible event handlers](ca2109.md) | A public or protected event-handling method was detected. Event-handling methods should not be exposed unless absolutely necessary. |

docs/fundamentals/code-analysis/quality-rules/reliability-warnings.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,3 +34,4 @@ Reliability rules support library and application reliability, such as correct m
3434
| [CA2020: Prevent behavioral change caused by built-in operators of IntPtr/UIntPtr](ca2020.md) | Some built-in operators added in .NET 7 behave differently than the user-defined operators in .NET 6 and earlier versions. Some operators that used to throw in unchecked context while overflowing don't throw anymore unless wrapped within checked context. Some operators that previously didn't throw in checked context now throw unless wrapped within unchecked context. |
3535
| [CA2021: Don't call Enumerable.Cast\<T> or Enumerable.OfType\<T> with incompatible types](ca2021.md) | A call to <xref:System.Linq.Enumerable.Cast%60%601(System.Collections.IEnumerable)?displayProperty=nameWithType> or <xref:System.Linq.Enumerable.OfType%60%601(System.Collections.IEnumerable)?displayProperty=nameWithType> specifies a type parameter that's incompatible with the type of the input collection. |
3636
| [CA2022: Avoid inexact read with Stream.Read](ca2022.md) | A call to `Stream.Read` might return fewer bytes than requested, resulting in unreliable code if the return value isn't checked. |
37+
| [CA2024: Do not use StreamReader.EndOfStream in async methods](ca2024.md) | The property <xref:System.IO.StreamReader.EndOfStream?displayProperty=nameWithType> can cause unintended synchronous blocking when no data is buffered. Instead, use <xref:System.IO.StreamReader.ReadLineAsync?displayProperty=nameWithType> directly, which returns `null` when reaching the end of the stream. |

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1147,6 +1147,8 @@ items:
11471147
href: ../../fundamentals/code-analysis/quality-rules/ca2021.md
11481148
- name: CA2022
11491149
href: ../../fundamentals/code-analysis/quality-rules/ca2022.md
1150+
- name: CA2024
1151+
href: ../../fundamentals/code-analysis/quality-rules/ca2024.md
11501152
- name: Security rules
11511153
items:
11521154
- name: Overview

0 commit comments

Comments
 (0)