dotnet / dotnet/aspnetcore

Intermittent ObjectDisposedException in SPA Hosting

Open
#42,608 15 comments 1 reaction 1 assignee Claimed by @adityamandaleeka View on GitHub
area-networking feature-spa investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

I have a small aspnetcore service to host/serve a SPA. Only intermittently, I am getting the following error in the event log.

```
Category: Microsoft.AspNetCore.Server.HttpSys.HttpSysListener
EventId: 6
SpanId: 9e2d8fa91d335924
TraceId: 9590e80341d42cca7b750ac04d095314
ParentId: 0000000000000000
RequestId: 4000002b-0005-ed00-b63f-84710c7967bb
RequestPath: /app/path/to/a/static/file.ext

ProcessRequestAsync

Exception:
System.ObjectDisposedException: ObjectDisposed_Generic
ObjectDisposed_ObjectName_Name, Microsoft.AspNetCore.Server.HttpSys.ResponseBody
at Microsoft.AspNetCore.Server.HttpSys.ResponseBody.CheckDisposed()
at Microsoft.AspNetCore.Server.HttpSys.ResponseBody.WriteAsync(Byte[] buffer, Int32 offset, Int32 count, CancellationToken cancellationToken)
at System.IO.Stream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
at Microsoft.AspNetCore.Server.HttpSys.ResponseStream.WriteAsync(ReadOnlyMemory`1 buffer, CancellationToken cancellationToken)
at System.IO.Pipelines.StreamPipeWriter.FlushAsyncInternal(Boolean writeToStream, ReadOnlyMemory`1 data, CancellationToken cancellationToken)
at System.IO.Pipelines.StreamPipeWriter.CompleteAsync(Exception exception)
at Microsoft.AspNetCore.Server.HttpSys.RequestContext.CompleteAsync()
at Microsoft.AspNetCore.Server.HttpSys.RequestContext`1.ExecuteAsync()
at Microsoft.AspNetCore.Server.HttpSys.RequestContext`1.ExecuteAsync()
```

However, the app runs in the browser without any issue. This problem is more prominent when running the automated e2e tests (using Playwright).

This is how I am configuring the SPA static files.

```csharp
public class Startup
{
public void ConfigureServices(IServiceCollection services)
{
services.AddSpaStaticFiles(opt =>
{
opt.RootPath = _Configuration.GetValue("Webroot", "./Content");
});
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env, ILogger logger, IConfiguration config)
{
app.UseSpaStaticFiles(new StaticFileOptions
{
OnPrepareResponse = context =>
{
if (context.Context.Response.HasStarted || context.Context.Response.StatusCode != (int)HttpStatusCode.OK) return;
context.Context.Response.Headers["Cache-Control"] = "public, max-age=1000";
context.Context.Response.Headers["Vary"] = "Accept-Encoding";
}
});
}
}
```

### Expected Behavior

The superfluous (as there is no error at the client/browser) error log should be avoided.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

6.0.301

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.