diff --git a/lib/fluent/supervisor.rb b/lib/fluent/supervisor.rb index b372f92249..740405a12d 100644 --- a/lib/fluent/supervisor.rb +++ b/lib/fluent/supervisor.rb @@ -190,6 +190,12 @@ def install_supervisor_signal_handlers $log.debug 'fluentd supervisor process got SIGUSR2' supervisor_sigusr2_handler end + + term_handler = trap :TERM do + # After SIGTERM, disable the sigdump file by ignoring SIGCONT. + trap(:CONT, nil) + term_handler.call if term_handler.is_a?(Proc) + end end if Fluent.windows? @@ -922,6 +928,9 @@ def install_main_process_signal_handlers end trap :TERM do + # After SIGTERM, disable the sigdump file by ignoring SIGCONT. + trap(:CONT, nil) + $log.debug "fluentd main process get SIGTERM" unless @finished @finished = true diff --git a/test/test_supervisor.rb b/test/test_supervisor.rb index c8b3f5845b..cd1e4d528c 100644 --- a/test/test_supervisor.rb +++ b/test/test_supervisor.rb @@ -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-#{$$}.log" end def teardown @@ -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 @@ -213,6 +214,52 @@ 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) + + begin + Process.kill :CONT, $$ + rescue + end + + sleep 1 + + assert_true 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) + + begin + Process.kill :TERM, $$ + Process.kill :CONT, $$ + rescue + end + + sleep 1 + + debug_msg = '[debug]: fluentd main process get SIGTERM' + "\n" + logs = $log.out.logs + assert{ logs.any?{|log| log.include?(debug_msg) } } + + assert_false File.exist?(@sigdump_path) + ensure + $log.out.reset if $log && $log.out && $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? @@ -239,7 +286,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 @@ -260,6 +307,39 @@ 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 + begin + Process.kill :CONT, $$ + rescue + end + + sleep 1 + + assert_true 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 + begin + Process.kill :TERM, $$ + Process.kill :CONT, $$ + rescue + end + + assert_false 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?