Skip to content

Swallow exceptions in NpgsqlDataReader.Dispose() #2372

Description

@roji

It is generally considered to be bad practice to throw from Dispose() methods - such exceptions can be hard for consumers to properly catch, mask previous exceptions which triggered the dispose, etc. The SqlClient behavior is apparently to actually swallow any exceptions that occur during disposal.

The proposal here would be to do the same, and log the exception.

Activity

  1. added this to the 4.1 milestone on Mar 15, 2019
  2. YohDeadfall commented on Mar 18, 2019

    @YohDeadfall
    Contributor

    @roji Could you provide a little bit more details about it? Otherwise, you're violating rules mentioned in the template, and we know nothing you have in the mind (:

  3. roji commented on Mar 18, 2019

    @roji
    MemberAuthor

    Yeah, sorry about that :) I wrote it in the middle of a discussion to make sure I wouldn't forget :) Am going to update the issue with a real description.

  4. modified the milestones: 4.1, 5.0 on Sep 1, 2019
  5. tedypranolo commented on Sep 15, 2019

    @tedypranolo
  6. roji commented on Sep 15, 2019

    @roji
    Author
  7. tedypranolo commented on Sep 15, 2019

    @tedypranolo
  8. self-assigned this
    on Nov 10, 2020
  9. added a commit that references this issue on Nov 13, 2020
    c732dc9
  10. changed the title [-]Consider not throwing from NpgsqlDataReader.Dispose()[/-] [+]Swallow exceptions in NpgsqlDataReader.Dispose()[/+] on Nov 14, 2020
  11. jemiller0 commented on Dec 7, 2020

    @jemiller0

    I think I found another place where exceptions are being thrown in Dispose(). It appears that NpgsqlTransaction.Dispose(Boolean disposing) needs to swallow exceptions as well.

    LibraryConsoleApplication Critical: 0 : Npgsql.NpgsqlOperationInProgressException (0x80004005): A command is already in progress: SELECT ... FROM user_users
       at Npgsql.NpgsqlConnector.<StartUserAction>g__DoStartUserAction|233_0(<>c__DisplayClass233_0& )
       at Npgsql.NpgsqlConnector.StartUserAction(ConnectorState newState, NpgsqlCommand command, CancellationToken cancellationToken, Boolean attemptPgCancellation)
       at Npgsql.NpgsqlConnector.StartUserAction(CancellationToken cancellationToken, Boolean attemptPgCancellation)
       at Npgsql.NpgsqlConnector.Rollback(Boolean async, CancellationToken cancellationToken)
       at Npgsql.NpgsqlTransaction.Rollback()
       at Npgsql.NpgsqlTransaction.Dispose(Boolean disposing)
       at System.Data.Common.DbTransaction.Dispose()
       at LdpLibrary.LdpDapperContext.Dispose() in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Library5\LdpLibrary\LdpDapperContext.cs:line 1060
       at LibraryConsoleApplication.Program2.LoadPeople() in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Library5\LibraryConsoleApplication\Program2.cs:line 1828
       at LibraryConsoleApplication.Program2.Main(String[] args) in C:\Users\jemiller\Documents\Visual Studio 2017\Projects\Library5\LibraryConsoleApplication\Program2.cs:line 87
    
    
  12. roji commented on Dec 9, 2020

    @roji
    MemberAuthor

    @jemiller0 can you please open a new issue for that?

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

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions