Commit d09f8d12 authored by Kamil Trzciński's avatar Kamil Trzciński

Ensure that QueryAnalyzer handles recursive calls

It is possible that Query Analyzer to unintentionally
trigger SQL query. To prevent Stack Too Deep discover
recursive calls and prevent them.
parent 352a17b0
......@@ -15,9 +15,13 @@ module Gitlab
def hook!
@subscriber = ActiveSupport::Notifications.subscribe('sql.active_record') do |event|
# In some cases analyzer code might trigger another SQL call
# to avoid stack too deep this detects recursive call of subscriber
with_ignored_recursive_calls do
process_sql(event.payload[:sql], event.payload[:connection])
end
end
end
private
......@@ -57,6 +61,17 @@ module Gitlab
nil
end
def with_ignored_recursive_calls
return if Thread.current[:query_analyzer_recursive]
begin
Thread.current[:query_analyzer_recursive] = true
yield
ensure
Thread.current[:query_analyzer_recursive] = nil
end
end
end
end
end
......@@ -19,6 +19,15 @@ RSpec.describe Gitlab::Database::QueryAnalyzer do
Project.connection.execute("SELECT 1 FROM projects")
end
it 'does prevent recursive execution' do
expect(analyzer).to receive(:enabled?).and_return(true)
expect(analyzer).to receive(:analyze) do
Project.connection.execute("SELECT 1 FROM projects")
end
Project.connection.execute("SELECT 1 FROM projects")
end
end
describe '#process_sql' do
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment