Getting the below exception every now and then, any suggestions?
Versions:
ASP.Net Core 2.1.3
Npgsql... 2.1.1.1
Docker-compose 3.4
Docker-compose:
postgres_image:
image: mdillon/postgis
restart: always
volumes:
- pgdata:/var/lib/postgresql/data
- ./postgres/:/docker-entrypoint-initdb.d/
ports:
- "5432"
Exceptions:
First Exception:
Type: ExtendedSocketException
An error occurred using the connection to database 'db' on server 'tcp://postgres_image:5432'.
Following Exception:
An exception occurred in the database while iterating the results of a query for context type 'ApplicationDbContext'.
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 172.18.0.4:5432
at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult)
at System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization)
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.TaskExtensions.WithCancellation(Task task, CancellationToken cancellationToken)
at Npgsql.TaskExtensions.WithTimeout(Task task, NpgsqlTimeout timeout)
at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenDbConnectionAsync(Boolean errorsExpected, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Storage.RelationalConnection.OpenAsync(CancellationToken cancellationToken, Boolean errorsExpected)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.BufferlessMoveNext(DbContext _, Boolean buffer, CancellationToken cancellationToken)
at Npgsql.EntityFrameworkCore.PostgreSQL.Storage.Internal.NpgsqlExecutionStrategy.ExecuteAsync[TState,TResult](TState state, Func`4 operation, Func`4 verifySucceeded, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncQueryingEnumerable`1.AsyncEnumerator.MoveNext(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.FirstOrDefault_[TSource](IAsyncEnumerable`1 source, CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.TaskResultAsyncEnumerable`1.Enumerator.MoveNext(CancellationToken cancellationToken)
at System.Linq.AsyncEnumerable.SelectEnumerableAsyncIterator`2.MoveNextCore(CancellationToken cancellationToken) at System.Linq.AsyncEnumerable.AsyncIterator`1.MoveNext(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.AsyncLinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext(CancellationToken cancellationToken)
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.ExecuteSingletonAsyncQuery[TResult](QueryContext queryContext, Func`2 compiledQuery, IDiagnosticsLogger`1 logger, Type contextType)
Are you configuring for connection resiliency?
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (111): Connection refused 172.18.0.4:5432
If you increase your connection timeout (+30 or so), can you still reproduce this?
I am going to try that and will let you know, it happens sporadically so it will take a few days to establish the result. Thanks much.
This issue seems to be under control now. Will re-open if it happens again. Thanks
If anyone ends up searching for this, check your connection string. I faced a the following exception as well:
Error Message:
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException : Resource temporarily unavailable
Stack Trace:
at System.Net.Dns.InternalGetHostByName(String hostName)
at System.Net.Dns.GetHostAddresses(String hostNameOrAddress)
at Npgsql.NpgsqlConnector.Connect(NpgsqlTimeout timeout) in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 663
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 555
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken) in C:\projects\npgsql\src\Npgsql\NpgsqlConnector.cs:line 414
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext() in C:\projects\npgsql\src\Npgsql\NpgsqlConnection.cs:line 274
It was because I made a stupid mistake of having a connection string like Server={host}:{port} instead of the correct Host={host};Port={port} format. Npgsql does pick up Server, but it does not parse the port, so you'll end up with looking up the whole string from DNS.
I still see this with the latest version of Npgsql. It happens if the database engine is shutting down when opening the connection. Npgsql should wrap it and treat it as a transient error.
ExtendedSocketException - Cannot assign requested address /run/postgresql/.s.PGSQL.5432
System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (99): Cannot assign requested address /run/postgresql/.s.PGSQL.5432
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
at System.Net.Sockets.Socket.UnsafeBeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state, Boolean flowContext)
at System.Net.Sockets.Socket.BeginConnect(EndPoint remoteEP, AsyncCallback callback, Object state)
at System.Threading.Tasks.TaskFactory`1.FromAsyncImpl[TArg1](Func`4 beginMethod, Func`2 endFunction, Action`1 endAction, TArg1 arg1, Object state, TaskCreationOptions creationOptions)
at Npgsql.NpgsqlConnector.ConnectAsync(NpgsqlTimeout timeout, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnector.RawOpen(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnector.Open(NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.ConnectorPool.AllocateLong(NpgsqlConnection conn, NpgsqlTimeout timeout, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlConnection.<>c__DisplayClass32_0.<<Open>g__OpenLong|0>d.MoveNext()
@admilazz opened #901 to have connection errors wrapped in NpgsqlException.
Thank you.
Most helpful comment
If anyone ends up searching for this, check your connection string. I faced a the following exception as well:
It was because I made a stupid mistake of having a connection string like
Server={host}:{port}instead of the correctHost={host};Port={port}format. Npgsql does pick upServer, but it does not parse the port, so you'll end up with looking up the whole string from DNS.