1

We host an Asp.Net Core website that many users visit and it is hosted on Azure. However, we get this error a lot of times and the application goes down:

'The specified CGI application encountered an error and the server terminated the process'

After contacting Microsoft we discovered that the application is making a lot of HTTPS calls through port 443 and we exceed the number of maximum allowed outbound connections.

We use HTTPS to send passport reset e-mail, Redis Cache, Azure Cloud Storage Upload and Sending Firebase notifications. Redis is handled in a static method

We declare the other services in Startup.CS like this:

    services.AddSingleton<ClientService>();
    services.AddSingleton<UploaderService>();
    services.AddSingleton<SharingService>();
    services.AddSingleton<EmailSMSUtility>();
    services.AddSingleton<PushNotificationUtility>();

We are thinking that changing these services to Scoped or Transient might help. Can you kindly suggest if we can do that to any of them and if that helps solving the problem?

Thanks a lot

Update: Please see the support engineers reply below:

At time of the error, we see a huge number of outbound connection to port 443 (HTTPS connections). Your website is running in Large instance. In large instance, you can only have 8192 outbound connection at any point of time. As most of this quota is used by your application, there is no room for new incoming request. That’s the reason, the new incoming requests are getting HTTP 502.3. I don’t believe there is a socket leak within your application. Due to the load, your website is making huge number of outbound connections and closing them. If the load is high on your website, and each request start to make an outbound call, you will be getting close to the outbound quota and hence the new incoming requests will have issue, end users will start to see HTTP 502.3 errors. Can you please check your code and see which external resource you are talking to on port 443. If you are using .NET HTTPClient to make these HTTPS calls, make sure you use static object (instead of creating a new instance every time). Static object of HTTPClient will reuse the socket connections: https://stackoverflow.com/a/22561368

Techy
  • 2,026
  • 4
  • 20
  • 41
  • I don't see how changing the object's scope will change the number of HTTPS calls that you are making. Sounds like you will still be making these calls regardless. – Andy T Jul 14 '17 at 23:16
  • @AndrésNava-.NET Do you have any other suggestion to solve this issue? Thank you – Techy Jul 14 '17 at 23:17
  • 1
    Why do you think putting Scoped or Transient will help? Calling the same object 5000 times or calling 5000 objects once will make the same number of calls. You should log which object generate the most calls and maybe serialize (or queue your calls) – hugo Jul 14 '17 at 23:18
  • @hugorgor Please see my edit above. Thank you – Techy Jul 14 '17 at 23:21
  • @ZainAlabdinTawfiq if you are sure that your code is thread-safe or not called from several thread, you can use singleton – hugo Jul 14 '17 at 23:26
  • @hugorgor Do you think MS Support suggestion above would help? – Techy Jul 15 '17 at 00:12
  • Rewrite your software to do a smaller number of requests but with larger payloads. Changing your service lifetime won't make a difference. Anything that uses HTTPClient you should reuse it as much as possible though (don't eagerly dispose of it) – Mardoxx Jul 15 '17 at 09:18
  • @Mardoxx would using Singleton support re usability? Thank you – Techy Jul 15 '17 at 11:46
  • Depends how it's written – Mardoxx Jul 15 '17 at 15:58
  • @Mardoxx I'm afraid MS observation is invalid, do you know how would the behavior change if I change to transient of scoped? – Techy Jul 16 '17 at 13:15

0 Answers0