Skip to content

Stream returned in route is disposed twice #59514

@jeremiahpopebiomerieux

Description

@jeremiahpopebiomerieux

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

When returning a stream as a file while creating a web API, the stream is disposed twice.

Expected Behavior

The stream should only be disposed once.

Steps To Reproduce

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
var app = builder.Build();

app.Map("/test", () =>
{
    var stream = new MyStream();
    return Results.File(stream);
});

app.Run();

internal class MyStream : MemoryStream
{
    private int _count;
    protected override void Dispose(bool disposing)
    {
        _count++;
        Console.WriteLine(_count);
        base.Dispose(disposing);
    }
}

I can also reproduce while using FileStreamResult.

Exceptions (if any)

No response

.NET Version

9.0.101

Anything else?

There is no stack trace.

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etc

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions