Skip to content

The 499 status code may be returned when using Request timeouts and Exception Handler together #52295

@marcominerva

Description

@marcominerva

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Depending of the order of the middlewares, when a request timeout occurs, a 499 status code (instead of 504) may be returned:

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRequestTimeouts();

var app = builder.Build();

// If I put the RequestTimeoutsMiddleware here (before Exception Handler), I get a 499 error when a Timeout occurs.
app.UseRequestTimeouts();

app.UseExceptionHandler();

// If I put the RequestTimeoutsMiddleware here (after Exception Handler), I get (as expected) a 504 error when a Timeout occurs.
//app.UseRequestTimeouts();

I think this is caused by this code in the Exception Handler Middleware: https://github.com/dotnet/aspnetcore/blob/main/src/Middleware/Diagnostics/src/ExceptionHandler/ExceptionHandlerMiddlewareImpl.cs#L117-L128

Expected Behavior

When a request timeout occurs, I expected to always get the 504 error (or the one that has been configured in the policy).

Steps To Reproduce

Minimal repro here: https://github.com/marcominerva/TimeoutIssue

Exceptions (if any)

No response

.NET Version

8.0.100

Anything else?

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-middlewareIncludes: URL rewrite, redirect, response cache/compression, session, and other general middlewares

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions