Npgsql.NpgsqlException (0x80004005): Exception while reading from stream

See original GitHub issue

The issue

When I try to retrieve too much data from database I get the errors below. This did not happen on .net core 3.1. I downgraded just the bare minimum packages today to confirm and after I downgraded I did not have this issue.

Very important point

I use geography column of NetTopologySuite.

My issue happens when I retrieve about 30 columns and 2 of them are geography column. When I omit the geography columns I can easily retrieve 500 rows without a problem. I don’t know how many rows would work.

When I have the 2 geography columns I can only get about 30 rows before I get one of the errors below. More than that never works.

Though I do have EntityFrameworkCore package, only Asp.Net Identity uses it. My query runs on Dapper.

Here’s the diff that is before / after my issue (this was the bare minimum I had to change to be able to do the downgrade).

-    <TargetFramework>netcoreapp3.1</TargetFramework>
+    <TargetFramework>net5.0</TargetFramework>
-    <PackageReference Include="Accelist.FluentValidation.Blazor" Version="3.0.0" />
+    <PackageReference Include="Accelist.FluentValidation.Blazor" Version="4.0.0" />
-    <PackageReference Include="LoreSoft.Blazor.Controls" Version="3.5.0.27" />
+    <PackageReference Include="LoreSoft.Blazor.Controls" Version="4.0.0.84" />
-    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="3.1.10" />
+    <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" />
-    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="3.1.10" />
+    <PackageReference Include="Microsoft.AspNetCore.Diagnostics.EntityFrameworkCore" Version="5.0.1" />
-    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="3.1.10" />
+    <PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.1" />
-    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="3.1.10" />
+    <PackageReference Include="Microsoft.AspNetCore.Identity.UI" Version="5.0.1" />
-    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="3.1.10" />
+    <PackageReference Include="Microsoft.Extensions.Logging.Debug" Version="5.0.0" />
-    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="3.1.4" />
+    <PackageReference Include="Microsoft.VisualStudio.Web.CodeGeneration.Design" Version="5.0.1" />
-    <PackageReference Include="Npgsql" Version="4.1.6" />
+    <PackageReference Include="Npgsql" Version="5.0.1" />
-    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="3.1.4" />
+    <PackageReference Include="Npgsql.EntityFrameworkCore.PostgreSQL" Version="5.0.0" />
-    <PackageReference Include="Npgsql.NetTopologySuite" Version="4.1.6" />
+    <PackageReference Include="Npgsql.NetTopologySuite" Version="5.0.1" />

This is the exception I usually get, but then occasionally my app waits 30 seconds and then I get timeout exception

System.Data.DataException: Exception while reading from stream
 ---> Npgsql.NpgsqlException (0x80004005): Exception while reading from stream
 ---> System.IO.EndOfStreamException: Attempted to read past the end of the stream.
   at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
   at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
   at Npgsql.TypeHandling.NpgsqlSimpleTypeHandler`1.Read[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
   at Npgsql.TypeHandling.NpgsqlTypeHandler`1.ReadAsObject(NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
   at Npgsql.NpgsqlDataReader.GetValue(Int32 ordinal)
   at Deserialize8e9e87b0-6954-4120-85bc-5be376fa8874(IDataReader )
   --- End of inner exception stack trace ---
   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3665
   at Deserialize8e9e87b0-6954-4120-85bc-5be376fa8874(IDataReader )
   at Dapper.SqlMapper.<>c__DisplayClass161_0`1.<GenerateMapper>b__0(IDataReader r)
   at Dapper.SqlMapper.MultiMapImpl[TReturn](IDbConnection cnn, CommandDefinition command, Type[] types, Func`2 map, String splitOn, IDataReader reader, Identity identity, Boolean finalize)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.MultiMapAsync[TReturn](IDbConnection cnn, CommandDefinition command, Type[] types, Func`2 map, String splitOn)
   at SLCRoad.Data.BaseAccess`2.QueryAsync[T](Query query, SqlQueryOptionsGeneric`1 sqlQueryOptions, IDbTransaction dbTransaction)

Here’s the timeout exception

System.Data.DataException: Exception while reading from stream
 ---> Npgsql.NpgsqlException (0x80004005): Exception while reading from stream
 ---> System.TimeoutException: Timeout during reading attempt
   at Npgsql.NpgsqlReadBuffer.<Ensure>g__EnsureLong|40_0(NpgsqlReadBuffer buffer, Int32 count, Boolean async, Boolean readingNotifications)
   at Npgsql.TypeHandling.NpgsqlSimpleTypeHandler`1.Read[TAny](NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
   at Npgsql.TypeHandling.NpgsqlTypeHandler`1.ReadAsObject(NpgsqlReadBuffer buf, Int32 len, Boolean async, FieldDescription fieldDescription)
   at Npgsql.NpgsqlDataReader.GetValue(Int32 ordinal)
   at Deserialize8e9e87b0-6954-4120-85bc-5be376fa8874(IDataReader )
   --- End of inner exception stack trace ---
   at Dapper.SqlMapper.ThrowDataException(Exception ex, Int32 index, IDataReader reader, Object value) in /_/Dapper/SqlMapper.cs:line 3665
   at Deserialize8e9e87b0-6954-4120-85bc-5be376fa8874(IDataReader )
   at Dapper.SqlMapper.<>c__DisplayClass161_0`1.<GenerateMapper>b__0(IDataReader r)
   at Dapper.SqlMapper.MultiMapImpl[TReturn](IDbConnection cnn, CommandDefinition command, Type[] types, Func`2 map, String splitOn, IDataReader reader, Identity identity, Boolean finalize)+MoveNext()
   at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at Dapper.SqlMapper.MultiMapAsync[TReturn](IDbConnection cnn, CommandDefinition command, Type[] types, Func`2 map, String splitOn)
   at SLCRoad.Data.BaseAccess`2.QueryAsync[T](Query query, SqlQueryOptionsGeneric`1 sqlQueryOptions, IDbTransaction dbTransaction)

Further technical details

Asp.Net Core 5.0.1 Npgsql version: 5.0.1 PostgreSQL version: 12.5 Operating system: Ubuntu 20.04 (also happens on Debian 10)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
timbzecommented, Dec 13, 2020

@vonzshik Bugfix is confirmed. Thanks!

0reactions
ngocketitcommented, Dec 22, 2021

Got same issue with 5.0.1. Tried 5.0.7 and confirmed that it’s fixed. Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Npgsql Exception while reading from stream, Postgres
All I am doing is SELECTs. Here is the stack trace: Exception while reading from stream at Npgsql.ReadBuffer.Ensure(Int32 count, Boolean ...
Read more >
Exception while reading from stream · Issue #4532
Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.TimeoutException: Timeout during reading attempt
Read more >
Solved: Postgresql: Exception while reading from stream
This error may happen when connection string is lacking port number. Try using server:port in connection string to Postgres database. Message 7 of...
Read more >
PostgreSQL connections fail with block message
Error on IMSI WCF Pipeline: Npgsql.NpgsqlException (0x80004005): Exception while reading from stream ---> System.IO.IOException: Unable to read data from ...
Read more >
PostgreSQL: Exception while reading from stream
Hello! Please help me solve this error. I am try to connect PostgreSQL.Database on Greenplum. When I try to connect just a table, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found