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:

1require 'stale_pid_cleaner'
2StalePidCleaner.check

StalePidCleaner:

01class StalePidCleaner
02  def self.check
03    fName = "./tmp/pids/server.pid"
04    if(File.exists?(fName))
05      puts "found pidfile: " + fName.to_s
06      pid = File.read(fName).to_i
07      if(pid < 1)
08        puts "invalid pid? " + pid.to_s
09      else
10        if(!alive?(pid))
11          puts "cleaning stale pidfile"
12          File.delete(fName)
13        end
14      end
15    end
16  end
17 
18  private
19  def self.alive?(pid)
20    begin
21      Process.kill(0, pid)
22      true
23     rescue Errno::ESRCH, TypeError # Process is dead
24       false
25     end
26   end
27end

Hope it helps also someone else