Skip to content

Reading a numeric that would overflow leads to hanging #2928

Closed

Description

@elipatov

Steps to reproduce

npgsql-deadlock.zip

It requires any PostgreSQL on localhost with login password from your tests (alternatively, change connection string).

The issue

I read a data in CommandBehavior.SequentialAccess mode. An exception occurs, reading column in the middle (in my case Numeric value does not fit in a System.Decimal). My code handles this exception: reader.Dispose() or ignore row and reader.Read(). It hangs until command timeouts.
Basically any operation that in ReaderState.InResult calls ConsumeRow stucks.

Hanging stack-trace for Dispose

System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode) Line 1780	C#
System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) Line 1741	C#
System.dll!System.Net.Sockets.NetworkStream.Read(byte[] buffer, int offset, int size) Line 508	C#
Npgsql.dll!Npgsql.NpgsqlReadBuffer.Ensure.__EnsureLong|0() Line 150	C#
Npgsql.dll!Npgsql.NpgsqlReadBuffer.Skip(long len, bool async) Line 223	C#
Npgsql.dll!Npgsql.NpgsqlSequentialDataReader.ConsumeRow(bool async) Line 300	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.NextResult(bool async, bool isConsuming) Line 380	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Consume(bool async) Line 634	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Close(bool connectionClosing, bool async) Line 671	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Close() Line 645	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Dispose(bool disposing) Line 640	C#
System.Data.dll!System.Data.Common.DbDataReader.Dispose()	Unknown

Hanging stack-trace for Read

System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags, out System.Net.Sockets.SocketError errorCode) Line 1780	C#
System.dll!System.Net.Sockets.Socket.Receive(byte[] buffer, int offset, int size, System.Net.Sockets.SocketFlags socketFlags) Line 1741	C#
System.dll!System.Net.Sockets.NetworkStream.Read(byte[] buffer, int offset, int size) Line 508	C#
Npgsql.dll!Npgsql.NpgsqlReadBuffer.Ensure.__EnsureLong|0() Line 139	C#
Npgsql.dll!Npgsql.NpgsqlReadBuffer.Skip(long len, bool async) Line 214	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.ConsumeRow.__ConsumeRowSequential|126_0(bool async2) Line 1920	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.ConsumeRow(bool async) Line 1897	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Read(bool async) Line 239	C#
Npgsql.dll!Npgsql.NpgsqlDataReader.Read() Line 161	C#

Further technical details

Npgsql version: 4.1.3.1 and 4.0.2
PostgreSQL version:10.7
Operating system: CentOS 7

Other details about my project setup:

Activity

  1. added this to the 4.1.4 milestone on Apr 24, 2020
  2. roji commented on Apr 24, 2020

    @roji
    Member

    Thanks, sequential access isn't as well-tested as it should be. You seem to have a good of idea of how things are happening, any chance you're interested in submitting a PR?

  3. elipatov commented on Apr 30, 2020

    @elipatov
    ContributorAuthor

    Here it is #2941

  4. changed the title [-]CommandBehavior.SequentialAccess in NpgsqlDataReader causes deadlock in Npgsql 4.1.3.1[/-] [+]Reading a numeric that would overflow leads to hanging[/+] on Apr 30, 2020
  5. added a commit that references this issue on Apr 30, 2020
    1e20ba6
  6. imb590 commented on Sep 18, 2020

    @imb590

    This bug is still present in 4.1.4 if the numeric value has valid scale, but causes overflows in DecimalRaw.Multiply here. For example,

    var conn = OpenConnection();
    var cmd = new NpgsqlCommand("select 10000000000000000.000000000000000001::numeric", conn);
    var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess);
    reader.Read();
    try
    {
        reader.GetValue(0);
    }
    catch (OverflowException)
    {
    }

    This leaves reader and the whole NpgsqlConnection in an invalid state, and reader.Dispose() and all the following attempts to use the connection fail with something like this:

    Npgsql.NpgsqlException: Unknown message code: 0
    
    Npgsql.NpgsqlException
    Unknown message code: 0
       at Npgsql.Util.PGUtil.ValidateBackendMessageCode(BackendMessageCode code)
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>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.Consume(Boolean async)
       at Npgsql.NpgsqlDataReader.Close(Boolean connectionClosing, Boolean async)
       at Npgsql.NpgsqlDataReader.Close()
       at Npgsql.NpgsqlDataReader.Dispose(Boolean disposing)
       at System.Data.Common.DbDataReader.Dispose()
    
  7. 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