BerlinVagrant / vagrant-dns

A plugin to manage DNS records for vagrant environments
MIT License
490 stars 50 forks source link

vagrant-dns hangs on restart #27

Closed yn closed 10 years ago

yn commented 10 years ago

I configured vagrant-dns according to instructions, but when bringing my machine up, the boot process hangs (the machine comes up successfully however, and DNS queries to it resolve).

This hang started happening only when I installed vagrant-dns, so I am thinking it is the culprit. Here's the behavior I've seen:

If I have a vagrant-dns server already running, the "vagrant up" process tries to kill it (I see messages from vagrant-dns to that effect). Then, I see in my process list that a new vagrant-dns is up. Then, everything hangs. If I press Ctrl-C, this is what I get:

^C/Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/ui.rb:184:in `synchronize': can't be called from trap context (ThreadError)
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/ui.rb:184:in `say'
from (eval):3:in `warn'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/ui.rb:215:in `block (2 levels) in <class:BasicScope>'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/action/runner.rb:62:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/util/busy.rb:49:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/util/busy.rb:49:in `block in fire_callbacks'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/util/busy.rb:49:in `each'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/util/busy.rb:49:in `fire_callbacks'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/util/busy.rb:33:in `block (2 levels) in register'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/batch_action.rb:76:in `call'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/batch_action.rb:76:in `join'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/batch_action.rb:76:in `block in run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/batch_action.rb:53:in `each'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/batch_action.rb:53:in `run'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:213:in `block (2 levels) in batch'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:208:in `tap'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:208:in `block in batch'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:207:in `synchronize'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:207:in `batch'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/plugins/commands/up/command.rb:55:in `execute'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/cli.rb:38:in `execute'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/lib/vagrant/environment.rb:484:in `cli'
from /Applications/Vagrant/embedded/gems/gems/vagrant-1.4.1/bin/vagrant:127:in `<top (required)>'
from /Applications/Vagrant/bin/../embedded/gems/bin/vagrant:23:in `load'
from /Applications/Vagrant/bin/../embedded/gems/bin/vagrant:23:in `<main>'
njam commented 10 years ago

I have the same behaviour. It seems the Daemons.run_proc-call hangs when starting and stopping the daemon proc. It doesn't matter what code the block contains (i.e. if I replace the RubyDNS::run_server with just loop { sleep 1 } I still see the same behaviour).

Within the daemons gem I tracked it down to call_as_daemon inside lib/daemons/daemonize.rb which looks like this:

  def call_as_daemon(block, logfile_name = nil, app_name = nil)
    rd, wr = IO.pipe

    if tmppid = safefork
      # parent
      wr.close
      pid = rd.read.to_i  // <-- THIS LINE IS BLOCKING
      rd.close
[...]
    else
      # child
      rd.close
[...]
      wr.write Process.pid
      wr.close
[...]
    end

so basically the child process sends its PID through a pipe to the parent which reads it. While debugging I noticed that the PID is sent into the pipe, but never received in the parent. So that rd.read-call hangs.

I can only reproduce this behaviour within the "vagrant ruby", by running bundle exec vagrant up. If I run the same code in my local ruby environment it doesn't hang.

Can anyone confirm this behaviour? I wanted to start using vagrant-dns just now, anyone using it for longer and can confirm this bug? I noticed that the Gemfile.lock is gitignored. Since the gemspec uses git ls-files to populate gem.files I assume the gem doesn't include a lock-file. Therefore probably the newest version will be installed for all gems when installing the plugin?

fnordfish commented 10 years ago

Hi,

I'm just experiencing the same issue. As a workaround (so that your vagrant up won't hang) you can disable auto run in your Vagrantfile:

Vagrant.configure("2") do |config|
  #....
  VagrantDNS::Config.auto_run = false
  # ...
end

and start/stop manually vagrant dns --start / vagrant dns --stop. I had to manually kill the running dns process: kill $(cat ~/.vagrant.d/tmp/dns/daemon/vagrant-dns.pid) in order to get star/stop working correctly.

Also, I reinstalled all my vagrant plugins by deleting ~/.vagrant.d/gems and vagrant plugin install all the things. I'm not sure if this made any differences, it was my first attempt to get daemons working correctly.

njam commented 10 years ago

My suspicion first was that some gem-dependencies is responsible for this.

Any other ideas?

Unfortunately I'm not too acquainted with bundler nor vagrant, which might be relevant since the problem only occurs in when running vagrant with bundle exec vagrant up, not when invoked from my local ruby as a normal script.

njam commented 10 years ago

I've isolated the problem a bit. It seems the Process.waitpid(tmppid) in the parent process hangs. I opened an issue with vagrant asking for help: https://github.com/mitchellh/vagrant/issues/2756

njam commented 10 years ago

A fix for this has landed in vagrant: https://github.com/mitchellh/vagrant/issues/2756 I think this can be closed.

njam commented 10 years ago

This is resolved with the now released vagrant 1.5.

fnordfish commented 10 years ago

Right. Vagrant 1.5 fixed this, also the workaround mentioned above does not work in 1.5 any more.