From 76ce08a493fbd9f8c38d719f4c329bc2153204fc Mon Sep 17 00:00:00 2001 From: Matthew Draper Date: Wed, 6 Jul 2016 08:28:06 +0930 Subject: Check connection ownership before allowing a thread to release it A thread can only release a connection if it owns it, or it's owned by a thread that has died. --- .../active_record/connection_adapters/abstract_adapter.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb index d4b9e301bc..5140238571 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_adapter.rb @@ -184,7 +184,17 @@ module ActiveRecord # this method must only be called while holding connection pool's mutex def expire - @owner = nil + if in_use? + if @owner != Thread.current && @owner.alive? + raise ActiveRecordError, "Cannot expire connection, " << + "it is owned by a different thread: #{@owner}. " << + "Current thread: #{Thread.current}." + end + + @owner = nil + else + raise ActiveRecordError, 'Cannot expire connection, it is not currently leased.' + end end def unprepared_statement -- cgit v1.2.3