:transaction strategy: uncommitted data disappears after perform_enqueued_jobs on Rails ≥ 7.2
まだ誰も着手していません。
評価
- 難易度
- 3/5
- 見積もり時間
- 1〜2日
- 初心者へのやさしさ
- 67/100
調査の方向性
Start at the :transaction strategy entry point and read the reproduction app's README, then run for i in {1..100}; do bin/rails test; done to observe the intermittent failure. Compare the strategy's connection setup and teardown with Rails' pinned transactional fixtures; done means the reproduction completes without RecordNotFound or lock-wait failures.
索引モデルが issue の本文から書いたものです。
説明
Below is summary of bug I have found in my company's app I work for. It is related to how database cleaner and rails internals handles the connections to database. I used AI to help investigating the problem but I verified (as far as I understand it) and reproduced the problem. Below is the summary of but I prepared a detailed rails app (see its' README) with instruction how to reproduce it https://github.com/radarek/database-cleaner-connection-bug.
Summary
On Rails ≥ 7.2, the :transaction strategy can intermittently cause ActiveRecord::RecordNotFound (and lock-wait timeouts on writes): a row created inside a test becomes invisible after an ActiveJob runs inline (perform_enqueued_jobs).
Reproduction app: https://github.com/radarek/database-cleaner-connection-bug (Rails 8.1, MySQL/trilogy, Minitest). for i in {1..100}; do bin/rails test; done should reproduce the problem at least once
Cause
The :transaction strategy opens its wrapping transaction on a leased connection:
connection = ActiveRecord::Base.lease_connection
connection.begin_transaction(joinable: false)
Running any ActiveJob wraps execution in Rails.application.reloader.wrap { … }. On completion, Rails' AR executor hook (ConnectionPool::ExecutorHooks.complete) sees a connection whose transaction is not joinable and releases it back to the pool — while it still holds the open transaction and all of the test's uncommitted rows.
The next query then leases a connection again. If the pool hands it a different physical connection (e.g. after the background Reaper reclaims a connection and reorders the LIFO free list), that connection is a separate DB session that can't see the uncommitted data → RecordNotFound. This is why it's intermittent and why it only shows up alongside other specs that grow the pool past one connection.
Suggested fix
On Rails ≥ 7.2, pin the connection instead of leasing it:
pool.pin_connection!(...) # setup
pool.unpin_connection! # teardown
A pinned connection is immune to the executor hook (checkout always returns the pinned connection), so the swap can't happen. This is exactly what Rails' own transactional fixtures do — and the pinned variant in the repro app passes 100% of runs.
Full mechanism write-up (LIFO free list, orphaned connections, Reaper timing) is in the repo README.
- 主要言語
- Ruby
- スター
- 3k
- フォーク
- 485
- PR マージ指標
- 30日以内にマージされた PR はありません
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートなし
- コントリビューションガイドなし
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
DatabaseCleaner/database_cleaner のほかの issue
-
Cleaning of dev DBオープン
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
-
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
-
Cannot truncate a table referenced in a foreign key constraint (ActiveRecord::StatementInvalid)オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
DatabaseCleaner/database_cleaner#717 · コメント 5 件 · リアクション 1 件 ·
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
DatabaseCleaner/database_cleaner#716 · コメント 2 件 ·
-
難易度 5/5 1週間以上 初心者へのやさしさ 10/100
DatabaseCleaner/database_cleaner#705 · コメント 1 件 · リアクション 5 件 ·
DatabaseCleaner/database_cleaner の issue をすべて見る
似ている issue
-
security
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 68/100
CircuitVerse/CircuitVerse#7967 · コメント 1 件 · リアクション 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 86/100
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
simp/pupmod-simp-stunnel#173 ·
-
Polish QDrantオープン
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信