npgsql: NpgsqlException - Exception while reading from stream
I am getting random error messages in my application log with the following stack trace. Most of the time everything is working well. I get about 10 errors and log messages like this a day in the production system. How do I figure out what is going on?
The issue
ERROR [18]
Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
--- End of inner exception stack trace ---
at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size)
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass31_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
at Npgsql.NpgsqlReadBuffer.<>c__DisplayClass31_0.<<Ensure>g__EnsureLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlConnector.<>c__DisplayClass161_0.<<ReadMessage>g__ReadMessageLong|0>d.MoveNext()
--- End of stack trace from previous location where exception was thrown ---
at Npgsql.NpgsqlDataReader.NextResult(Boolean async, Boolean isConsuming)
at Npgsql.NpgsqlDataReader.NextResult()
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior, Boolean async, CancellationToken cancellationToken)
at Npgsql.NpgsqlCommand.ExecuteDbDataReader(CommandBehavior behavior)
at System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior)
at Dapper.SqlMapper.ExecuteReaderWithFlagsFallback(IDbCommand cmd, Boolean wasClosed, CommandBehavior behavior) in C:\projects\dapper\Dapper\SqlMapper.cs:line 1051
at Dapper.SqlMapper.QueryImpl[T](IDbConnection cnn, CommandDefinition command, Type effectiveType)+MoveNext() in C:\projects\dapper\Dapper\SqlMapper.cs:line 1079
at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
at Dapper.SqlMapper.Query[T](IDbConnection cnn, String sql, Object param, IDbTransaction transaction, Boolean buffered, Nullable`1 commandTimeout, Nullable`1 commandType) in C:\projects\dapper\Dapper\SqlMapper.cs:line 721
at Dapper.FastCrud.SqlStatements.GenericSqlStatements`1.BatchSelect(IDbConnection connection, AggregatedSqlStatementOptions`1 statementOptions) in D:\a\1\s\Dapper.FastCrud\SqlStatements\GenericSqlStatements.cs:line 297
at XXX.Core.Data.SqlRepository`1.FirstOrDefault(String where, Object param, String order)
at XXXXX.App.Worker.Tasks.XXX.Process() in C:\Dropbox\dev\Src\shop\XXX\Tasks\ShopifyWebhookTask.cs:line 13
at XXX.App.Worker.TaskBase`1.ProcessMessage(String message) in C:\Dropbox\dev\Src\shop\XXX\TaskBase.cs:line 30
at Shop.Core.Common.Queue.BaseTaskProcessor`1.Process(QueueMessage message) in C:\Dropbox\dev\Src\shop\XXX\Queue\BaseTaskProcessor.cs:line 38
Exception message:
Stack trace:

Npgsql version: 4.0.10 PostgreSQL version: 10.10 Operating system: Azure Windows Function + Azure Managed PostgresSql.
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Comments: 19 (4 by maintainers)
I’m using 4.1.2 now and still getting the same behaviour, gonna try to reproduce it locally and debug.
Guys, the error message is saying that PostgreSQL closed the connection for some reason. In these cases, the PostgreSQL log almost always contains a message that provides insight into why that happened - please post those messages here and we can investigate further, otherwise there’s no enough information.