about summary refs log tree commit homepage
path: root/lib/yahns/server.rb
diff options
context:
space:
mode:
authorEric Wong <e@80x24.org>2013-10-21 03:43:43 +0000
committerEric Wong <e@80x24.org>2013-10-21 04:09:24 +0000
commit3a9330dfcc187e4e7f32ede08d1fed416a568d71 (patch)
tree2d52cc363143e2ebaccd9f21e6bec78416972bdb /lib/yahns/server.rb
parentd3335cbaee6c47880b3bb5a372b966842edfeade (diff)
downloadyahns-3a9330dfcc187e4e7f32ede08d1fed416a568d71.tar.gz
There is no way we can rely on any potential vfork optimization
given our use of redirects (vfork only pauses calling thread).

This partially reverts commit d973de0d6dfdf799e111d9f9a71170b61a0ac100
Diffstat (limited to 'lib/yahns/server.rb')
-rw-r--r--lib/yahns/server.rb27
1 files changed, 18 insertions, 9 deletions
diff --git a/lib/yahns/server.rb b/lib/yahns/server.rb
index 9bb8067..67dfea2 100644
--- a/lib/yahns/server.rb
+++ b/lib/yahns/server.rb
@@ -182,15 +182,24 @@ class Yahns::Server # :nodoc:
       end
     end
 
-    opts = {}
-    @listeners.each { |sock| opts[sock.fileno] = sock }
-    env = { "YAHNS_FD" => opts.keys.map(&:to_s).join(',') }
-    opts[:chdir] = @config.value(:working_directory) || Yahns::START[:cwd]
-    cmd = [ Yahns::START[0] ].concat(Yahns::START[:argv])
-    @logger.info "spawning #{cmd.inspect} (in #{opts[:chdir]})"
-    cmd << opts
-    @reexec_pid = Process.spawn(env, *cmd)
-    proc_name 'master (old)'
+    # We cannot use Process.spawn here because of redirects + close-on-exec
+    # We must keep close_on_exec=true in the parent process and only set
+    # close_on_exec=false in the child.  There must be no opportunity
+    # for the user app to ever get a listen socket with close_on_exec=false
+    @reexec_pid = fork do
+      redirects = {}
+      @listeners.each do |sock|
+        sock.close_on_exec = false
+        redirects[sock.fileno] = sock
+      end
+      ENV['YAHNS_FD'] = redirects.keys.map(&:to_s).join(',')
+      redirects[:close_others] = true
+      Dir.chdir(@config.value(:working_directory) || Yahns::START[:cwd])
+      cmd = [ Yahns::START[0] ].concat(Yahns::START[:argv])
+      @logger.info "executing #{cmd.inspect} (in #{Dir.pwd})"
+      cmd << redirects
+      exec(*cmd)
+    end
   end
 
   # unlinks a PID file at given +path+ if it contains the current PID