Project

General

Profile

Patch #44116 » 0002-Fix-flaky-IssueNestedSetConcurrencyTest.patch

Katsuya HIDAKA, 2026-05-23 04:39

View differences:

test/unit/issue_nested_set_concurrency_test.rb
23 23
  self.use_transactional_tests = false
24 24

  
25 25
  def setup
26
    @connection_pool_mutex = Mutex.new
26 27
    skip if sqlite?
27 28
    if mysql?
28 29
      connection = ActiveRecord::Base.connection_db_config.configuration_hash.deep_dup
......
87 88
      threads = []
88 89
      ids_to_remove.each_slice(10) do |ids|
89 90
        threads << Thread.new do
90
          ActiveRecord::Base.connection_pool.with_connection do
91
            begin
91
          begin
92
            with_thread_connection do
92 93
              ids.each do |id|
93 94
                Issue.find(id).update(parent_id: nil)
94 95
              end
95
            rescue => e
96
              Thread.current[:exception] = e.message
97 96
            end
97
          rescue => e
98
            Thread.current[:exception] = e.message
98 99
          end
99 100
        end
100 101
      end
......
118 119

  
119 120
  private
120 121

  
121
  def threaded(count, &)
122
  def threaded(count, &block)
122 123
    with_settings :notified_events => [] do
123 124
      threads = []
124 125
      count.times do |i|
125 126
        threads << Thread.new(i) do
126
          ActiveRecord::Base.connection_pool.with_connection do
127
            begin
128
              yield
129
            rescue => e
130
              Thread.current[:exception] = e.message
131
            end
127
          begin
128
            with_thread_connection(&block)
129
          rescue => e
130
            Thread.current[:exception] = e.message
132 131
          end
133 132
        end
134 133
      end
......
138 137
      end
139 138
    end
140 139
  end
140

  
141
  def with_thread_connection(&)
142
    # Active Record updates its connection lease registry when a thread obtains a
143
    # connection for the first time, which can race when test threads start
144
    # together.
145
    @connection_pool_mutex.synchronize do
146
      # Initialize this thread's connection lease before concurrent updates.
147
      ActiveRecord::Base.connection_pool.with_connection { nil }
148
    end
149
    ActiveRecord::Base.connection_pool.with_connection(&)
150
  end
141 151
end
(2-2/3)