Skip to content

Commit 60ad2fa

Browse files
committed
wrap _fork from Ruby 3.1
Ruby 3.1 calls `_fork` from `Kernel#fork` and `Process.fork` (and so on) so we only need to wrap `_fork`. This patch is required to wrap `fork` features as a nearest wrapper for `_fork`. fix #273
1 parent f378be7 commit 60ad2fa

File tree

2 files changed

+83
-44
lines changed

2 files changed

+83
-44
lines changed

lib/debug/session.rb

Lines changed: 75 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2130,9 +2130,49 @@ def self.compare_path(a, b)
21302130
end
21312131

21322132
module ForkInterceptor
2133-
def fork(&given_block)
2134-
return super unless defined?(SESSION) && SESSION.active?
2133+
if Process.respond_to? :_fork
2134+
def _fork
2135+
return yield unless defined?(SESSION) && SESSION.active?
21352136

2137+
parent_hook, child_hook = __fork_setup_for_debugger
2138+
2139+
super.tap do |pid|
2140+
if pid != 0
2141+
# after fork: parent
2142+
parent_hook.call pid
2143+
else
2144+
# after fork: child
2145+
child_hook.call
2146+
end
2147+
end
2148+
end
2149+
else
2150+
def fork(&given_block)
2151+
return yield unless defined?(SESSION) && SESSION.active?
2152+
parent_hook, child_hook = __fork_setup_for_debugger
2153+
2154+
if given_block
2155+
new_block = proc {
2156+
# after fork: child
2157+
child_hook.call
2158+
given_block.call
2159+
}
2160+
super(&new_block).tap{|pid| parent_hook.call(pid)}
2161+
else
2162+
super.tap do |pid|
2163+
if pid
2164+
# after fork: parent
2165+
parent_hook.call pid
2166+
else
2167+
# after fork: child
2168+
child_hook.call
2169+
end
2170+
end
2171+
end
2172+
end
2173+
end
2174+
2175+
private def __fork_setup_for_debugger
21362176
unless fork_mode = CONFIG[:fork_mode]
21372177
if CONFIG[:parent_on_fork]
21382178
fork_mode = :parent
@@ -2179,26 +2219,7 @@ def fork(&given_block)
21792219
}
21802220
end
21812221

2182-
if given_block
2183-
new_block = proc {
2184-
# after fork: child
2185-
child_hook.call
2186-
given_block.call
2187-
}
2188-
pid = super(&new_block)
2189-
parent_hook.call(pid)
2190-
pid
2191-
else
2192-
if pid = super
2193-
# after fork: parent
2194-
parent_hook.call pid
2195-
else
2196-
# after fork: child
2197-
child_hook.call
2198-
end
2199-
2200-
pid
2201-
end
2222+
return parent_hook, child_hook
22022223
end
22032224
end
22042225

@@ -2215,28 +2236,46 @@ def trap sig, *command, &command_proc
22152236
end
22162237
end
22172238

2218-
if RUBY_VERSION >= '3.0.0'
2239+
if Process.respond_to? :_fork
2240+
module ::Process
2241+
class << self
2242+
prepend ForkInterceptor
2243+
end
2244+
end
2245+
2246+
# trap
22192247
module ::Kernel
2220-
prepend ForkInterceptor
22212248
prepend TrapInterceptor
22222249
end
2250+
module ::Signal
2251+
class << self
2252+
prepend TrapInterceptor
2253+
end
2254+
end
22232255
else
2224-
class ::Object
2225-
include ForkInterceptor
2226-
include TrapInterceptor
2256+
if RUBY_VERSION >= '3.0.0'
2257+
module ::Kernel
2258+
prepend ForkInterceptor
2259+
prepend TrapInterceptor
2260+
end
2261+
else
2262+
class ::Object
2263+
include ForkInterceptor
2264+
include TrapInterceptor
2265+
end
22272266
end
2228-
end
22292267

2230-
module ::Kernel
2231-
class << self
2232-
prepend ForkInterceptor
2233-
prepend TrapInterceptor
2268+
module ::Kernel
2269+
class << self
2270+
prepend ForkInterceptor
2271+
prepend TrapInterceptor
2272+
end
22342273
end
2235-
end
22362274

2237-
module ::Process
2238-
class << self
2239-
prepend ForkInterceptor
2275+
module ::Process
2276+
class << self
2277+
prepend ForkInterceptor
2278+
end
22402279
end
22412280
end
22422281

test/protocol/hover_raw_dap_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -514,7 +514,7 @@ def test_hover_works_correctly
514514
value: /JSON::Ext::Generator::GeneratorMethods::Integer/,
515515
type: "Array",
516516
variablesReference: 19,
517-
indexedVariables: 10,
517+
indexedVariables: /(9|10)/,
518518
namedVariables: /\d+/
519519
}
520520
]
@@ -550,7 +550,7 @@ def test_hover_works_correctly
550550
value: /Module/,
551551
type: "Array",
552552
variablesReference: 21,
553-
indexedVariables: 8,
553+
indexedVariables: /(7|8)/,
554554
namedVariables: /\d+/
555555
}
556556
]
@@ -1347,7 +1347,7 @@ def test_1641198331
13471347
value: /Object/,
13481348
type: "Array",
13491349
variablesReference: 12,
1350-
indexedVariables: 7,
1350+
indexedVariables: /(6|7)/,
13511351
namedVariables: /\d+/
13521352
}
13531353
]
@@ -1469,7 +1469,7 @@ def test_1641198331
14691469
value: /Object/,
14701470
type: "Array",
14711471
variablesReference: 17,
1472-
indexedVariables: 7,
1472+
indexedVariables: /(6|7)/,
14731473
namedVariables: /\d+/
14741474
}
14751475
]
@@ -1530,7 +1530,7 @@ def test_1641198331
15301530
value: /Object/,
15311531
type: "Array",
15321532
variablesReference: 20,
1533-
indexedVariables: 7,
1533+
indexedVariables: /(6|7)/,
15341534
namedVariables: /\d+/
15351535
}
15361536
]
@@ -1591,7 +1591,7 @@ def test_1641198331
15911591
value: /Object/,
15921592
type: "Array",
15931593
variablesReference: 23,
1594-
indexedVariables: 7,
1594+
indexedVariables: /(6|7)/,
15951595
namedVariables: /\d+/
15961596
}
15971597
]
@@ -1713,7 +1713,7 @@ def test_1641198331
17131713
value: /Object/,
17141714
type: "Array",
17151715
variablesReference: 28,
1716-
indexedVariables: 7,
1716+
indexedVariables: /(6|7)/,
17171717
namedVariables: /\d+/
17181718
}
17191719
]
@@ -1774,7 +1774,7 @@ def test_1641198331
17741774
value: /Object/,
17751775
type: "Array",
17761776
variablesReference: 31,
1777-
indexedVariables: 7,
1777+
indexedVariables: /(6|7)/,
17781778
namedVariables: /\d+/
17791779
}
17801780
]

0 commit comments

Comments
 (0)