-
Notifications
You must be signed in to change notification settings - Fork 6k
CA2025 Analyzer documentation #46051
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
--- | ||
title: "CA2025: Do not pass 'IDisposable' instances into unawaited tasks" | ||
description: "Learn about code analysis rule CA2025 - Do not pass 'IDisposable' instances into unawaited tasks" | ||
ms.date: 05/08/2025 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA2025 | ||
- DoNotPassDisposablesIntoUnawaitedTasksAnalyzer | ||
helpviewer_keywords: | ||
- CA2025 | ||
author: steveberdy | ||
dev_langs: | ||
- CSharp | ||
--- | ||
|
||
# CA2025: Do not pass 'IDisposable' instances into unawaited tasks | ||
|
||
| Property | Value | | ||
|-------------------------------------|------------------------------------------------------| | ||
| **Rule ID** | CA2025 | | ||
| **Title** | Do not pass 'IDisposable' instances into unawaited tasks | | ||
| **Category** | [Reliability](reliability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
| **Enabled by default in .NET 10** | As warning | | ||
|
||
## Cause | ||
|
||
An `IDisposable` instance is passed into an unawaited task and potentially disposed before the task is finished using the instance. | ||
|
||
## Rule description | ||
|
||
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. | ||
|
||
## Examples | ||
|
||
The following code snippets (and their Visual Basic equivalents) are violations of CA2025: | ||
|
||
```csharp | ||
public Task DoSomethingAsync() | ||
{ | ||
// Using statements and using blocks can both be violations. | ||
using (var disposable = new DisposableThing()) | ||
{ | ||
return DoSomethingInternalAsync(disposable); | ||
} | ||
} | ||
``` | ||
|
||
```csharp | ||
public async Task DoThingsAsync() | ||
{ | ||
var disposable = new DisposableThing(); | ||
var task = DoSomethingInternalAsync(disposable); | ||
// More code here. | ||
dispose.Dispose(); | ||
// It's a violation if arguments are disposed before the task is awaited. | ||
await task.ConfigureAwait(false); | ||
} | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
Suppress these warnings if you know tasks finish using `IDisposable` instances before they're disposed. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA2025 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA2025 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA2025.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.