Skip to content

Commit

Permalink
Merge pull request #20973 from Fryguy/fix_current_request_bug
Browse files Browse the repository at this point in the history
Fix key collision with Thread[:current_request]

(cherry picked from commit c52be90)
  • Loading branch information
jrafanie authored and simaishi committed Jan 29, 2021
1 parent f2f37c5 commit cd7f402
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions lib/request_started_on_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@ def call(env)
complete_request
end

def start_request(path, started_on)
Thread.current[:current_request] = path
def start_request(path_info, started_on)
Thread.current[:current_request_path_info] = path_info
Thread.current[:current_request_started_on] = started_on
end

def complete_request
Thread.current[:current_request] = nil
Thread.current[:current_request_path_info] = nil
Thread.current[:current_request_started_on] = nil
end

Expand All @@ -29,7 +29,7 @@ def self.long_running_requests
allowable_request_start_time = long_request.ago

relevant_thread_list.each do |thread|
request = thread[:current_request]
request = thread[:current_request_path_info]
started_on = thread[:current_request_started_on]

# There's a race condition where the complete_request method runs in another
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/request_started_on_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
let(:fake_threads) { [@fake_thread] }

it "returns request, duration and thread" do
@fake_thread = {:current_request => "/api/ping", :current_request_started_on => 3.minutes.ago}
@fake_thread = {:current_request_path_info => "/api/ping", :current_request_started_on => 3.minutes.ago}
long_requests = described_class.long_running_requests.first
expect(long_requests[0]).to eql "/api/ping"
expect(long_requests[1]).to be_within(0.1).of(Time.now.utc - 3.minutes.ago)
expect(long_requests[2]).to eql @fake_thread
end

it "skips threads that haven't timed out yet" do
@fake_thread = {:current_request => "/api/ping", :current_request_started_on => 30.seconds.ago}
@fake_thread = {:current_request_path_info => "/api/ping", :current_request_started_on => 30.seconds.ago}
expect(described_class.long_running_requests).to be_empty
end

Expand Down

0 comments on commit cd7f402

Please sign in to comment.