Stale PID-Files in development

Somehow in my Development-Environment (Windows with Eclipse) webrick comes up with stale PID-Files.

I stop the Server, start it again and sometimes (not always) i get the message:

A server is already running.

This happend since i updated to rails-Version X.

But now i finally figured out (after googling and googling and finding nothing helpful) how to make it work again.

In application.rb before declaration of your own application insert:


require 'stale_pid_cleaner'
StalePidCleaner.check

StalePidCleaner:


class StalePidCleaner
  def self.check
    fName = "./tmp/pids/server.pid"
    if(File.exists?(fName))
      puts "found pidfile: " + fName.to_s
      pid = File.read(fName).to_i
      if(pid < 1)
        puts "invalid pid? " + pid.to_s
      else
        if(!alive?(pid))
          puts "cleaning stale pidfile"
          File.delete(fName)
        end
      end
    end
  end

  private
  def self.alive?(pid)
    begin
      Process.kill(0, pid)
      true
     rescue Errno::ESRCH, TypeError # Process is dead
       false
     end
   end
end

Hope it helps also someone else