0

I have a problem where result ignores the wait timeout of 5000ms and returns it when .Result is done... This is causing my IIS to crash since none of the requests made to the IIS has a timeout and creates a very long queue and execution time.

EDIT: Clarification: It should return "123;NK" if the wait exceeds 5000ms.

public class Client : IMotorClient
    {
        private IMotorProxy Link { get; set; }

        //private IOfficeGenerator OfficeGenerator { get; set; }

        private IOfficeRegistryLegacy OfficeRegistry { get; set; }

        public WebInternalGatewayClient(IMotorProxy proxy, IOfficeRegistryLegacy officeRegistry)
        {
            OfficeRegistry = officeRegistry;
            Link = proxy;
        }

        public string Execute(string host, int port, string command)
        {
            var officeGuid = OfficeRegistry.GetOfficeGuid(host, port);

            var result = "";
            var task = Task.Run(() => result = Link.Submit(officeGuid, command).Result);
            var value = task.Wait(TimeSpan.FromMilliseconds(5000));

            if (!value)
            {
                result = "123;NK;";
            }

            return result;
        }
    }
Andreas012
  • 31
  • 3
  • 5
    We don't know the types you're working with but that `.Result` access looks suspicious to me. Please try to create a [mcve] – Damien_The_Unbeliever Mar 15 '21 at 11:19
  • I'd imagine that `Link.Submit(officeGuid, command).Result` is deadlocking. You should probably be `await`ing this – Liam Mar 15 '21 at 11:22
  • @Andreas012, just to clarify, is your intention to wait 5 secs before executing the task, and then wait for the task to complete (before checking the value)? – Just Shadow Mar 15 '21 at 11:33
  • @JustShadow My intention is; if the Wait exceeds 5 secs return "123;NK". Thanks! – Andreas012 Mar 15 '21 at 11:40
  • 5
    fundamentally, your `Execute` method is synchronous; anything you do, even with a timeout, is still "sync over async", which is to say: a terrible terrible idea – Marc Gravell Mar 15 '21 at 11:44
  • Isn't your `Link.Submit(...) ` already returning a `Task` that you can wait for ? – XouDo Mar 15 '21 at 12:06
  • 2
    The IIS is probably crashing because the `ThreadPool` runs out of available threads. Which is caused by your liberal use of `Task.Run`s and blocking waitings. What you should do is to change the `Execute` method to `ExecuteAsync`, having a `Task` return value. And then search inside your project for instances of `Task.Run(`, `.Result` and `.Wait(`, and eliminate them one by one. – Theodor Zoulias Mar 15 '21 at 12:08
  • 2
    1. [Use `async` all the way](https://learn.microsoft.com/en-us/archive/msdn-magazine/2013/march/async-await-best-practices-in-asynchronous-programming). 2. [Don't use `Task.Run` on ASP.NET](https://learn.microsoft.com/en-us/archive/msdn-magazine/2014/october/async-programming-introduction-to-async-await-on-asp-net). Once these issues are resolved, *then* try to add timeouts and ask a question if it doesn't work properly. – Stephen Cleary Mar 15 '21 at 14:02

0 Answers0