I noticed some weird behavior while using nokogiri to parse a ~300mb xml file: when there's an error in the code, ruby hangs for about 3 minutes before exiting. Here's a bare-bones example that reproduces this for me:
require 'nokogiri'
class Foo
def open_it
@doc = File.open('enwikivoyage-latest-pages-articles.xml') { |f| Nokogiri::XML(f) }
puts "about to exit"
whatever
end
end
Foo.new.open_it
In this example I open the file, prints something, then trigger a NameError (since `whatever` doesn't exist). What happens is that "about to exit" prints after a few seconds, then the program hangs for 3 mintues before finally exiting:
$ time ruby test.rb
about to exit
test.rb:7:in `open_it': undefined local variable or method `foobar' for #<Foo:0x007fcbbca35a68> (NameError)
from test.rb:11:in `<main>'
ruby test.rb 169.80s user 6.98s system 98% cpu 2:58.66 total
So far this only seems to happen when I try to reference an undefined variable. If I replace `whatever` in the example code with `exit`, `raise 'foo'`, or even `raise NameError.new` the program exits within a few seconds:
$ time ruby test.rb
about to exit
ruby test.rb 4.46s user 0.55s system 99% cpu 5.020 total
Any idea what's up with this? Is it a (minor) bug, or am I doing something wrong? I'm using ruby 2.4.0p0 with nokogiri 1.7.0.1, although I see the same issue with 2.3.1p112 and 1.7.0.