The following example:
Produces the following exception in JRuby:
/opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:865:in `new': Address already in use - Address in use (Errno::EADDRINUSE)
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:865:in `open'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:865:in `open_server'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:759:in `open_server'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:757:in `each'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:757:in `open_server'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:1339:in `initialize'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:1628:in `new'
from /opt/work/jruby.git/lib/ruby/1.8/drb/drb.rb:1628:in `start_service'
from test-drb.rb:4
from test-drb.rb:3:in `times'
from test-drb.rb:3
After some investigation it seems that the flaw is in the DRb itself, and simple
addition of sleep 1 in lib/ruby/1.8/drb/drb.rb, in method run (line 1428), leads to the same exception even in MRI.
Basically, a new Thread is being started in the run method, it has begin-ensure block, but in case when a thread is started but not yet entered begin-ensure, an attempt to stop the service and to kill the thread leads to situation when ensure block is not executed, and the following satement is not being run: @protocol.close if @protocol
Hence, the port is not being freed.
This leads to some rubyspec failures, since the specs exhibit exactly this behavior, starting and stopping the DRb service quickly.