Skip to content

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

Closed

Description

@timbze

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

  1. timbze commented on Dec 12, 2020

    @timbze
    Author

    Possibly related to npgsql/efcore.pg#1599

  2. YohDeadfall commented on Dec 12, 2020

    @YohDeadfall
    Contributor

    Could you provide a minimal repro, so we can run it and see the error?

  3. timbze commented on Dec 12, 2020

    @timbze
    Author
  4. vonzshik commented on Dec 12, 2020

    @vonzshik
    Contributor

    Hello @timbze, thank you for the repro!

    Can confirm, there is a bug in Npgsql. Specifically, here:

    _buf.Read(span.Slice(0, count));
    _read += count;
    return count;

    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.

  5. self-assigned this
    on Dec 12, 2020
  6. added this to the 5.0.2 milestone on Dec 12, 2020
  7. timbze commented on Dec 12, 2020

    @timbze
    Author

    @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.

  8. vonzshik commented on Dec 12, 2020

    @vonzshik
    Contributor

    I was talking about ColumnStream.ReadAsync.

    Dapper async might still be using the non-async version though

    Yep, it does synchronously here, and here. Might be a good idea to raise this issue with Dapper folks.

  9. added 2 commits that reference this issue on Dec 13, 2020
    40a1fd3
    fb12040
  10. roji commented on Dec 13, 2020

    @roji
    Member

    Dapper async might still be using the non-async version though

    Yep, it does synchronously here, and here. Might be a good idea to raise this issue with Dapper folks.

    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?

  11. timbze commented on Dec 13, 2020

    @timbze
    Author

    @vonzshik Bugfix is confirmed. Thanks!

  12. petermcneely commented on Oct 13, 2021

    @petermcneely

    @vonzshik Bugfix is confirmed. Thanks!

    @vonzshik did you happen to file an issue with Dapper? If so, could you please link it? Thanks!

  13. ngocketit commented on Dec 22, 2021

    @ngocketit

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

  14. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions