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

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

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

  • Add documentation in README

diff --git a/README.md b/README.md
index c0fb6f0..bf6374d 100644
--- a/README.md
+++ b/README.md
@@ -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:
diff --git a/lib/patches/net_patches.rb b/lib/patches/net_patches.rb
index a0dee12..69746c6 100644
--- a/lib/patches/net_patches.rb
+++ b/lib/patches/net_patches.rb
@@ -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
diff --git a/lib/prepend_net_http_patch.rb b/lib/prepend_net_http_patch.rb
new file mode 100644
index 0000000..4af87a0
--- /dev/null
+++ b/lib/prepend_net_http_patch.rb
@@ -0,0 +1,5 @@
+# frozen_string_literal: true
+
+module Rack
+  MINI_PROFILER_PREPEND_NET_HTTP_PATCH = true
+end

GitHub sha: d7e30177

This commit appears in #444 which was merged by OsamaSayegh.