提交 c81a74c9 编写于 作者: S Sean Griffin

Merge pull request #18167 from al2o3cr/checkin_connection_leak

Fix connection leak when a thread checks in additional connections.
......@@ -363,7 +363,7 @@ def checkin(conn)
conn.expire
end
release owner
release conn, owner
@available.add conn
end
......@@ -376,7 +376,7 @@ def remove(conn)
@connections.delete conn
@available.delete conn
release conn.owner
release conn, conn.owner
@available.add checkout_new_connection if @available.any_waiting?
end
......@@ -424,10 +424,12 @@ def acquire_connection
end
end
def release(owner)
def release(conn, owner)
thread_id = owner.object_id
@reserved_connections.delete thread_id
if @reserved_connections[thread_id] == conn
@reserved_connections.delete thread_id
end
end
def new_connection
......
......@@ -35,6 +35,22 @@ def checkout_checkin_connections(pool_size, threads)
end
end
def checkout_checkin_connections_loop(pool_size, loops)
ActiveRecord::Base.establish_connection(@connection.merge({:pool => pool_size, :checkout_timeout => 0.5}))
@connection_count = 0
@timed_out = 0
loops.times do
begin
conn = ActiveRecord::Base.connection_pool.checkout
ActiveRecord::Base.connection_pool.checkin conn
@connection_count += 1
ActiveRecord::Base.connection.tables
rescue ActiveRecord::ConnectionTimeoutError
@timed_out += 1
end
end
end
def test_pooled_connection_checkin_one
checkout_checkin_connections 1, 2
assert_equal 2, @connection_count
......@@ -42,6 +58,20 @@ def test_pooled_connection_checkin_one
assert_equal 1, ActiveRecord::Base.connection_pool.connections.size
end
def test_pooled_connection_checkin_two
checkout_checkin_connections_loop 2, 3
assert_equal 3, @connection_count
assert_equal 0, @timed_out
assert_equal 2, ActiveRecord::Base.connection_pool.connections.size
end
def test_pooled_connection_remove
ActiveRecord::Base.establish_connection(@connection.merge({:pool => 2, :checkout_timeout => 0.5}))
old_connection = ActiveRecord::Base.connection
extra_connection = ActiveRecord::Base.connection_pool.checkout
ActiveRecord::Base.connection_pool.remove(extra_connection)
assert_equal ActiveRecord::Base.connection, old_connection
end
private
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册