Skip to content

Regression: Derived FileStream no longer calls Close in DisposeAsync #82176

@pharring

Description

@pharring

Description

I have a class derived from FileStream with an overridden Close method.

In .NET 6, prior to 6.0.14, the overridden Close method would be called from DisposeAsync so that await using new FileCacheFileStream(...) would ensure my derived Close method is called.

However, this stopped working in 6.0.14
Probably related to #80439

Reproduction Steps

using System;
using System.IO;
using System.Threading.Tasks;

class Program
{
    static async Task Main(string[] args)
    {
        await using MyStream file = new(Path.GetTempFileName(), FileMode.Open);
    }
}

class MyStream : FileStream
{
    public MyStream(string path, FileMode mode) : base(path, mode)
    {
    }

    public override void Close()
    {
        Console.WriteLine("Close called.");
        base.Close();
    }
}

In .NET Core 3.1, .NET 5 and .NET 6, prior to 6.0.14, the Close method would be called from DisposeAsync so that await using new FileCacheFileStream(...) would ensure my derived Close method is called.

However, this stopped working in 6.0.14
Probably related to #80439

Expected behavior

The program should output "Close called."

Actual behavior

The program exits with no output.

Regression?

Yes
Worked in .NET 6 prior to 6.0.14

Known Workarounds

Override DisposeAsync in the derived class and call Close. However, the difficulty with that is knowing whether Close has already been called via another code path (i.e. if the internal "IsClosed" property is set)

Configuration

.NET 6.0.14
Windows 11
x64

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions