Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Merge pull request #505 from TimMoore/pidfile-create-exclusive

Stop overwriting existing pidfiles.
  • Loading branch information...
commit e6b70a7c46adbe1f3057157438c46cd9f966b8a3 2 parents 3aaab6b + 85b50e2
@raggi raggi authored
Showing with 20 additions and 1 deletion.
  1. +4 −1 lib/rack/server.rb
  2. +16 −0 test/spec_server.rb
View
5 lib/rack/server.rb
@@ -329,8 +329,11 @@ def daemonize_app
end
def write_pid
- ::File.open(options[:pid], 'w'){ |f| f.write("#{Process.pid}") }
+ ::File.open(options[:pid], ::File::CREAT | ::File::EXCL | ::File::WRONLY ){ |f| f.write("#{Process.pid}") }
at_exit { ::File.delete(options[:pid]) if ::File.exist?(options[:pid]) }
+ rescue Errno::EEXIST
+ check_pid!
+ retry
end
def check_pid!
View
16 test/spec_server.rb
@@ -110,6 +110,22 @@ def with_stderr
server.send(:pidfile_process_status).should.eql :not_owned
end
+ should "not write pid file when it is created after check" do
+ pidfile = Tempfile.open('pidfile') { |f| break f }.path
+ ::File.delete(pidfile)
+ server = Rack::Server.new(:pid => pidfile)
+ ::File.open(pidfile, 'w') { |f| f.write(1) }
+ with_stderr do |err|
+ should.raise(SystemExit) do
+ server.send(:write_pid)
+ end
+ err.rewind
+ output = err.read
+ output.should.match(/already running/)
+ output.should.include? pidfile
+ end
+ end
+
should "inform the user about existing pidfiles with running processes" do
pidfile = Tempfile.open('pidfile') { |f| f.write(1); break f }.path
server = Rack::Server.new(:pid => pidfile)

0 comments on commit e6b70a7

Please sign in to comment.
Something went wrong with that request. Please try again.