Skip to content
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

Ensure child processes are killed #364

Closed
wants to merge 1 commit into from
Closed
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
31 changes: 30 additions & 1 deletion lib/install/bin/webpack-dev-server.tt
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@ $stdout.sync = true
require "shellwords"
require "yaml"

def descendant_processes(base=nil)
return [] if base.nil?
descendants = Hash.new{|ht,k| ht[k]=[k]}
Hash[*`ps -eo pid,ppid`.scan(/\d+/).map{|x|x.to_i}].each{|pid,ppid|
descendants[ppid] << descendants[pid]
}
descendants[base].flatten - [base]
end

ENV["RAILS_ENV"] ||= "development"
RAILS_ENV = ENV["RAILS_ENV"]

Expand All @@ -30,6 +39,26 @@ end
newenv = { "NODE_PATH" => NODE_MODULES_PATH }
cmdline = [WEBPACK_BIN, "--progress", "--color", "--config", DEV_SERVER_CONFIG] + ARGV

# Trap term and int signals to ensure child processes are removed
Signal.trap("TERM") do
exit 1 if $webpacker_dev_server_pid.nil?
# Kill the subprocesses
descendant_processes($webpacker_dev_server_pid).each do |d|
Process.kill(9, d)
end
end

Signal.trap("INT") do
exit 1 if $webpacker_dev_server_pid.nil?
# Kill the subprocesses
descendant_processes($webpacker_dev_server_pid).each do |d|
Process.kill(9, d)
end
end

Dir.chdir(APP_PATH) do
exec newenv, *cmdline
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As it's currently using exec, the immediate "child" process takes over here. That would seem to make any handling of further processes a matter for that one (the real webpack-dev-server). Does that process not manifest the same problem if run directly?

$webpacker_dev_server_pid = spawn newenv, *cmdline
end

wait $webpacker_dev_server_pid
$webpacker_dev_server_pid = nil