All rules
CA2025Reliability Enabled by default: No

Do not pass 'IDisposable' instances into unawaited tasks

Do not pass 'IDisposable' instances into unawaited tasks

Microsoft docs

Description

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.

Cause

An IDisposable instance is passed into an unawaited task and potentially disposed before the task is finished using the instance.

Example

public Task DoSomethingAsync()
{
    // Using statements and using blocks can both be violations.
    using (var disposable = new DisposableThing())
    {
        return DoSomethingInternalAsync(disposable);
    }
}

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

Suppress these warnings if you know tasks finish using IDisposable instances before they're disposed.

Group results
0 yes 0 no
ConsensusNone (disabled)
Severity preference (yes voters)
Suggestion0
Warning0
Error0