4
votes

My Rails application uses the inherited_resources gem. I am currently trying to speed it up to be able to handle a much larger data set. So I went forward (with the help of the Bullet gem) to use eager loading where it would prove helpful. Within inherited_resources it looks something like this:

def collection
  my_widgets ||= end_of_association_chain.includes(:association_one, :association_two, :association_three, :association_four)

  @widgets = case params[:filter]
                  when nil then my_widgets.all
                  when 'old' then my_widgets.old
                  when 'new' then my_widgets.new
                  end

  @final_collection
end

The new code is .includes(:association_one, :association_two, :association_three, :association_four)

This simple change sped up my loadtime in the logs by about 5 times for my test huge database. However, the browser would just sit there, blank. Nothing. With the chrome loading icon going.

When I kill the server in the Terminal, I get this rather unique error:

 ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread

Backtrace at the bottom.

I have already attempted the solution discussed in the top answer to this question: How can I serve requests concurrently with Rails 4? , with the following:

#config/environments/development.rb
config.cache_classes = false
config.eager_load = true
config.allow_concurrency=true

What am I missing here? Why does simply adding these associations into eager loading, which significantly speeds up things behind the scenes, lend me an unresponsive browser with this ThreadError?

    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:22:in `unlock'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:22:in `ensure in call'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/lock.rb:23:in `call'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/content_length.rb:15:in `call'
    /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:88:in `service'
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:138:in `service'
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/httpserver.rb:94:in `run'
    /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:294:in `block in start_thread'
/Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:48:in `shutdown': undefined method `shutdown' for nil:NilClass (NoMethodError)
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:280:in `block in start'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `call'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `join'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block (2 levels) in start'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `each'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:206:in `block in start'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:32:in `start'
    from /Users/johndoeuser/.rvm/rubies/ruby-2.2.1/lib/ruby/2.2.0/webrick/server.rb:162:in `start'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/handler/webrick.rb:34:in `run'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/rack-1.6.4/lib/rack/server.rb:286:in `start'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/server.rb:80:in `start'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:80:in `block in server'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `tap'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:75:in `server'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands/commands_tasks.rb:39:in `run_command!'
    from /Users/johndoeuser/.rvm/gems/ruby-2.2.1/gems/railties-4.2.6/lib/rails/commands.rb:17:in `'
    from bin/rails:4:in `require'
    from bin/rails:4:in `'

edit: I am starting to wonder if this is less of a thread error than my dataset being too large for eager loading, is there a threshold where you are eager loading so many objects that, while it loads quickly in the terminal, the browser just sits there forever?

1

1 Answers

1
votes

Is switching from WEBrick to Puma an option? I had a application that required concurrent requests. I was getting the error you described:

ERROR ThreadError: Attempt to unlock a mutex which is locked by another thread

Switching to Puma solved that problem right out of the box.