Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Jun 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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