ruby-prof / ruby-prof

A ruby profiler. See https://ruby-prof.github.io for more information.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Profiling Rails RuntimeError: RubyProf is not running

shaneardell opened this issue · comments

I'm attempting to get started with profiling my existing Rails application and followed the instructions in the REAMDE under Profiling Rails.

I've created a :profile environment, added the gem to the gemfile, and updated config.ru to use the rack adapter.

I am able to start my server locally just fine using the profile environment, however I get the following in the browser:
An unhandled lowlevel error occurred. The application logs may have details.

And I get the following stacktrace:

2019-10-30 11:48:35 -0600: Rack app error handling request { GET / }
#<RuntimeError: RubyProf is not running.>
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/ruby-prof-1.0.0/lib/ruby-prof/rack.rb:75:in `resume'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/ruby-prof-1.0.0/lib/ruby-prof/rack.rb:75:in `resume'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/ruby-prof-1.0.0/lib/ruby-prof/rack.rb:26:in `call'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/puma-3.12.0/lib/puma/configuration.rb:225:in `call'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/puma-3.12.0/lib/puma/server.rb:658:in `handle_request'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/puma-3.12.0/lib/puma/server.rb:472:in `process_client'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/puma-3.12.0/lib/puma/server.rb:332:in `block in run'
/Users/shaneardell/.rvm/gems/ruby-2.4.0@hit-server/gems/puma-3.12.0/lib/puma/thread_pool.rb:133:in `block in spawn_thread'

Is there another step that I missed around calling RubyProf.start somewhere?

Thanks for any guidance you can offer!

Good question, must be a bug in the rack adapter. When the adapter gets created the profiler should get start and then immediately get paused. Will have to take a look.

Ok, I wasn't able to duplicate this. Maybe try the latest version of ruby-prof?

I get the same error even after upgrading to 1.3.1. It doesn't always happen on the first few requests, but seems to be related to interleaved requests (I have puma running with 5 threads). I tried setting the max threads to 1, but that seems that makes my application unhappy in other ways such that I'm unable to use it.

Here is a cleaned up log showing the problem. Each line starts with a request ID, and I've added some debugging statements to initialize, pause, resume and call. You can see that two concurrent requests are sharing the same Rack::RubyProf::RackProfiler and its contained RubyProf::Profile. I have verified that these requests are on different threads. This leads to a double pause and consequent runtime error:

[702c9853-b3cc-424b-8595-029b99d6a507] Started GET "routeA" for 127.0.0.1 at 2020-04-14 12:12:49 +0930
[702c9853-b3cc-424b-8595-029b99d6a507] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:154 initialize @profile: #<RubyProf::Profile:0x00007ffb5b81b5d8>

[9b3dd142-c696-4074-81ec-5427c39a79b4] Started GET "routeB" for 127.0.0.1 at 2020-04-14 12:12:49 +0930
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:154 initialize @profile: #<RubyProf::Profile:0x00007ffb5a376a88>

[702c9853-b3cc-424b-8595-029b99d6a507] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:26 resume [hex(object_id), hex(@profile.object_id)]: ["0x7ffb5b81b650", "0x7ffb5b81b5d8"]
[702c9853-b3cc-424b-8595-029b99d6a507] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:11 call profiler: #<Rack::RubyProf::RackProfiler:0x00007ffb5a376ad8 @options={:path=>"./tmp/ruby-prof", :min_percent=>1}, @profile=#<RubyProf::Profile:0x00007ffb5a376a88>, @printer_klasses={RubyProf::FlatPrinter=>"flat.txt", RubyProf::GraphPrinter=>"graph.txt", RubyProf::GraphHtmlPrinter=>"graph.html", RubyProf::CallStackPrinter=>"call_stack.html"}, @tmpdir="./tmp/ruby-prof", @max_requests=1, @requests_count=0, @printed=false>
[702c9853-b3cc-424b-8595-029b99d6a507] Processing by ControllerA#index as JSON

[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:26 resume [hex(object_id), hex(@profile.object_id)]: ["0x7ffb5a376ad8", "0x7ffb5a376a88"]
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:11 call profiler: #<Rack::RubyProf::RackProfiler:0x00007ffb5a376ad8 @options={:path=>"./tmp/ruby-prof", :min_percent=>1}, @profile=#<RubyProf::Profile:0x00007ffb5a376a88>, @printer_klasses={RubyProf::FlatPrinter=>"flat.txt", RubyProf::GraphPrinter=>"graph.txt", RubyProf::GraphHtmlPrinter=>"graph.html", RubyProf::CallStackPrinter=>"call_stack.html"}, @tmpdir="./tmp/ruby-prof", @max_requests=1, @requests_count=0, @printed=false>
[9b3dd142-c696-4074-81ec-5427c39a79b4] Processing by ControllerB#index as JSON

[702c9853-b3cc-424b-8595-029b99d6a507] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (0.49ms)
[702c9853-b3cc-424b-8595-029b99d6a507] Completed 200 OK in 329ms (Views: 34.3ms | ActiveRecord: 134.6ms)
[702c9853-b3cc-424b-8595-029b99d6a507] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:31 pause [hex(object_id), hex(@profile.object_id)]: ["0x7ffb5a376ad8", "0x7ffb5a376a88"]

[9b3dd142-c696-4074-81ec-5427c39a79b4] [active_model_serializers] Rendered ActiveModel::Serializer::Null with Hash (1.09ms)
[9b3dd142-c696-4074-81ec-5427c39a79b4] Completed 200 OK in 356ms (Views: 3.7ms | ActiveRecord: 160.5ms)
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof-1.3.1/lib/ruby-prof/rack.rb:31 pause [hex(object_id), hex(@profile.object_id)]: ["0x7ffb5a376ad8", "0x7ffb5a376a88"]
[9b3dd142-c696-4074-81ec-5427c39a79b4]
[9b3dd142-c696-4074-81ec-5427c39a79b4] RuntimeError (RubyProf is not running.):
[9b3dd142-c696-4074-81ec-5427c39a79b4]
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof (1.3.1) lib/ruby-prof/rack.rb:86:in 'pause'
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof (1.3.1) lib/ruby-prof/rack.rb:86:in 'pause'
[9b3dd142-c696-4074-81ec-5427c39a79b4] ruby-prof (1.3.1) lib/ruby-prof/rack.rb:31:in 'call'

I'm using Rails 5.1.7, Rack 2.0.7, ruby-prof 1.3.1, puma 3.12.1 on MRI 2.6.3 on OSX.

Ah - I see what you mean. Yeah that was broken in this commit: 3ef8889

The idea was aggregate different calls, but its not thread safe. I'll revert that change.