-
Notifications
You must be signed in to change notification settings - Fork 21.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31174 from BrentWheeldon/bmw-db-load-deadlock-5-1
Backport prevent deadlocks with load interlock and DB lock to 5-1-stable
- Loading branch information
Showing
3 changed files
with
74 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
activesupport/lib/active_support/concurrency/load_interlock_aware_monitor.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
# frozen_string_literal: true | ||
|
||
require "monitor" | ||
|
||
module ActiveSupport | ||
module Concurrency | ||
# A monitor that will permit dependency loading while blocked waiting for | ||
# the lock. | ||
class LoadInterlockAwareMonitor < Monitor | ||
# Enters an exclusive section, but allows dependency loading while blocked | ||
def mon_enter | ||
mon_try_enter || | ||
ActiveSupport::Dependencies.interlock.permit_concurrent_loads { super } | ||
end | ||
end | ||
end | ||
end |
55 changes: 55 additions & 0 deletions
55
activesupport/test/concurrency/load_interlock_aware_monitor_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
# frozen_string_literal: true | ||
|
||
require "abstract_unit" | ||
require "concurrent/atomic/count_down_latch" | ||
require "active_support/concurrency/load_interlock_aware_monitor" | ||
|
||
module ActiveSupport | ||
module Concurrency | ||
class LoadInterlockAwareMonitorTest < ActiveSupport::TestCase | ||
def setup | ||
@monitor = ActiveSupport::Concurrency::LoadInterlockAwareMonitor.new | ||
end | ||
|
||
def test_entering_with_no_blocking | ||
assert @monitor.mon_enter | ||
end | ||
|
||
def test_entering_with_blocking | ||
load_interlock_latch = Concurrent::CountDownLatch.new | ||
monitor_latch = Concurrent::CountDownLatch.new | ||
|
||
able_to_use_monitor = false | ||
able_to_load = false | ||
|
||
thread_with_load_interlock = Thread.new do | ||
ActiveSupport::Dependencies.interlock.running do | ||
load_interlock_latch.count_down | ||
monitor_latch.wait | ||
|
||
@monitor.synchronize do | ||
able_to_use_monitor = true | ||
end | ||
end | ||
end | ||
|
||
thread_with_monitor_lock = Thread.new do | ||
@monitor.synchronize do | ||
monitor_latch.count_down | ||
load_interlock_latch.wait | ||
|
||
ActiveSupport::Dependencies.interlock.loading do | ||
able_to_load = true | ||
end | ||
end | ||
end | ||
|
||
thread_with_load_interlock.join | ||
thread_with_monitor_lock.join | ||
|
||
assert able_to_use_monitor | ||
assert able_to_load | ||
end | ||
end | ||
end | ||
end |