Skip to content

11.0: closing a connection inside a TransactionScope while the transaction is in a failed state breaks ROLLBACK TO SAVEPOINT recovery #6766

Open

Description

@denisdrake

Description

Since #6424 (fix for #3735), NpgsqlConnection.Close() inside an enlisted TransactionScope calls
connector.ResetWithinEnlistedTransaction(), which prepends the reset message
(SET SESSION AUTHORIZATION DEFAULT; RESET ALL; CLOSE ALL; UNLISTEN *; SELECT pg_advisory_unlock_all(); DISCARD SEQUENCES; DISCARD TEMP)
to the next command sent on the same physical connection.

If the PostgreSQL transaction is in the failed state (InFailedTransactionBlock) when the connection is closed,
the prepended reset can never succeed: the next command — typically ROLLBACK TO SAVEPOINT, the standard way
to recover — fails with 25P02 because the reset runs first, the connector breaks, and the whole ambient
transaction is lost (TransactionScope.Dispose then throws TransactionAbortedException → Connection is not open).

This pattern (open/close a connection per unit of work inside one TransactionScope, with savepoints for nested
units) worked in 10.0.x and is not mentioned in the 11.0 breaking changes.

Repro

using System;
using System.Transactions;
using Npgsql;

// connection string: "Host=...;Database=...;Username=...;Password=...;Enlist=true" (pooling on, defaults)
await using var dataSource = NpgsqlDataSource.Create(args[0]);
using (var setup = dataSource.OpenConnection())
    Exec(setup, "DROP TABLE IF EXISTS repro_rows; CREATE TABLE repro_rows (id int)");

using (var scope = new TransactionScope())
{
    using (var conn = dataSource.OpenConnection())
    {
        Exec(conn, "INSERT INTO repro_rows VALUES (1)");
        Exec(conn, "SAVEPOINT sp");
    }
    using (var conn = dataSource.OpenConnection()) // same physical connection
    {
        Exec(conn, "INSERT INTO repro_rows VALUES (2)");
        try { Exec(conn, "SELECT 1/0"); } catch (PostgresException) { } // 22012, transaction is now failed
    } // closed while the transaction is in the failed state
    using (var conn = dataSource.OpenConnection())
    {
        Exec(conn, "ROLLBACK TO SAVEPOINT sp"); // 11.0.0: 25P02
        Exec(conn, "INSERT INTO repro_rows VALUES (3)");
    }
    scope.Complete();
}
// expected committed rows: 1, 3

static void Exec(NpgsqlConnection conn, string sql)
{
    using var cmd = new NpgsqlCommand(sql, conn);
    cmd.ExecuteNonQuery();
}

Results (PostgreSQL 18.1):

Npgsql Result
10.0.3 ROLLBACK TO SAVEPOINT succeeds, rows 1, 3 committed
11.0.0 PostgresException 25P02: current transaction is aborted… on ROLLBACK TO SAVEPOINT; nothing committed
11.0.0 + No Reset On Close=true succeeds, rows 1, 3 committed

Suggested fix

  • In ResetWithinEnlistedTransaction() (or at the point the reset would be prepended), do not prepend the reset when
    TransactionStatus == InFailedTransactionBlock; or defer it until the transaction is no longer failed / the connector
    leaves the enlisted pending list. The reset cannot run in a failed transaction block anyway.
  • Consider a separate opt-out for the in-transaction reset. Today the only switch is No Reset On Close, which also
    disables the reset on return to the pool. Besides temp tables, the in-transaction reset now releases session-level advisory locks
    (pg_advisory_unlock_all()) and runs RESET ALL in the middle of a transaction, which changes behaviour for code that keeps session
    state across close/reopen inside one TransactionScope.
  • Mention the behaviour change in the 11.0 breaking changes.

Activity

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