From e9909f655c57504034b47fbdcc64b523e29bb668 Mon Sep 17 00:00:00 2001 From: Thomas Walpole Date: Tue, 16 May 2017 20:53:02 -0700 Subject: [PATCH] Explicitly shutdown webkit_server when shutting down - Issue #944 --- .travis.yml | 2 +- lib/capybara/webkit/server.rb | 43 +++++++++++++++++++++++++++++++---- 2 files changed, 39 insertions(+), 6 deletions(-) diff --git a/.travis.yml b/.travis.yml index 0779505..a28b220 100644 --- a/.travis.yml +++ b/.travis.yml @@ -29,7 +29,7 @@ matrix: env: QMAKE=/usr/lib/x86_64-linux-gnu/qt4/bin/qmake - rvm: 2.3.3 gemfile: gemfiles/master.gemfile - - rvm: jruby-9.1.8.0 + - rvm: jruby-9.1.13.0 gemfile: Gemfile allow_failures: - gemfile: gemfiles/master.gemfile diff --git a/lib/capybara/webkit/server.rb b/lib/capybara/webkit/server.rb index 7821214..35344b5 100644 --- a/lib/capybara/webkit/server.rb +++ b/lib/capybara/webkit/server.rb @@ -26,15 +26,23 @@ module Capybara discover_port discover_pid forward_output_in_background_thread + register_shutdown_hook end private def open_pipe - @pipe_stdin, - @pipe_stdout, - @pipe_stderr, - @wait_thr = Open3.popen3(SERVER_PATH) + if IO.respond_to?(:popen4) + @pid, + @pipe_stdin, + @pipe_stdout, + @pipe_stderr = IO.popen4(SERVER_PATH) + else + @pipe_stdin, + @pipe_stdout, + @pipe_stderr, + @wait_thr = Open3.popen3(SERVER_PATH) + end end def discover_port @@ -58,7 +66,7 @@ module Capybara end def discover_pid - @pid = @wait_thr[:pid] + @pid ||= @wait_thr[:pid] end def forward_output_in_background_thread @@ -67,6 +75,31 @@ module Capybara IO.copy_stream(@pipe_stderr, @output_target) if @output_target end end + + def register_shutdown_hook + @owner_pid = Process.pid + at_exit do + if Process.pid == @owner_pid + kill_process + end + end + end + + def kill_process + if @pid + if RUBY_PLATFORM =~ /mingw32/ + Process.kill(9, @pid) + else + Process.kill("INT", @pid) + end + Process.wait(@pid) + @wait_thr.join if @wait_thr + end + rescue Errno::ESRCH, Errno::ECHILD + # This just means that the webkit_server process has already ended + ensure + @pid = @wait_thr = nil + end end end end