Skip to content

Instantly share code, notes, and snippets.

@purbon
Created August 4, 2016 10:18
Show Gist options
  • Save purbon/106e80a5d85b3a6fbf1e5f10b5d0d643 to your computer and use it in GitHub Desktop.
Save purbon/106e80a5d85b3a6fbf1e5f10b5d0d643 to your computer and use it in GitHub Desktop.
race condition while jruby is resolving host files
#!/usr/bin/env ruby
require "resolv"
require 'jruby'
class Resolv
class Hosts
def initialize(filename = DefaultFileName)
@filename = filename
@mutex = Mutex.new
@initialized = nil
@mutex.lock
end
end
end
class DnsLookup
def initialize
@nameserver = [ "8.8.8.8" ]
@resolv = ::Resolv.new(resolvers=[::Resolv::Hosts.new, ::Resolv::DNS.new(:nameserver => @nameserver, :search => [], :ndots => 1)])
end
def getname(address)
@resolv.getname(address).force_encoding(Encoding::UTF_8)
end
end
if __FILE__ == $0
lookup = DnsLookup.new
thr = Thread.new(lookup) do |_lookup|
raise Exception.new unless _lookup.getname("54.218.47.147") == "ec2-54-218-47-147.us-west-2.compute.amazonaws.com"
end
Thread.pass until thr.status == "sleep"
JRuby.reference(thr).native_thread.interrupt
thr.join
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment