Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RequestTimeouts not works as expected #50607

Closed
1 task done
WeihanLi opened this issue Sep 9, 2023 · 2 comments
Closed
1 task done

RequestTimeouts not works as expected #50607

WeihanLi opened this issue Sep 9, 2023 · 2 comments
Labels
needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically

Comments

@WeihanLi
Copy link
Contributor

WeihanLi commented Sep 9, 2023

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

RequestTimeouts not working, when I try out the request timeouts by following the document here
https://learn.microsoft.com/en-us/aspnet/core/performance/timeouts?view=aspnetcore-8.0

it's not working as expected, no timeout while it should timeout

Expected Behavior

timeout

Steps To Reproduce

test code as below:

using Microsoft.AspNetCore.Http.Timeouts;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRequestTimeouts(options =>
{
    options.DefaultPolicy = new RequestTimeoutPolicy()
    {
        Timeout = TimeSpan.FromSeconds(1),
        WriteTimeoutResponse = context => context.Response.WriteAsync("timeout")
    };
    options.AddPolicy("timeout-2s", TimeSpan.FromSeconds(2));
});
var app = builder.Build();
app.UseRequestTimeouts();
app.Map("/", () => "Hello world");
app.MapGet("/timeout", async () =>
{
    await Task.Delay(TimeSpan.FromSeconds(5));
    return Results.Content("No timeout!", "text/plain");
});
app.MapGet("/timeout-policy", async () =>
{
    await Task.Delay(TimeSpan.FromSeconds(5));
    return Results.Content("No timeout!", "text/plain");
}).WithRequestTimeout("timeout-2s");
app.MapGet("/timeout-catch", [RequestTimeout(1000)]async () =>
{
    try
    {
        await Task.Delay(TimeSpan.FromSeconds(5));
    } 
    catch (TaskCanceledException)
    {
        return Results.Content("Timeout!", "text/plain");
    }
    return Results.Content("No timeout!", "text/plain");
});
app.Run();

all these cases no timeout

Exceptions (if any)

No response

.NET Version

8.0.100-preview.7.23376.3

Anything else?

.NET SDK:
 Version:   8.0.100-preview.7.23376.3
 Commit:    daebeea8ea

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.25947
 OS Platform: Windows
 RID:         win10-x64
 Base Path:   C:\Program Files\dotnet\sdk\8.0.100-preview.7.23376.3\

.NET workloads installed:
There are no installed workloads to display.

Host:
  Version:      8.0.0-preview.7.23375.6
  Architecture: x64
  Commit:       65b696cf5e
  RID:          win-x64

.NET SDKs installed:
  6.0.408 [C:\Program Files\dotnet\sdk]
  7.0.102 [C:\Program Files\dotnet\sdk]
  7.0.105 [C:\Program Files\dotnet\sdk]
  8.0.100-preview.7.23376.3 [C:\Program Files\dotnet\sdk]

.NET runtimes installed:
  Microsoft.AspNetCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.AspNetCore.App 8.0.0-preview.7.23375.9 [C:\Program Files\dotnet\shared\Microsoft.AspNetCore.App]
  Microsoft.NETCore.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.NETCore.App 8.0.0-preview.7.23375.6 [C:\Program Files\dotnet\shared\Microsoft.NETCore.App]
  Microsoft.WindowsDesktop.App 6.0.13 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.15 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 6.0.16 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 7.0.5 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]
  Microsoft.WindowsDesktop.App 8.0.0-preview.7.23376.1 [C:\Program Files\dotnet\shared\Microsoft.WindowsDesktop.App]

Other architectures found:
  None

Environment variables:
  Not set

global.json file:
  Not found

Learn more:
  https://aka.ms/dotnet/info

Download .NET:
  https://aka.ms/dotnet/download
@dotnet-issue-labeler dotnet-issue-labeler bot added the needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically label Sep 9, 2023
@davidfowl
Copy link
Member

Timeouts in asynchronous code or cooperative. Your code needs to use the RequestAborted cancellation token in order to get notified.

using Microsoft.AspNetCore.Http.Timeouts;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddRequestTimeouts(options =>
{
    options.DefaultPolicy = new RequestTimeoutPolicy()
    {
        Timeout = TimeSpan.FromSeconds(1),
        WriteTimeoutResponse = context => context.Response.WriteAsync("timeout")
    };
    options.AddPolicy("timeout-2s", TimeSpan.FromSeconds(2));
});
var app = builder.Build();
app.UseRequestTimeouts();
app.Map("/", () => "Hello world");
app.MapGet("/timeout", async (CancellationToken token) =>
{
    await Task.Delay(TimeSpan.FromSeconds(5), token);
    return Results.Content("No timeout!", "text/plain");
});
app.MapGet("/timeout-policy", async () =>
{
    await Task.Delay(TimeSpan.FromSeconds(5));
    return Results.Content("No timeout!", "text/plain");
}).WithRequestTimeout("timeout-2s");
app.MapGet("/timeout-catch", [RequestTimeout(1000)] async (CancellationToken token) =>
{
    try
    {
        await Task.Delay(TimeSpan.FromSeconds(5), token);
    } 
    catch (TaskCanceledException)
    {
        return Results.Content("Timeout!", "text/plain");
    }
    return Results.Content("No timeout!", "text/plain");
});
app.Run();

PS: You could that writes the response on timeout should check if the response has already started before doing so.

@WeihanLi
Copy link
Contributor Author

WeihanLi commented Sep 9, 2023

get it, thanks very much @davidfowl

@WeihanLi WeihanLi closed this as completed Sep 9, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Oct 9, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
needs-area-label Used by the dotnet-issue-labeler to label those issues which couldn't be triaged automatically
Projects
None yet
Development

No branches or pull requests

2 participants