File tree Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Expand file tree Collapse file tree 2 files changed +27
-7
lines changed Original file line number Diff line number Diff line change @@ -6,7 +6,7 @@ module ModernCoreExt
6
6
def _fork
7
7
pid = super
8
8
if pid == 0
9
- ForkTracker . check!
9
+ ForkTracker . after_fork_callback
10
10
end
11
11
pid
12
12
end
@@ -37,14 +37,22 @@ module CoreExtPrivate
37
37
@callbacks = [ ]
38
38
39
39
class << self
40
- def check!
40
+ def after_fork_callback
41
41
new_pid = Process . pid
42
42
if @pid != new_pid
43
43
@callbacks . each ( &:call )
44
44
@pid = new_pid
45
45
end
46
46
end
47
47
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
+
48
56
def hook!
49
57
if Process . respond_to? ( :_fork ) # Ruby 3.1+
50
58
::Process . singleton_class . prepend ( ModernCoreExt )
Original file line number Diff line number Diff line change @@ -160,15 +160,27 @@ def test_check
160
160
3 . times { ActiveSupport ::ForkTracker . check! }
161
161
end
162
162
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
+
164
180
assert_difference -> { count } , +1 do
165
181
3 . times { ActiveSupport ::ForkTracker . check! }
166
182
end
167
183
end
168
-
169
- assert_difference -> { count } , +1 do
170
- 3 . times { ActiveSupport ::ForkTracker . check! }
171
- end
172
184
ensure
173
185
ActiveSupport ::ForkTracker . unregister ( handler )
174
186
end
You can’t perform that action at this time.
0 commit comments