Reputation: 1900
We are getting the following exception which looks like it is originating from the Npgsql.dll
.
We use this DLL
in our ASP.Net
C#
Application hosted on a Ubuntu 14.04
server under Apache
mod_mono
. That DLL
is responsible for talking to the Postgresql Server
. We have the latest version of Postgresql
installed on the server.
This error happens randomly then crash the server.
System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> Npgsql.NpgsqlException:
Failed to establish a connection to 'localhost'.
at Npgsql.NpgsqlClosedState.Open (Npgsql.NpgsqlConnector context, Int32 timeout) [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlConnector.Open () [0x00000] in <filename unknown>:0
at Npgsql.NpgsqlConnectorPool.GetPooledConnector (Npgsql.NpgsqlConnection Connection) [0x00000] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Web.UI.Page.ProcessException (System.Exception e) [0x00000] in <filename unknown>:0
at System.Web.UI.Page.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:0
at ASP.default_aspx.ProcessRequest (System.Web.HttpContext context) [0x00000] in <filename unknown>:0
at System.Web.HttpApplication+<Pipeline>c__Iterator1.MoveNext () [0x00000] in <filename unknown>:0
at System.Web.HttpApplication.Tick () [0x00000] in <filename unknown>:0
System.TimeoutException: Dns hostname lookup timeout. Increase Timeout value in ConnectionString.
at Npgsql.NpgsqlClosedState.Open (Npgsql.NpgsqlConnector context, Int32 timeout) [0x00000] in <filename unknown>:0
Any help with figuring out what causing this error will be much appreciated.
Upvotes: 2
Views: 1992
Reputation: 33
You should run your application like this
MONO_THREADS_PER_CPU=50 mono MyApp.exe
The reason is the use of ThreadPool in npgsql version 2.2.7, and also because the socket in also uses a ThreadPool. Npgsql uses the GetHostEntryAsync method to resolve host names when opening a connection that uses a ThreadPool. Even if you specify ip explicitly in the connection string, npgsql will call the BeginConnect socket method, which is asynchronous and will use a ThreadPool. In mono by default the number of threads in the pool per core of CPU is 1. While in .net there is other algorithm. As a result, the threads in the pool begin to run short and the mono environment starts to issue them slowly.
When there are few threads per core, the pool does not issue the existing ready threads and creates a new one, and creation takes a long time. With the MONO_THREADS_PER_CPU parameter, we tell the mono runtime to keep ready in the pool N * (number of cores) threads
Upvotes: 0
Reputation: 11273
Looks like the DNS hostname lookup for "localhost" is timing out. Maybe instead of using DNS use the loopback IP 127.0.0.1 in the connection string.
Upvotes: 1