Skip to content

Commit 3635c3f

Browse files
authored
CA2025 Analyzer documentation (#46051)
1 parent 68d7eed commit 3635c3f

File tree

4 files changed

+86
-0
lines changed

4 files changed

+86
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
---
2+
title: "CA2025: Do not pass 'IDisposable' instances into unawaited tasks"
3+
description: "Learn about code analysis rule CA2025 - Do not pass 'IDisposable' instances into unawaited tasks"
4+
ms.date: 05/08/2025
5+
ms.topic: reference
6+
f1_keywords:
7+
- CA2025
8+
- DoNotPassDisposablesIntoUnawaitedTasksAnalyzer
9+
helpviewer_keywords:
10+
- CA2025
11+
author: steveberdy
12+
dev_langs:
13+
- CSharp
14+
---
15+
16+
# CA2025: Do not pass 'IDisposable' instances into unawaited tasks
17+
18+
| Property | Value |
19+
|-------------------------------------|------------------------------------------------------|
20+
| **Rule ID** | CA2025 |
21+
| **Title** | Do not pass 'IDisposable' instances into unawaited tasks |
22+
| **Category** | [Reliability](reliability-warnings.md) |
23+
| **Fix is breaking or non-breaking** | Non-breaking |
24+
| **Enabled by default in .NET 10** | As warning |
25+
26+
## Cause
27+
28+
An `IDisposable` instance is passed into an unawaited task and potentially disposed before the task is finished using the instance.
29+
30+
## Rule description
31+
32+
Unawaited tasks that use `IDisposable` instances might use those instances long after they've been disposed. Ensure tasks using those instances are completed before the instances are disposed.
33+
34+
## Examples
35+
36+
The following code snippets (and their Visual Basic equivalents) are violations of CA2025:
37+
38+
```csharp
39+
public Task DoSomethingAsync()
40+
{
41+
// Using statements and using blocks can both be violations.
42+
using (var disposable = new DisposableThing())
43+
{
44+
return DoSomethingInternalAsync(disposable);
45+
}
46+
}
47+
```
48+
49+
```csharp
50+
public async Task DoThingsAsync()
51+
{
52+
var disposable = new DisposableThing();
53+
var task = DoSomethingInternalAsync(disposable);
54+
// More code here.
55+
dispose.Dispose();
56+
// It's a violation if arguments are disposed before the task is awaited.
57+
await task.ConfigureAwait(false);
58+
}
59+
```
60+
61+
## When to suppress warnings
62+
63+
Suppress these warnings if you know tasks finish using `IDisposable` instances before they're disposed.
64+
65+
## Suppress a warning
66+
67+
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
68+
69+
```csharp
70+
#pragma warning disable CA2025
71+
// The code that's violating the rule is on this line.
72+
#pragma warning restore CA2025
73+
```
74+
75+
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md).
76+
77+
```ini
78+
[*.{cs,vb}]
79+
dotnet_diagnostic.CA2025.severity = none
80+
```
81+
82+
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
@@ -190,6 +190,7 @@ The following table lists code quality analysis rules.
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. |
192192
> | [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. |
193+
> | [CA2025: Do not pass `IDisposable` instances into unawaited tasks](ca2025.md) | Unawaited tasks that use `IDisposable` instances might use those instances long after they have been disposed. Ensure tasks using those instances are completed before the instances are disposed. |
193194
> | [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. |
194195
> | [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. |
195196
> | [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
@@ -35,3 +35,4 @@ Reliability rules support library and application reliability, such as correct m
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. |
3737
| [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. |
38+
| [CA2025: Do not pass `IDisposable` instances into unawaited tasks](ca2025.md) | Unawaited tasks that use `IDisposable` instances might use those instances long after they have been disposed. Ensure tasks using those instances are completed before the instances are disposed. |

docs/navigate/tools-diagnostics/toc.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1170,6 +1170,8 @@ items:
11701170
href: ../../fundamentals/code-analysis/quality-rules/ca2022.md
11711171
- name: CA2024
11721172
href: ../../fundamentals/code-analysis/quality-rules/ca2024.md
1173+
- name: CA2025
1174+
href: ../../fundamentals/code-analysis/quality-rules/ca2025.md
11731175
- name: Security rules
11741176
items:
11751177
- name: Overview

0 commit comments

Comments
 (0)