0

I would expect IIS to be able to handle multiple requests to this method at the same time. what actually happens when I test this is that these requests are processed one at a time. What am I missing?

    public async Task<IActionResult> Index()
    {

        await Task.Run(() =>
        {
            Thread.Sleep(10000);
        }); 

        return View();
    }

EDIT: I tried to send the request via Postman instead of Chrome and it beheavies as expected. Request are processed simultaneously. At this point I'm wondering why Chrome is waiting for the previous request to be processed before sending another one. Here below the screenshot from the Chrome network window.

enter image description here

LNyarla
  • 454
  • 1
  • 8
  • 23
  • 2
    `Thread.Sleep()` sleeps the entire thread, and multiple tasks (in our case, multiple async methods) can and are executed on the same thread, what you want (only for testing purposes of course) is `await`[`Task.Delay`](https://learn.microsoft.com/en-us/dotnet/api/system.threading.tasks.task.delay?view=net-5.0) – MindSwipe Sep 28 '21 at 13:12
  • 1
    `handled without blocking further requests.` no. Every request is handled by a different thread so there's no blocking. `async` is used to avoid blocking the request thread while awaiting for async operations like IO to finish. – Panagiotis Kanavos Sep 28 '21 at 13:13
  • 1
    So if you're hosting in IIS then each request is dispatched on the same main thread (which is why async is so critical). The code you show is actuallty blocking because you're forcing that same thread to wait for a task that sleeps... the same main thread. Instead, try `await Task.Delay(10000).ConfigureAwait(false);` – zaitsman Sep 28 '21 at 13:13
  • when I test this is that these requests are processed one at a time.` that's not the case at all. This code starts *another* background thread only to block it. The current request thread isn't blocked but that doesn't help, because another thread from the threadpool is now blocked. IIS keeps serving requests as they arrive and isn't affected by `Thread.Sleep` – Panagiotis Kanavos Sep 28 '21 at 13:14
  • 1
    What's the *real* problem? The question's code doesn't block any requests. It wastes a thread for 10 seconds but that doesn't block other threads *unless* there are so many requests that the threadpool gets exhausted. That's what `async/await` was built to avoid. If `Index { await Task.Delay(10000); return View();}` was used the thread wouldn't be blocked. The response would still take 10 seconds but the thread would be available to serve other requests – Panagiotis Kanavos Sep 28 '21 at 13:16
  • @zaitsman `if you're hosting in IIS then each request is dispatched on the same main thread` definitely not. IIS uses threads from a threadpool to serve requests – Panagiotis Kanavos Sep 28 '21 at 13:18
  • @PanagiotisKanavos any other request to any other controller is handled in parallel but if I call the same action again I have to wait for the previous request to be processed. – LNyarla Sep 28 '21 at 13:25
  • 1
    No you don't. That's not how ASP.NET works. Each request creates a new Controller instance that's disposed as soon as the request ends. Blocking an action only affects a single request. How did you determine that `Thread.Sleep` affects the entire controller? Especially when it *doesn't* affect the request thread? That `await Task.Return(()=>Thread.Sleep());` affects a *different* background thread used to serve the job generated by `Task.Run` – Panagiotis Kanavos Sep 28 '21 at 13:25
  • @PanagiotisKanavos after a further test I noticed that any other action method on the same controller is executed in parallel but I confirm you that only one Index method can run at a time. – LNyarla Sep 28 '21 at 13:59
  • 1
    _"are processed one at a time"_ needs better proof. – H H Sep 29 '21 at 06:26
  • @PanagiotisKanavos is right, I tried to send the request via Postman instead of Chrome and it beheavies as expected. Request are processed simultaneously. At this point I'm wondering why Chrome is waiting for the previous request to be processed before sending another one. – LNyarla Sep 29 '21 at 10:20

1 Answers1

1

This behavior is due to Chrome locking the cache and waiting to see the result of one request before requesting the same resource again. Chrome stalls when making multiple requests to same resource?.

LNyarla
  • 454
  • 1
  • 8
  • 23