I've tracked it down:

This example

  require 'rb-inotify'
  notifier = INotify::Notifier.new

  begin
    io = notifier.to_io
    nr = 1
    while true
      nr = nr +1
      if IO.select([notifier.to_io], [], [], 0.0001) # 0.01 yields "bad file descriptor!
         notifier.process
      end
    end

  rescue
    puts "nr is #{nr}"
  end

outputs:

 tmp i%for i in `seq 20`; do ruby test.rb; done                                                                                                                                                      /tmp nixos   
nr is 6768
nr is 6768
nr is 6770
nr is 6768
nr is 6770
nr is 6768
nr is 6768
nr is 6770
nr is 6768
nr is 6768
nr is 6768
nr is 6768
nr is 6768
nr is 6769
nr is 6768
nr is 6769
nr is 6768
nr is 6768
nr is 6770
nr is 6769


So the problem occurs roughly after the same amount of to_io calls.
using lsof -p $THIS_RUBY_PROCESS it shows the file descriptor is gone!

When suspending the prog while there is no error looking at losf -p $PID
output shows that file descriptor 3 is connected to inotify.
So it must be notifier.to_io which is causing trouble. I added a puts to
verify that the file descriptor is always 3.

So my fix is easy:  use notifier.to_io to define a local var and use
that local var within the IO.select loop. Then my problem is gone

Nathan: Can you recommend this example instead?

io = notifier.to_io
while true
  if IO.select([io], [], [], 0.0001)
     notifier.process
  end
end

This seems to work. I'm still interested what's causing this trouble.
Obviously there is a problem with the to_io code which looks like this:


  def to_io
    raise NotImplementedError.new("INotify::Notifier#to_io is not supported under JRuby") if RUBY_PLATFORM =~ /java/
    IO.new(@fd)
  end

So ruby doesn't like callign IO.new(@fd) over and over again.

Is this a bug?

Marc Weber