<?xml version="1.0" encoding="UTF-8"?>
<commit>
  <added type="array"/>
  <modified type="array">
    <modified>
      <diff>@@ -501,32 +501,58 @@ private
 		File.chmod(0666, tempfile_path)
 		tempfile.close
 		
-		if self.class.fork_supported?
-			pid = safe_fork do
-				ObjectSpace.each_object(IO) do |obj|
-					obj.close rescue nil
+		if self.class.fork_supported? || Process.respond_to?(:spawn)
+			if Process.respond_to?(:spawn)
+				pid = Process.spawn(command,
+					:in  =&gt; &quot;/dev/null&quot;,
+					:out =&gt; tempfile_path,
+					:err =&gt; tempfile_path,
+					:close_others =&gt; true
+				)
+			else
+				pid = safe_fork do
+					ObjectSpace.each_object(IO) do |obj|
+						obj.close rescue nil
+					end
+					STDIN.reopen(&quot;/dev/null&quot;, &quot;r&quot;)
+					STDOUT.reopen(tempfile_path, &quot;w&quot;)
+					STDERR.reopen(tempfile_path, &quot;w&quot;)
+					exec(command)
 				end
-				STDIN.reopen(&quot;/dev/null&quot;, &quot;r&quot;)
-				STDOUT.reopen(tempfile_path, &quot;w&quot;)
-				STDERR.reopen(tempfile_path, &quot;w&quot;)
-				exec(command)
 			end
 			
 			# run_command might be running in a timeout block (like
 			# in #start_without_locking).
 			begin
-				Process.waitpid(pid) rescue nil
+				interruptable_waitpid(pid)
+			rescue Errno::ECHILD
+				# Maybe a background thread or whatever waitpid()'ed
+				# this child process before we had the chance. There's
+				# no way to obtain the exit status now. Assume that
+				# it started successfully; if it didn't we'll know
+				# that later by checking the PID file and by pinging
+				# it.
+				return
 			rescue Timeout::Error
 				# If the daemon doesn't fork into the background
 				# in time, then kill it.
-				Process.kill('SIGTERM', pid) rescue nil
+				begin
+					Process.kill('SIGTERM', pid)
+				rescue SystemCallError
+				end
 				begin
 					Timeout.timeout(5, Timeout::Error) do
-						Process.waitpid(pid) rescue nil
+						begin
+							interruptable_waitpid(pid)
+						rescue SystemCallError
+						end
 					end
 				rescue Timeout::Error
-					Process.kill('SIGKILL', pid)
-					Process.waitpid(pid) rescue nil
+					begin
+						Process.kill('SIGKILL', pid)
+						interruptable_waitpid(pid)
+					rescue SystemCallError
+					end
 				end
 				raise
 			end
@@ -578,4 +604,23 @@ private
 			return pid
 		end
 	end
+	
+	if RUBY_VERSION &lt; &quot;1.9&quot;
+		def interruptable_waitpid(pid)
+			Process.waitpid(pid)
+		end
+	else
+		# On Ruby 1.9, Thread#kill (which is called by timeout.rb) may
+		# not be able to interrupt Process.waitpid. So here we use a
+		# special version that's a bit less efficient but is at least
+		# interruptable.
+		def interruptable_waitpid(pid)
+			result = nil
+			while !result
+				result = Process.waitpid(pid, Process::WNOHANG)
+				sleep 0.01 if !result
+			end
+			return result
+		end
+	end
 end</diff>
      <filename>lib/daemon_controller.rb</filename>
    </modified>
    <modified>
      <diff>@@ -1,4 +1,4 @@
-require File.join(File.dirname(__FILE__), &quot;test_helper&quot;)
+require File.expand_path(File.join(File.dirname(__FILE__), &quot;test_helper&quot;))
 require 'daemon_controller'
 require 'benchmark'
 require 'socket'
@@ -181,8 +181,8 @@ describe DaemonController, &quot;#stop&quot; do
 		result = Benchmark.measure do
 			@controller.stop
 		end
-		@controller.running?.should be_false
-		(0.3 .. 0.5).should === result.real
+		@controller.should_not be_running
+		(0.3 .. 0.6).should === result.real
 	end
 	
 	it &quot;raises StopTimeout if the daemon does not stop in time&quot; do</diff>
      <filename>spec/daemon_controller_spec.rb</filename>
    </modified>
    <modified>
      <diff>@@ -51,10 +51,10 @@ end
 # 'go!' method has been called.
 class WaitingThread &lt; Thread
 	def initialize
+		@mutex = Mutex.new
+		@cond = ConditionVariable.new
+		@go = false
 		super do
-			@mutex = Mutex.new
-			@cond = ConditionVariable.new
-			@go = false
 			@mutex.synchronize do
 				while !@go
 					@cond.wait(@mutex)</diff>
      <filename>spec/test_helper.rb</filename>
    </modified>
  </modified>
  <removed type="array"/>
  <parents type="array">
    <parent>
      <id>c4e1f22fff0e726dcd1b14778b6c919d43b908b5</id>
    </parent>
  </parents>
  <author>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </author>
  <url>http://github.com/FooBarWidget/daemon_controller/commit/de366a08623bb46f2de25a2ae768712ede2a8b21</url>
  <id>de366a08623bb46f2de25a2ae768712ede2a8b21</id>
  <committed-date>2009-11-06T05:36:28-08:00</committed-date>
  <authored-date>2009-11-06T05:36:28-08:00</authored-date>
  <message>Ruby 1.9 compatibility fixes.</message>
  <tree>cac61d00ebe79336045e0cdfbfc92dcc9e4d362a</tree>
  <committer>
    <name>Hongli Lai (Phusion)</name>
    <email>hongli@phusion.nl</email>
  </committer>
</commit>
