diff --git a/lib/mini_profiler.rb b/lib/mini_profiler.rb index 109c4b75..8ec7db22 100644 --- a/lib/mini_profiler.rb +++ b/lib/mini_profiler.rb @@ -163,6 +163,7 @@ def tool_disabled_message(client_settings) def call(env) start = Process.clock_gettime(Process::CLOCK_MONOTONIC) + outer_context = current client_settings = ClientSettings.new(env, @storage, start) MiniProfiler.deauthorize_request if @config.authorization_mode == :allow_authorized @@ -415,8 +416,8 @@ def call(env) client_settings.handle_cookie([status, headers, body]) ensure - # Make sure this always happens - self.current = nil + # Make sure this always happens, restoring the context of an outer request on the same thread + self.current = outer_context end def matches_action?(action, env) diff --git a/spec/integration/mini_profiler_spec.rb b/spec/integration/mini_profiler_spec.rb index 3c24b445..e5381a45 100644 --- a/spec/integration/mini_profiler_spec.rb +++ b/spec/integration/mini_profiler_spec.rb @@ -717,4 +717,18 @@ def load_prof(response) expect(last_response.body).to include('This is the help menu') end end + + describe 'with a nested request on the same thread' do + it 'keeps the outer request profiled' do + inner = Rack::MiniProfiler.new(lambda { |env| [200, { Rack::CONTENT_TYPE => 'text/html' }, +'