Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core request timeout not triggering

I can't get the timeout to automatically trigger in the ASP.NET Core 8.0 Preview.

https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRequestTimeouts();

var app = builder.Build();

app.UseRequestTimeouts();

app.MapGet("/", async (HttpContext context) =>
    {
        try
        {
            await Task.Delay(TimeSpan.FromSeconds(10), context.RequestAborted);
        }
        catch (TaskCanceledException)
        {
            return Results.Content("Timeout!", "text/plain");
        }

        return Results.Content("No timeout.", "text/plain");
    })
    .WithRequestTimeout(TimeSpan.FromSeconds(1));

app.Run();

Expected output: Trigger a timeout after 1 second

Actual output: Returns success after 10 seconds

like image 748
Yorro Avatar asked Sep 10 '25 03:09

Yorro


2 Answers

When you run your application with a debugger attached the request timeout check is skipped.

Microsoft.AspNetCore.Http.Timeouts.RequestTimeoutsMiddleware that is added with UseRequestTimeouts contains the following code:

internal sealed class RequestTimeoutsMiddleware
{
    public Task Invoke(HttpContext context)
    {
        if (Debugger.IsAttached)
        {
            return _next(context);
        }
        ...
    }
}

https://github.com/dotnet/aspnetcore/blob/v8.0.0-preview.5.23302.2/src/Http/Http/src/Timeouts/RequestTimeoutsMiddleware.cs#L31

like image 130
Fsamot Avatar answered Sep 13 '25 04:09

Fsamot


I believe using "Start Without Debugging" will help.

like image 25
Brilliant-DucN Avatar answered Sep 13 '25 06:09

Brilliant-DucN