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

Fix key collision with Thread[:current_request] #20973

Merged
merged 1 commit into from
Jan 21, 2021
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
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
jrafanie marked this conversation as resolved.
Show resolved Hide resolved
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