Skip to content

Commit 699a31e

Browse files
shunichiko1
authored andcommitted
Support Process.daemon
1 parent 1b8e120 commit 699a31e

File tree

3 files changed

+52
-6
lines changed

3 files changed

+52
-6
lines changed

lib/debug/session.rb

+16-2
Original file line numberDiff line numberDiff line change
@@ -2421,8 +2421,20 @@ def fork(&given_block)
24212421
end
24222422
end
24232423

2424-
private def __fork_setup_for_debugger
2425-
fork_mode = CONFIG[:fork_mode]
2424+
module DaemonInterceptor
2425+
def daemon
2426+
return super unless defined?(SESSION) && SESSION.active?
2427+
2428+
_, child_hook = __fork_setup_for_debugger(:child)
2429+
2430+
super.tap do
2431+
child_hook.call
2432+
end
2433+
end
2434+
end
2435+
2436+
private def __fork_setup_for_debugger fork_mode = nil
2437+
fork_mode ||= CONFIG[:fork_mode]
24262438

24272439
if fork_mode == :both && CONFIG[:parent_on_fork]
24282440
fork_mode = :parent
@@ -2487,6 +2499,7 @@ def trap sig, *command, &command_proc
24872499
module ::Process
24882500
class << self
24892501
prepend ForkInterceptor
2502+
prepend DaemonInterceptor
24902503
end
24912504
end
24922505

@@ -2522,6 +2535,7 @@ class << self
25222535
module ::Process
25232536
class << self
25242537
prepend ForkInterceptor
2538+
prepend DaemonInterceptor
25252539
end
25262540
end
25272541
end

test/console/daemon_test.rb

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# frozen_string_literal: true
2+
3+
require_relative '../support/console_test_case'
4+
5+
module DEBUGGER__
6+
class DaemonTest < ConsoleTestCase
7+
def program
8+
# Ignore SIGHUP since the test debuggee receives SIGHUP after Process.daemon.
9+
# When manualy debugging a daemon, it doesn't receive SIGHUP.
10+
# I don't know why.
11+
<<~'RUBY'
12+
1| trap(:HUP, 'IGNORE')
13+
2| puts 'Daemon starting'
14+
3| Process.daemon
15+
4| puts 'Daemon started'
16+
RUBY
17+
end
18+
19+
def test_daemon
20+
# The program can't be debugged locally since the parent process exits when Process.daemon is called.
21+
debug_code program, remote: :remote_only do
22+
type 'b 3'
23+
type 'c'
24+
assert_line_num 3
25+
type 'b 4'
26+
type 'c'
27+
assert_line_num 4
28+
type 'c'
29+
end
30+
end
31+
end
32+
end

test/support/console_test_case.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -91,10 +91,10 @@ def debug_code(program, remote: true, &test_steps)
9191
if remote && !NO_REMOTE && MULTITHREADED_TEST
9292
begin
9393
th = [
94-
new_thread { debug_code_on_local },
94+
(new_thread { debug_code_on_local } unless remote == :remote_only),
9595
new_thread { debug_code_on_unix_domain_socket },
9696
new_thread { debug_code_on_tcpip },
97-
]
97+
].compact
9898

9999
th.each do |t|
100100
if fail_msg = t.join.value
@@ -109,11 +109,11 @@ def debug_code(program, remote: true, &test_steps)
109109
th.each {|t| t.join}
110110
end
111111
elsif remote && !NO_REMOTE
112-
debug_code_on_local
112+
debug_code_on_local unless remote == :remote_only
113113
debug_code_on_unix_domain_socket
114114
debug_code_on_tcpip
115115
else
116-
debug_code_on_local
116+
debug_code_on_local unless remote == :remote_only
117117
end
118118
end
119119
end

0 commit comments

Comments
 (0)