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