Skip to content

Disable sigdump after stop #4043

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions lib/fluent/supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,10 @@ def supervisor_get_dump_config_handler
{ conf: @fluentd_conf }
end

def dump
super unless @stop
end

private

def reopen_log
Expand Down Expand Up @@ -413,6 +417,10 @@ def spawn(process_manager)
def after_start
(config[:worker_pid] ||= {})[@worker_id] = @pm.pid
end

def dump
super unless @stop
end
end

class Supervisor
Expand Down Expand Up @@ -754,12 +762,6 @@ def options
end

def run_worker
begin
require 'sigdump/setup'
rescue Exception
# ignore LoadError and others (related with signals): it may raise these errors in Windows
end

Process.setproctitle("worker:#{@system_config.process_name}") if @process_name

if @standalone_worker && @system_config.workers != 1
Expand Down Expand Up @@ -940,6 +942,10 @@ def install_main_process_signal_handlers
trap :USR2 do
reload_config
end

trap :CONT do
dump_non_windows
end
end
end

Expand Down Expand Up @@ -969,7 +975,7 @@ def install_main_process_command_handlers
reload_config
when "DUMP"
$log.debug "fluentd main process get #{cmd} command"
dump
dump_windows
else
$log.warn "fluentd main process get unknown command [#{cmd}]"
end
Expand Down Expand Up @@ -1020,7 +1026,15 @@ def reload_config
end
end

def dump
def dump_non_windows
begin
Sigdump.dump unless @finished
rescue => e
$log.error("failed to dump: #{e}")
end
end

def dump_windows
Thread.new do
begin
FluentSigdump.dump_windows
Expand Down
74 changes: 72 additions & 2 deletions test/test_supervisor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ def setup
@tmp_dir = tmp_dir
@tmp_root_dir = File.join(@tmp_dir, 'root')
FileUtils.mkdir_p(@tmp_dir)
@sigdump_path = "/tmp/sigdump-#{Process.pid}.log"
end

def teardown
Expand Down Expand Up @@ -191,7 +192,7 @@ def test_system_config
end
end

def test_main_process_signal_handlers
def test_usr1_in_main_process_signal_handlers
omit "Windows cannot handle signals" if Fluent.windows?

create_info_dummy_logger
Expand All @@ -213,6 +214,46 @@ def test_main_process_signal_handlers
$log.out.reset if $log && $log.out && $log.out.respond_to?(:reset)
end

def test_cont_in_main_process_signal_handlers
omit "Windows cannot handle signals" if Fluent.windows?

opts = Fluent::Supervisor.default_options
sv = Fluent::Supervisor.new(opts)
sv.send(:install_main_process_signal_handlers)

Process.kill :CONT, Process.pid

sleep 1

assert{ File.exist?(@sigdump_path) }
ensure
File.delete(@sigdump_path) if File.exist?(@sigdump_path)
end

def test_term_cont_in_main_process_signal_handlers
omit "Windows cannot handle signals" if Fluent.windows?

create_debug_dummy_logger

opts = Fluent::Supervisor.default_options
sv = Fluent::Supervisor.new(opts)
sv.send(:install_main_process_signal_handlers)

Process.kill :TERM, Process.pid
Process.kill :CONT, Process.pid

sleep 1

debug_msg = "[debug]: fluentd main process get SIGTERM\n"
logs = $log.out.logs
assert{ logs.any?{|log| log.include?(debug_msg) } }

assert{ not File.exist?(@sigdump_path) }
ensure
$log.out.reset if $log&.out&.respond_to?(:reset)
File.delete(@sigdump_path) if File.exist?(@sigdump_path)
end

def test_main_process_command_handlers
omit "Only for Windows, alternative to UNIX signals" unless Fluent.windows?

Expand All @@ -239,7 +280,7 @@ def test_main_process_command_handlers
$log.out.reset if $log && $log.out && $log.out.respond_to?(:reset)
end

def test_supervisor_signal_handler
def test_usr1_in_supervisor_signal_handler
omit "Windows cannot handle signals" if Fluent.windows?

create_debug_dummy_logger
Expand All @@ -260,6 +301,35 @@ def test_supervisor_signal_handler
$log.out.reset if $log && $log.out && $log.out.respond_to?(:reset)
end

def test_cont_in_supervisor_signal_handler
omit "Windows cannot handle signals" if Fluent.windows?

server = DummyServer.new
server.install_supervisor_signal_handlers

Process.kill :CONT, Process.pid

sleep 1

assert{ File.exist?(@sigdump_path) }
ensure
File.delete(@sigdump_path) if File.exist?(@sigdump_path)
end

def test_term_cont_in_supervisor_signal_handler
omit "Windows cannot handle signals" if Fluent.windows?

server = DummyServer.new
server.install_supervisor_signal_handlers

Process.kill :TERM, Process.pid
Process.kill :CONT, Process.pid

assert{ not File.exist?(@sigdump_path) }
ensure
File.delete(@sigdump_path) if File.exist?(@sigdump_path)
end

def test_windows_shutdown_event
omit "Only for Windows platform" unless Fluent.windows?

Expand Down