rspec-rails: rspec hangs in the middle of testing

Whether I type “rspec” or “rake spec” in the terminal, it just hangs and never finishes the testing suite. I’ve commented out tests, ran test files individually, but it still just stops. Closest I got to having it finish was uninstalling and reinstalling rspec-rails. First time I run it, I get it to finish.

Second time, I get this:

$ rake spec
/Users/edmac/.rvm/rubies/ruby-2.1.3/bin/ruby -I/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/lib:/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-support-3.2.2/lib /Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/exe/rspec --pattern spec/\*\*\{,/\*/\*\*\}/\*_spec.rb
I, [2015-04-14T11:28:18.114799 #4618]  INFO -- : Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.
.........*.........*....*.......*..*..*..*.*......^C
RSpec is shutting down and will print the summary report... Interrupt again to force quit.
rake aborted!
Interrupt:
/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/lib/rspec/core/rake_task.rb:69:in `system'
/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/lib/rspec/core/rake_task.rb:69:in `run_task'
/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/lib/rspec/core/rake_task.rb:89:in `block (2 levels) in define'
/Users/edmac/.rvm/gems/ruby-2.1.3/gems/rspec-core-3.2.3/lib/rspec/core/rake_task.rb:87:in `block in define'
/Users/edmac/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `eval'
/Users/edmac/.rvm/gems/ruby-2.1.3/bin/ruby_executable_hooks:15:in `<main>'
Tasks: TOP => spec
(See full trace by running task with --trace)

I had to shut it down because it was hanging for ten minutes. The next time I run the suite, I get:

$ rspec
I, [2015-04-14T11:35:50.690190 #4777]  INFO -- : Not reporting to Code Climate because ENV['CODECLIMATE_REPO_TOKEN'] is not set.

About this issue

  • Original URL
  • State: closed
  • Created 12 years ago
  • Comments: 15 (5 by maintainers)

Commits related to this issue

Most upvoted comments

@JonRowe’s suggestion is a good one, but here’s a slightly more robust way to get the stack trace, so we can see what it’s stuck at. Add this code to any file that is loaded as part of your suite (such as spec/spec_helper.rb):

puts "rspec pid: #{Process.pid}"

trap 'USR1' do
  threads = Thread.list

  puts
  puts "=" * 80
  puts "Received USR1 signal; printing all #{threads.count} thread backtraces."

  threads.each do |thr|
    description = thr == Thread.main ? "Main thread" : thr.inspect
    puts
    puts "#{description} backtrace: "
    puts thr.backtrace.join("\n")
  end

  puts "=" * 80
end

That’ll print off the pid of the RSpec process, and then you can run this from another terminal session:

$ kill -USR1 <the pid>

…and it’ll print off the stacktraces of all threads. (Note that it’ll print to RSpec’s stdout, not to the terminal where you ran kill from).

The nice thing about this compared to ctrl-c is that you get the stacktraces for all threads and it doesn’t halt the process – so you can run it repeatedly to see if it’s staying stuck at one spot, or if it’s making progress but just going really slow or whatever.

Wanted to also point out that if you binding.pry a test in another window, then try to run RSpec, your test will wait for the other test to finish and therefore never run (unless you were to exit that debugging session).

Restarting your computer could solve this problem if you’re just really unsure where the process is but otherwise ps aux | grep ruby could probably show you then you could kill -9 the PID.

Thanks so much guys, and sorry for the delay in getting back. Not an rspec problem it turned out. I had a bunch of “zombie” processes hitting the server (only seemed to happen with RSpec though, because Minitest had no problems that I could see). I just had to type out

ps -ax | grep

in the terminal and yes,

killall <the name of the process>

or

kill <pid>

It took a while to figure out and go through all the different server processes (and the ones on my team mates laptops) but all good now. Cheers!

So try a couple of things, first off run try to run your specs with rspec /spec rather than rake spec that way if you have to CTRL-C you’ll get a trace of where your specs are stuck rather than just aborting the rake task.

Secondly do spring stop before running the specs to remove that as a possibility.

I just wanted to add something here for future internet explorers that find this post.

My test suite started hanging and I could not figure it out. No stack trace, no nothing. Restarted my computer and everything, thinking the testing server needed to reboot.

Turns out, I went to test the app manually and BOOM, I had a simple syntax error, unexpected end-of-input, expecting end in the controller that was causing the test suite to hang and there was no stack trace in the test suite to notify me about the syntax error.

Simple mistake on my part, and nothing to do with Rspec, but just something for others to look into.

@cicio look at the backtrace. The first 4 lines are from spec_helper.rb and are probably from the trap block you pasted into your spec_helper.rb.

After that comes lib/hangperson_game.rb:37 which is probably the source of your problem. You can also try sending the signal, waiting a bit (30 seconds or so), sending the signal again, etc. By sending it multiple times like this you can see if its always stuck at the exact same line or whether it is at different lines. If it’s at different lines, it suggests your code is running some slow algorithm, but is in fact making progress. If it’s the same line, maybe there’s some sort of blocking I/O there that just gets stuck.

Wowza, so also adding my experience to future explorers as well. And special h/t to @myronmarston for the trap backtrace code that uncovered the lead.

For us, our test suite was hanging at the very end. After implementing Myron’s code, we got the stack trace (abbreviated):

/home/semaphore/app/spec/spec_helper.rb:243:in `backtrace'14:40
/home/semaphore/app/spec/spec_helper.rb:243:in `block (3 levels) in <top (required)>'14:40
/home/semaphore/app/spec/spec_helper.rb:239:in `each'14:40
/home/semaphore/app/spec/spec_helper.rb:239:in `block (2 levels) in <top (required)>'14:40
/home/semaphore/app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.4.8/lib/rails/application/finisher.rb:138:in `lock'14:40
/home/semaphore/app/vendor/bundle/ruby/2.7.0/gems/railties-6.0.4.8/lib/rails/application/finisher.rb:138:in `run'14:40
/home/semaphore/app/vendor/bundle/ruby/2.7.0/gems/activesupport-6.0.4.8/lib/active_support/execution_wrapper.rb:28:in `before'

So, I explored the Railtie application finisher.rb code and saw it was doing something with mutex locking, probably the GIL. But what? So, after chasing this down wrongly, I ended up searching the right keywords: “rspec rails application finisher mutex hanging”, which then took me to right question/answer on StackOverflow: https://stackoverflow.com/questions/21313628/rspec-hangs-after-it-finishes-running-through-my-tests

In the end, it was SimpleCov that was the culprit. As reported:

When simplecov exists, by default it will try to merge the recorded coverage with what’s on disk. To avoid corruption it uses a lock file to guard this merge. Because my virtual box shared fs is not actually posix compliant, the lock file would never acquire, and silently block forever here.

NOTE: this has apparently been fixed in newer versions of SimpleCov (we were running an old version).