Skip to content

Commit

Permalink
DEV: Allow Net::HTTP#request patch to be applied with module prepend …
Browse files Browse the repository at this point in the history
…or method aliasing (#444)

* DEV: Allow Net::HTTP#request patch to be applied with module prepend to method aliasing

* Add documentation in README
  • Loading branch information
OsamaSayegh authored Jun 11, 2020
1 parent 44b01ff commit d7e3017
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 8 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ If you don't want to manually require Mini Profiler:
gem 'rack-mini-profiler', require: ['enable_rails_patches', 'rack-mini-profiler']
```

#### `Net::HTTP` stack level too deep errors

If you start seeing `SystemStackError: stack level too deep` errors from `Net::HTTP` after installing Mini Profiler, this means there is another patch for `Net::HTTP#request` that conflicts with Mini Profiler's patch in your application. To fix this, change `rack-mini-profiler` gem line in your `Gemfile` to the following:

```ruby
gem 'rack-mini-profiler', require: ['prepend_net_http_patch', 'rack-mini-profiler']
```

If you currently have `require: false`, remove the `'rack-mini-profiler'` string from the `require` array above so the gem line becomes like this:

```ruby
gem 'rack-mini-profiler', require: ['prepend_net_http_patch']
```

This conflict happens when a ruby method is patched twice, once using module prepend, and once using method aliasing. See this [ruby issue](https://bugs.ruby-lang.org/issues/11120) for details. The fix is to apply all patches the same way. Mini Profiler by default will apply its patch using method aliasing, but you can change that to module prepend by adding `require: ['prepend_net_http_patch']` to the gem line as shown above.

#### Rails and manual initialization

In case you need to make sure rack_mini_profiler is initialized after all other gems, or you want to execute some code before rack_mini_profiler required:
Expand Down
26 changes: 18 additions & 8 deletions lib/patches/net_patches.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,25 @@

if (defined?(Net) && defined?(Net::HTTP))

Net::HTTP.class_eval do
def request_with_mini_profiler(*args, &block)
request = args[0]
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
request_without_mini_profiler(*args, &block)
if defined?(Rack::MINI_PROFILER_PREPEND_NET_HTTP_PATCH)
module NetHTTPWithMiniProfiler
def request(request, *args, &block)
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
super
end
end
end
alias request_without_mini_profiler request
alias request request_with_mini_profiler
Net::HTTP.prepend(NetHTTPWithMiniProfiler)
else
Net::HTTP.class_eval do
def request_with_mini_profiler(*args, &block)
request = args[0]
Rack::MiniProfiler.step("Net::HTTP #{request.method} #{request.path}") do
request_without_mini_profiler(*args, &block)
end
end
alias request_without_mini_profiler request
alias request request_with_mini_profiler
end
end

end
5 changes: 5 additions & 0 deletions lib/prepend_net_http_patch.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

module Rack
MINI_PROFILER_PREPEND_NET_HTTP_PATCH = true
end

0 comments on commit d7e3017

Please sign in to comment.