mirror of
https://github.com/discourse/discourse.git
synced 2025-02-25 18:55:32 -06:00
DEV: Report simultaneous use of PG::Connection objects
This commit is contained in:
parent
66893c020f
commit
be3e4ab3f5
@ -24,6 +24,8 @@ if ENV["TRACE_PG_CONNECTIONS"]
|
|||||||
FileUtils.mkdir_p(TRACE_DIR)
|
FileUtils.mkdir_p(TRACE_DIR)
|
||||||
@trace_filename = "#{TRACE_DIR}/#{Process.pid}_#{self.object_id}.txt"
|
@trace_filename = "#{TRACE_DIR}/#{Process.pid}_#{self.object_id}.txt"
|
||||||
trace File.new(@trace_filename, "w")
|
trace File.new(@trace_filename, "w")
|
||||||
|
@access_log_mutex = Mutex.new
|
||||||
|
@accessor_thread = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -34,5 +36,47 @@ if ENV["TRACE_PG_CONNECTIONS"]
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def log_access(&blk)
|
||||||
|
@access_log_mutex.synchronize do
|
||||||
|
if !@accessor_thread.nil?
|
||||||
|
Rails.logger.error <<~STRING
|
||||||
|
PG Clash: A connection is being accessed from two locations
|
||||||
|
|
||||||
|
#{@accessor_thread} was using the connection. Backtrace:
|
||||||
|
|
||||||
|
#{@accessor_thread.backtrace.join("\n")}
|
||||||
|
|
||||||
|
#{Thread.current} is now attempting to use the connection. Backtrace:
|
||||||
|
|
||||||
|
#{Thread.current.backtrace.join("\n")}
|
||||||
|
STRING
|
||||||
|
end
|
||||||
|
@accessor_thread = Thread.current
|
||||||
|
end
|
||||||
|
result = yield
|
||||||
|
@access_log_mutex.synchronize do
|
||||||
|
@accessor_thread = nil
|
||||||
|
end
|
||||||
|
result
|
||||||
|
end
|
||||||
|
|
||||||
end)
|
end)
|
||||||
|
|
||||||
|
class PG::Connection
|
||||||
|
LOG_ACCESS_METHODS = [:exec, :sync_exec, :async_exec,
|
||||||
|
:sync_exec_params, :async_exec_params,
|
||||||
|
:sync_prepare, :async_prepare,
|
||||||
|
:sync_exec_prepared, :async_exec_prepared,
|
||||||
|
]
|
||||||
|
|
||||||
|
LOG_ACCESS_METHODS.each do |method|
|
||||||
|
new_method = "#{method}_without_logging".to_sym
|
||||||
|
alias_method new_method, method
|
||||||
|
|
||||||
|
define_method(method) do |*args, &blk|
|
||||||
|
log_access { send(new_method, *args, &blk) }
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Loading…
Reference in New Issue
Block a user