Skip to content

Commit 0f61a0e

Browse files
authored
Merge pull request rails#47419 from Shopify/fork-tracker-trust-callback
Make ForkTracker.check! a noop on Ruby 3.1+
2 parents 71fdc90 + a445596 commit 0f61a0e

File tree

2 files changed

+27
-7
lines changed

2 files changed

+27
-7
lines changed

activesupport/lib/active_support/fork_tracker.rb

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ module ModernCoreExt
66
def _fork
77
pid = super
88
if pid == 0
9-
ForkTracker.check!
9+
ForkTracker.after_fork_callback
1010
end
1111
pid
1212
end
@@ -37,14 +37,22 @@ module CoreExtPrivate
3737
@callbacks = []
3838

3939
class << self
40-
def check!
40+
def after_fork_callback
4141
new_pid = Process.pid
4242
if @pid != new_pid
4343
@callbacks.each(&:call)
4444
@pid = new_pid
4545
end
4646
end
4747

48+
if Process.respond_to?(:_fork) # Ruby 3.1+
49+
def check!
50+
# We trust the `_fork` callback
51+
end
52+
else
53+
alias_method :check!, :after_fork_callback
54+
end
55+
4856
def hook!
4957
if Process.respond_to?(:_fork) # Ruby 3.1+
5058
::Process.singleton_class.prepend(ModernCoreExt)

activesupport/test/fork_tracker_test.rb

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -160,15 +160,27 @@ def test_check
160160
3.times { ActiveSupport::ForkTracker.check! }
161161
end
162162

163-
Process.stub(:pid, Process.pid + 1) do
163+
if Process.respond_to?(:_fork)
164+
Process.stub(:pid, Process.pid + 1) do
165+
assert_no_difference -> { count } do
166+
3.times { ActiveSupport::ForkTracker.check! }
167+
end
168+
169+
assert_no_difference -> { count } do
170+
3.times { ActiveSupport::ForkTracker.check! }
171+
end
172+
end
173+
else
174+
Process.stub(:pid, Process.pid + 1) do
175+
assert_difference -> { count }, +1 do
176+
3.times { ActiveSupport::ForkTracker.check! }
177+
end
178+
end
179+
164180
assert_difference -> { count }, +1 do
165181
3.times { ActiveSupport::ForkTracker.check! }
166182
end
167183
end
168-
169-
assert_difference -> { count }, +1 do
170-
3.times { ActiveSupport::ForkTracker.check! }
171-
end
172184
ensure
173185
ActiveSupport::ForkTracker.unregister(handler)
174186
end

0 commit comments

Comments
 (0)