Using TransactionScope and EF together with raw connection might trigger idle-in-transaction timeout

オープン
#3,602 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
停滞
技術スタック
csharp, postgresql
領域
databases

調査の方向性

Start by locating and running the Ambient_transaction_bug test in the TransactionNpgsqlTest class. Trace how the test combines TransactionScope, DbContext, and a raw NpgsqlConnection, then compare behavior with the commented-out context usage and ping. Done when the test reliably demonstrates the reported timeout and the transaction behavior is corrected without using a prepared transaction.

索引モデルが issue の本文から書いたものです。

説明

I have a piece of code where I use both DbContext and data source to get raw sql connection, and in together with TransactionScope, it triggers idle-in-transaction timeout.
Here's a unit test that I put in TransactionNpgsqlTest class to reproduce it. If we "ping" DbContext every now and then this test passes. If we comment out the first context usage, it also passes, but if we leave it in current state, you should experience the timeout (mine is set to 5 seconds).
My expectation would be that no matter in which connection, the transaction is the same and no timeout should be triggered.
Also, prepared transaction count check is there to ensure that no distributed transaction is being used.
P.S. I am not sure if this is more of Npgsql problem or EF itself, but trying to start here.

  [ConditionalFact]
  public async Task Ambient_transaction_bug()
  {
      await using var context = CreateContext();
      var timeoutInMilliseconds = await context.Database.SqlQueryRaw<int>("SELECT setting::INTEGER AS \"Value\"  FROM pg_settings WHERE name = 'idle_in_transaction_session_timeout'").Order().FirstAsync();
      Assert.True(timeoutInMilliseconds > 0);

      timeoutInMilliseconds /= 2;

      using var transactionScope = new TransactionScope(TransactionScopeOption.Required, TransactionScopeAsyncFlowOption.Enabled);
      // Adding this line causes "terminating connection due to idle-in-transaction timeout" error at the bottom. Feels like transactions are not scoped.
      _ = context.Set<TransactionCustomer>().Any();

      await using (var conn = new NpgsqlConnection(TestStore.ConnectionString))
      {
          conn.Open();
          for (var i = 0; i < 3; i++) // A loop to show that this does not invoke transaction session timeout when doing some "work" in the same connection
          {
              await using var cmd = new NpgsqlCommand("SELECT 1", conn);
              cmd.ExecuteNonQuery();

              // Uncommenting this Assert would NOT cause the idle-in-transaction timeout anymore, because it "pings" EF transaction.
              //Assert.Equal(0, GetNumberOfPreparedTransactions(context));
              await Task.Delay(timeoutInMilliseconds);
          }
      }

      _ = context.Set<TransactionCustomer>().Any();
      Assert.Equal(0, GetNumberOfPreparedTransactions(context));
  }

  int GetNumberOfPreparedTransactions(DbContext context)
      => context.Database.SqlQueryRaw<int>("SELECT COUNT(*) AS \"Value\" FROM pg_prepared_xacts WHERE database = 'postgres'").Order().First();
主要言語
C#
スター
1.8k
フォーク
268
平均マージ
2時間 35分
マージ済み PR(30日)
136

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

npgsql/efcore.pg のほかの issue

npgsql/efcore.pg の issue をすべて見る

似ている issue

C# の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。