Reputation: 21599
Consider these two controller methods:
public async Task DoAsync() {
await TaskThatRunsFor10MinutesAsync().ConfigureAwait(false);
}
public void DoWithTaskRunAndReturnEarly() {
Task.Run(() => TaskThatRunsFor10MinutesAsync());
}
Let's say on the client side we don't care about the response. We might wait for it to complete, we might abandon it half-way due user refreshing the page -- but it doesn't matter that DoWithTaskRunAndReturnEarly
returns earlier.
Is there an important server-side difference between those two approaches, e.g. in reliability, timeouts, etc? Does ASP.NET Core ever abort threads on a timeout?
Upvotes: 9
Views: 818
Reputation: 38764
In ASP.NET Core 1.1 the socket will stay in the CLOSE_WAIT state until the application Task
completes. That could lead to resource exhaustion on the server side if you have lots of long running tasks on the server where the client has already disconnected (refreshed the page in your case).
Is there an important server-side difference between those two approaches, e.g. in reliability, timeouts, etc? Does ASP.NET Core ever abort threads on a timeout?
Thread aborts aren't a "thing" in .NET Core anymore and wouldn't work for async code anyways (there's no active thread when you're awaiting).
In the next version of ASP.NET Core (2.x at time of writing) the RequestAborted cancellation token trigger when the client socket sends a FIN so you'll be able to react to clients disconnecting to cancel long running work. On top of that, it will close the socket even before the application completes.
Upvotes: 6