gitlab-org--gitlab-foss/lib/peek/views/rugged.rb
Stan Hu 7a5c4cd0ca Fix SystemStackError when Peek bar is active with Rugged calls
Peek attempts to serialize results with `to_json`, which calls
`ActiveSupport::JSON`. If an object is passed to `to_json` that contains
instance variables, `ActiveSupport` will attempt to recursively traverse
all variables.

The problem is that we can get into an infinite loop if the instance
references to an instance that references to something else that points
back to the same instance.

To avoid this mess, we just call `to_s` on the object. It appears only
`Gitlab::Git::Repository` and `::Repository` are the culprits here.

Closes https://gitlab.com/gitlab-org/gitlab-ce/issues/65404
2019-07-31 15:47:19 -07:00

46 lines
1.1 KiB
Ruby

# frozen_string_literal: true
module Peek
module Views
class Rugged < DetailedView
def results
return {} unless calls > 0
super
end
private
def duration
::Gitlab::RuggedInstrumentation.query_time
end
def calls
::Gitlab::RuggedInstrumentation.query_count
end
def call_details
::Gitlab::RuggedInstrumentation.list_call_details
end
def format_call_details(call)
super.merge(args: format_args(call[:args]))
end
def format_args(args)
args.map do |arg|
# ActiveSupport::JSON recursively calls as_json on all
# instance variables, and if that instance variable points to
# something that refers back to the same instance, we can wind
# up in an infinite loop. Currently this only seems to happen with
# Gitlab::Git::Repository and ::Repository.
if arg.instance_variables.present?
arg.to_s
else
arg
end
end
end
end
end
end