Bad file descriptor with Nokogiri 1.5.0 #495

tychobrailleur opened this Issue Jul 18, 2011 · 11 comments


None yet

7 participants


I am getting a Bad file descriptor error when using Nokogiri 1.5.0 with JRuby 1.6.2 and 1.6.3 on Win7. I have managed to reproduce the issue with a very simple script which works with MRI:

require 'rubygems'
require 'optparse'
require 'nokogiri'

poms = File.join("**", "pom.xml")
Dir.glob(poms).each do
  puts p
  f =
  puts 'parsing...' if $DEBUG
  doc = Nokogiri::XML(f)
  puts 'Done.'

The XML file being parsed doesn't seem to matter.
Here is the stacktrace:

C:\scripts>jruby reduced_script.rb C:\test
Errno::EBADF: Bad file descriptor - Bad file descriptor
       getStackTrace at java/lang/
    getBacktraceData at org/jruby/runtime/backtrace/
        getBacktrace at org/jruby/runtime/backtrace/
    prepareBacktrace at org/jruby/
            preRaise at org/jruby/exceptions/
            preRaise at org/jruby/exceptions/
              <init> at org/jruby/exceptions/
   newRaiseException at org/jruby/
  newErrnoEBADFError at org/jruby/
            finalize at org/jruby/util/io/
             cleanup at org/jruby/util/io/
              close2 at org/jruby/
               close at org/jruby/
               close at org/jruby/
                call at org/jruby/RubyFile$i$0$0$close.gen:65535
        cacheAndCall at org/jruby/runtime/callsite/
                call at org/jruby/runtime/callsite/
            __file__ at reduced_script.rb:13
                call at reduced_script$block_0$RUBY$__file__:65535
               yield at org/jruby/runtime/
               yield at org/jruby/runtime/
               yield at org/jruby/runtime/
          eachCommon at org/jruby/
                each at org/jruby/
                call at org/jruby/RubyArray$i$0$0$each.gen:65535
        cacheAndCall at org/jruby/runtime/callsite/
           callBlock at org/jruby/runtime/callsite/
            callIter at org/jruby/runtime/callsite/
              (root) at reduced_script.rb:7
           runScript at org/jruby/
         runNormally at org/jruby/
         runFromMain at org/jruby/
       doRunFromMain at org/jruby/
         internalRun at org/jruby/
                 run at org/jruby/
                 run at org/jruby/
                main at org/jruby/

C:\scripts>java -version
java version "1.6.0_23"
Java(TM) SE Runtime Environment (build 1.6.0_23-b05)
Java HotSpot(TM) 64-Bit Server VM (build 19.0-b09, mixed mode)

C:\scripts>jruby -v
jruby 1.6.3 (ruby-1.8.7-p330) (2011-07-07 965162f) (Java HotSpot(TM) 64-Bit Serv
er VM 1.6.0_23) [Windows 7-amd64-java]

C:\scripts>jruby -S gem list nokogiri

*** LOCAL GEMS ***

nokogiri (1.5.0 java)

Apologies if this has been already raised.


This showstopper also occurs on Ubuntu. This means that even the simple example from the Nokogiri tutorial fails with the Bad descriptor error:

require 'rubygems'
require 'nokogiri'

f ="blossom.xml")
doc = Nokogiri::XML(f)

Getting the same issue. My codebase works fine with MRI nokogiri on my OS X machine. Using jruby one on windows.


@yokolet - this seems like it should be easy to solve. Would you mind taking a look if you have a moment?

yokolet commented Feb 22, 2012

Sorry, I wasn't aware of this issue. I'll have a look.


At the time of raising that issue, I had tracked it down to using a relative path in Using an absolute path was not causing the problem.

yokolet commented Feb 23, 2012

Thanks for additional info.

I fixed this issue in rev. ef406cc .If you have a chance, try master.

Thanks for reporting the bug. And sorry for my really late response.

@yokolet yokolet closed this Feb 23, 2012

Hi Yoko,

I have been able to run my original script using master, so I reckon your fix did the trick indeed. Thanks for that. And still looking forward to your talk! ;-)

yokolet commented Feb 23, 2012

Good to know.

And, yes, I look forward to JRubyConf :)


I'm getting this same error on Nokogiri 1.5.5 using JRuby 1.7.0 under Ubuntu 12.04. The error seems to occur when closing a file that has been parsed by Nokogiri. I can use the Nokogiri document if I don't close the file.


f ="test.xml")
f.close # Error is caused by this line




> java -version
java version "1.7.0_05"
Java(TM) SE Runtime Environment (build 1.7.0_05-b06)
Java HotSpot(TM) Server VM (build 23.1-b03, mixed mode)

> jruby -v
jruby 1.7.0 (1.9.3p203) 2012-10-22 ff1ebbe on Java HotSpot(TM) Server VM 1.7.0_05-b06 [linux-i386]

> jruby -S gem list nokogiri

*** LOCAL GEMS ***

nokogiri (1.5.5 java)

I'm able to reproduce this bug with nokogiri 1.5.5 in jruby on OSX

irb(main):001:0> f ="spec/fixtures/simple_asset.xml")
=> #<File:spec/fixtures/simple_asset.xml>
irb(main):003:0> require 'nokogiri'
=> true
irb(main):004:0> Nokogiri.XML(f)
=> #nokogiri object (removed)
irb(main):005:0> f.close
Errno::EBADF: Bad file descriptor - Bad file descriptor
    from org/jruby/ `close'
    from org/jruby/ `close'
    from (irb):5:in `evaluate'
    from org/jruby/ `eval'
    from org/jruby/ `loop'
    from org/jruby/ `catch'
    from org/jruby/ `catch'
    from /Users/bemathis/.rbenv/versions/jruby-1.7.0/bin/irb:13:in `(root)'
irb(main):006:0> exit
@jvshahid jvshahid reopened this Nov 9, 2012
jvshahid commented Nov 9, 2012

Thanks for reporting this bug. I just confirmed that the issue is on master with JRuby 1.6.7 and JRuby 1.7.0. I'll take a look and get back to you shortly.

@jvshahid jvshahid added a commit that closed this issue Nov 9, 2012
@jvshahid jvshahid fixes #495
Xerces will close the InputSource which closes the InputStream of the RubyIO
that was passed to Nokogiri::XML. This closes the underlying FileDescriptor
that is maintained internally by JRuby while the RubyIO object is still
referencing it, which causes a BadFileDescriptor when RubyIO#close is called.
@jvshahid jvshahid closed this in f75a283 Nov 9, 2012
@flavorjones flavorjones added a commit that referenced this issue Mar 6, 2013
@flavorjones flavorjones Updating Manifest.txt.
Related to f75a283 (#495).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment