Skip to content

Intermittent ObjectDisposedException in SPA Hosting #42608

@Sayan751

Description

@Sayan751

Is there an existing issue for this?

  • 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.

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<Startup> 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

Metadata

Metadata

Labels

area-networkingIncludes servers, yarp, json patch, bedrock, websockets, http client factory, and http abstractionsfeature-spainvestigate

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions