Skip to content

NpgsqlException 57014: canceling statement due to user request #3052

Closed

Description

@jack0718

Hi, i use version(4.1.4) from GitHUb> Fix4.1.4 branch,
I get this Exception, after execution application that call single select query.
that this occurs only when the I was open connection to excute SQL Command

SQL Command:Select payment.payment_id,payment.customer_id,payment.staff_idFrom public.payment

Exception message:
This Stack trace:
Npgsql.PostgresException
  HResult=0x80004005
  Message=57014: canceling statement due to user request
  Source=Npgsql
  StackTrace: 
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlConnector.cs:line 940
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlConnector.cs:line 968
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Threading.Tasks.ValueTask`1.get_Result()
   at Npgsql.NpgsqlDataReader.<NextResult>d__44.MoveNext() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 500
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at Npgsql.NpgsqlDataReader.<Consume>d__64.MoveNext() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 793
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Npgsql.NpgsqlDataReader.<Close>d__69.MoveNext() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 848
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.GetResult()
   at Npgsql.NpgsqlDataReader.Close() in F:\postgresSQL\npgsql\src\Npgsql\NpgsqlDataReader.cs:line 817

Further technical details

Npgsql version:4.1.4.0
PostgreSQL version:PostgreSQL 11.3 (Debian 11.3-1.pgdg90+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 6.3.0-18+deb9u1) 6.3.0 20170516, 64-bit
Operating system: Windows 10 Enterprise

Activity

  1. roji commented on Jul 5, 2020

    @roji
    Member

    The exception indicates that someone is cancelling your query (e.g. by calling NpgsqlCommand.Cance or triggering a cancellation token), but it's not possible to know who that is only from the exception trace posted above. Any chance you can post a minimal code sample that shows the error? Also, are you using any additional layers on top of Npgsql (e.g. Dapper)?

  2. YohDeadfall commented on Jul 5, 2020

    @YohDeadfall
    Contributor

    It's possible that the command was cancelled even by a different instance of the application if pgbouncer is used with enabled pooling. Why so? To cancel a command you need the second connection because of the nature of PostgreSQL, and the connection Id is required too. The driver remembers the received id, but it's a physical id which doesn't correspond to the current connection from bouncer.

  3. jemiller0 commented on Dec 1, 2020

    @jemiller0

    There is something to watch out for regarding this error. I've seen the error when using Dapper and iterating over a result set using a foreach. The results are being read a row at a time (not read into an array/list and then iterated over). The exception below results in an NpgsqlException (the one you list above) in my global exception handler that is outside the foreach loop. To see the actual exception, I found that I have to put a try/catch block within the foreach loop itself. So, I think the exception can still appear even if the connection wasn't cancelled on the back end. Though I think I am actually running into that problem also (intermittent dropped connections when querying a PostgreSQL instance in Amazon's cloud). I just tested it by intentionally throwing an exception in the body of the foreach to see what kind of exception is throw to the global exception handler I have in my Main() method.

                    foreach (var jo in ldc.Query<string>("SELECT data FROM inventory_instances"))
                    {
                        try
                        {
                            throw new Exception("Test!!!");
                        }
                        catch (Exception e)
                        {
                            traceSource.TraceEvent(TraceEventType.Critical, 0, e.ToString());
                            throw;
                        }
                    }
    
  4. jemiller0 commented on Dec 1, 2020

    @jemiller0

    I think the behavior that I'm seeing may actually be a bug. Here's my stack trace. The problem is that Dispose() is throwing an exception. The rules for Dispose() say that it should never throw an exception if I remember correctly.

    LibraryConsoleApplication Critical: 0 : Npgsql.PostgresException (0x80004005): 57014: canceling statement due to user request
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location ---
       at Npgsql.NpgsqlConnector.<>c__DisplayClass160_0.<<DoReadMessage>g__ReadMessageLong|0>d.MoveNext()
    --- End of stack trace from previous location ---
       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()
       at Dapper.SqlMapper.QueryImpl[T](IDbConnection cnn, CommandDefinition command, Type effectiveType)+<>m__Finally1() in /_/Dapper/SqlMapper.cs:line 1122
       at Dapper.SqlMapper.QueryImpl[T](IDbConnection cnn, CommandDefinition command, Type effectiveType)+System.IDisposable.Dispose()
       at LibraryConsoleApplication.Program2.LoadBibs() in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Library5\LibraryConsoleApplication\Program2.cs:line 268
       at LibraryConsoleApplication.Program2.Main(String[] args) in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Library5\LibraryConsoleApplication\Program2.cs:line 134
      Exception data:
        Severity: ERROR
        SqlState: 57014
        MessageText: canceling statement due to user request
        File: postgres.c
        Line: 3124
        Routine: ProcessInterrupts
        ThreadId=1
        DateTime=2020-12-01T20:15:39.6113755Z
    
  5. roji commented on Dec 2, 2020

    @roji
    Member

    @jemiller0 which version of Npgsql are you using? #2372 should have taken care of this for 5.0.0. Regardless, can you please open a separate issue if there's a problem?

  6. jemiller0 commented on Dec 2, 2020

    @jemiller0

    Thanks @roji. I'm using .NET Framework. It looked like 5.0.0 required .NET 5. So, I'm currently using 4.1.6.

  7. YohDeadfall commented on Dec 2, 2020

    @YohDeadfall
    Contributor

    Don't be cheated by the version number. It's accidentally aligned with .NET 5 because Microsoft skipped version for due to .NET Framework. The provider is available for .NET Standard 2 and above.

  8. jemiller0 commented on Dec 2, 2020

    @jemiller0

    Thanks @YohDeadfall. I can confirm that 5.0.0 does indeed appear to fix the problem. I like the new behavior much better. The old behavior was very confusing.

  9. YohDeadfall commented on Dec 2, 2020

    @YohDeadfall
    Contributor

    Good, closing the issue then.

  10. jemiller0 commented on Dec 2, 2020

    @jemiller0

    @YohDeadfall I don't know if it fixes the problem that @jack0718 had when he created this issue. Mine was actually the one in #2372.

  11. jemiller0 commented on Dec 2, 2020

    @jemiller0

    @roji @YohDeadfall I just wanted to say, I think you guys are doing a great job. I can't remember what the other issues were, but, I ran into a weird issue here or there and remember @roji responding and problems being fixed. It is very refreshing to run into a problem like the one with Dispose() throwing exceptions and having developers fix it straight away. This stands in stark contrast to a lot of other projects that I've had experience with. So, thanks, and keep up the good work.

  12. YohDeadfall commented on Dec 2, 2020

    @YohDeadfall
    Contributor

    Mostly all cancellation stuff was made by @vonzshik, he did a lot just before 5.0 and now is a member of the crew. Another big feature was made by @Brar, it's replication support. So it's not just Shay and me (: Anyway, thank you for the kind words!

  13. deleted a comment from YohDeadfall on Jan 17, 2021
  14. roji commented on Jan 17, 2021

    @roji
    Member

    Closing as no responses were provided for the original issue by @jack0718.

  15. MuniprasadAN commented on May 28, 2024

    @MuniprasadAN

    For me , its timeout.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions