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

Enhance Thread and Fiber Safety in ActiveRecordExaminer #353

Closed
wants to merge 8 commits into from
32 changes: 29 additions & 3 deletions lib/appmap/handler/rails/sql_handler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,42 @@ def database_type
end

def in_transaction?
ActiveRecord::Base.connection.open_transactions > 0
execute_in_context do
ActiveRecord::Base.connection_pool.with_connection do
ActiveRecord::Base.connection.open_transactions > 0
end
end
end

def execute_query(sql)
ActiveRecord::Base.connection.execute(sql).to_a
execute_in_context do
ActiveRecord::Base.connection_pool.with_connection do
coderberry marked this conversation as resolved.
Show resolved Hide resolved
ActiveRecord::Base.connection.execute(sql).to_a
end
end
end

private

# Assure Thread and Fiber safety for ActiveRecord queries.
# This is only necessary for applications using graphql-ruby.
# This change wraps the
coderberry marked this conversation as resolved.
Show resolved Hide resolved
def execute_in_context
coderberry marked this conversation as resolved.
Show resolved Hide resolved
if ActiveSupport::IsolatedExecutionState.context == :fiber
Fiber.new do
ActiveRecord::Base.connection_pool.with_connection { yield }
end.resume
else
Thread.new do
ActiveRecord::Base.connection_pool.with_connection { yield }
end
coderberry marked this conversation as resolved.
Show resolved Hide resolved
end
end
end
end

def call(_, started, finished, _, payload) # (name, started, finished, unique_id, payload)
def call(_, started, finished, _, payload)
# (name, started, finished, unique_id, payload)
coderberry marked this conversation as resolved.
Show resolved Hide resolved
return if AppMap.tracing.empty?

return if Thread.current[AppMap::Hook::Method::HOOK_DISABLE_KEY] == true
Expand Down