Repository navigation
Description
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)
Activity
timbze commentedon Dec 12, 2020
Possibly related to npgsql/efcore.pg#1599
YohDeadfall commentedon Dec 12, 2020
Could you provide a minimal repro, so we can run it and see the error?
timbze commentedon Dec 12, 2020
Here's a repro https://github.com/timbze/NpgsqlExceptionRepro
You will need to install PostGis on the database
vonzshik commentedon Dec 12, 2020
Hello @timbze, thank you for the repro!
Can confirm, there is a bug in Npgsql. Specifically, here:
npgsql/src/Npgsql/NpgsqlReadBuffer.Stream.cs
Lines 146 to 149 in d2c6216
In some cases, we might not read all bytes in one go, but it assumes it does (but async version seems to lack this behavior).
I'll submit a pr shortly.
timbze commentedon Dec 12, 2020
@vonzshik Thanks for taking care of this!
I'm not sure I understand what you mean with async version, but my original issue was while using Dapper async. Dapper async might still be using the non-async version though, I don't know. The stack trace in my OP would show that for you I guess.
vonzshik commentedon Dec 12, 2020
I was talking about
ColumnStream.ReadAsync.Yep, it does synchronously here, and here. Might be a good idea to raise this issue with Dapper folks.
Fixed ColumnStream.Read, when ReadBuffer haven't read requested count
Fixed ColumnStream.Read, when ReadBuffer haven't read requested count
roji commentedon Dec 13, 2020
Yeah, I seem to remember they do this for better performance - something about ReadAsync being inefficient on SqlClient, and only rarely actually performing I/O (at least that's the logic I remember). Dapper generally has a mindset of covering up for underlying issues in SqlClient.
Maybe open an issue with them an ping me on it?
timbze commentedon Dec 13, 2020
@vonzshik Bugfix is confirmed. Thanks!
petermcneely commentedon Oct 13, 2021
@vonzshik did you happen to file an issue with Dapper? If so, could you please link it? Thanks!
ngocketit commentedon Dec 22, 2021
Got same issue with 5.0.1. Tried 5.0.7 and confirmed that it's fixed. Thanks!