|
| 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). |
0 commit comments