Getting sporadic SocketExceptions/ExtendedSocketExceptions / Connection Refused using Docker
See original GitHub issueGetting 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)
Issue Analytics
- State:
- Created 5 years ago
- Comments:7 (2 by maintainers)
Top Results From Across the Web
Connection refused on docker container
In some situations the internal process in the docker container is trying to bind to localhost (or 127.0.0.1 explicitly), but this can be...
Read more >Connection refused? Docker networking and how it impacts ...
Learn how to fix connection refused errors when trying to connect to a Docker container.
Read more >Docker connection refused between two containers
I deployed two container in the same network (tried bridge and another that i created to test). When creating the image of container...
Read more >Troubleshooting 'Connection Refused' When Running ...
If your security group doesn't allow incoming connections on the port your application is listening on, you'll get a 'Connection Refused' error.
Read more >Connection refused right after container start · Issue #71
My assumption is that the logic that retrieves mapped port is not blocked by startup await, while in Docker desktop there is a...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
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.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.