The underlying connection was closed: A connection that was expected to be kept alive was closed by the server


Symptoms


If you use .NET search API in high-traffic environments, you may occasionally run into the error "The underlying connection was closed: A connection that was expected to be kept alive was closed by the server." This error comes from the .NET Framework. It means that a query server used by the client has closed the connection (issued a FIN packet) right before the API client sent a query.

Resolution


ESP 5.x search APIs use HTTP 1.1, which translates into utilizing persistent connections. This means that an idle connection does not get terminated right away. The idea is to use the same connection to transport data in the future. These connections cannot stay idle forever, and the .NET Framework as well as a query server may close it. A query server will close the connection after 15 seconds of inactivity. The .NET Framework will close the connection after 100 seconds of inactivity. Given these numbers, it is possible that a query client will try to use a connection that is being closed. The problem is described by Microsoft in the following Knowledge Base article:
You can download the hotfix, and install if this is an option. These are other solutions:
  • Disable keep-alive. This is not recommended because you are going to exactly what HTTP 1.1 was designed to avoid.
  • Decrease the idle timeout in the .NET Framework by changing the value of the  System.Net.ServicePointManager.MaxServicePointIdleTime property. The value should be lower than 15 seconds to force the client to close the connection before the query server does it.
  • Extend the idle timeout in the query server. (Please contact FAST Technical Support if you need to implement this option.)