Skip to content

Commit f631159

Browse files
Merge pull request #46680 from dotnet/main
Merge main into live
2 parents 683da33 + 3635c3f commit f631159

File tree

6 files changed

+93
-7
lines changed

6 files changed

+93
-7
lines changed

docs/core/testing/unit-testing-with-dotnet-test.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -90,12 +90,12 @@ To address the issues encountered when running `dotnet test` with MTP in VSTest
9090
To enable this mode, add a `dotnet.config` file to the root of the repository or solution.
9191
9292
```ini
93-
[dotnet.test:runner]
93+
[dotnet.test.runner]
9494
name = "Microsoft.Testing.Platform"
9595
```
9696
97-
> [!NOTE]
98-
> The format will change from `dotnet.test:runner` to `dotnet.test.runner` in .NET 10 SDK Preview 4.
97+
> [!IMPORTANT]
98+
> The `dotnet test` experience for MTP is only supported in `Microsoft.Testing.Platform` version 1.7 and later.
9999
100100
Since this mode is specifically designed for Microsoft.Testing.Platform, neither `TestingPlatformDotnetTestSupport` nor the additional `--` are required.
101101

docs/core/tools/dotnet-test.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,17 @@ The `dotnet test` command builds the solution and runs the tests with either VST
1616
Some examples of the `dotnet.config` file:
1717

1818
```ini
19-
[dotnet.test:runner]
19+
[dotnet.test.runner]
2020
name = "Microsoft.Testing.Platform"
2121
```
2222

2323
```ini
24-
[dotnet.test:runner]
24+
[dotnet.test.runner]
2525
name = "VSTest"
2626
```
2727

28-
> [!NOTE]
29-
> The format will change from `dotnet.test:runner` to `dotnet.test.runner` in .NET 10 SDK Preview 4.
28+
> [!IMPORTANT]
29+
> The `dotnet test` experience for MTP is only supported in `Microsoft.Testing.Platform` version 1.7 and later.
3030
3131
> [!TIP]
3232
> For conceptual documentation about `dotnet test`, see [Testing with dotnet test](../testing/unit-testing-with-dotnet-test.md).
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)