Skip to content

Commit e982267

Browse files
authored
Merge pull request rails#53891 from byroot/ac-avoid-pinned-connection
Action Cable pg adapter: avoid using a pinned connection
2 parents ae69680 + 76da968 commit e982267

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

actioncable/lib/action_cable/subscription_adapter/postgresql.rb

+5-6
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,17 @@ def shutdown
3535
end
3636

3737
def with_subscriptions_connection(&block) # :nodoc:
38-
ar_conn = ActiveRecord::Base.connection_pool.checkout.tap do |conn|
39-
# Action Cable is taking ownership over this database connection, and will
40-
# perform the necessary cleanup tasks
41-
ActiveRecord::Base.connection_pool.remove(conn)
42-
end
38+
# Action Cable is taking ownership over this database connection, and will
39+
# perform the necessary cleanup tasks.
40+
# We purposedly avoid #checkout to not end up with a pinned connection
41+
ar_conn = ActiveRecord::Base.connection_pool.new_connection
4342
pg_conn = ar_conn.raw_connection
4443

4544
verify!(pg_conn)
4645
pg_conn.exec("SET application_name = #{pg_conn.escape_identifier(identifier)}")
4746
yield pg_conn
4847
ensure
49-
ar_conn.disconnect!
48+
ar_conn&.disconnect!
5049
end
5150

5251
def with_broadcast_connection(&block) # :nodoc:

activerecord/lib/active_record/connection_adapters/abstract/connection_pool.rb

+8-8
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ def schedule_query(future_result) # :nodoc:
693693
Thread.pass
694694
end
695695

696+
def new_connection # :nodoc:
697+
connection = db_config.new_connection
698+
connection.pool = self
699+
connection
700+
rescue ConnectionNotEstablished => ex
701+
raise ex.set_pool(self)
702+
end
703+
696704
private
697705
def connection_lease
698706
@leases[ActiveSupport::IsolatedExecutionState.context]
@@ -878,14 +886,6 @@ def remove_connection_from_thread_cache(conn, owner_thread = conn.owner)
878886
end
879887
alias_method :release, :remove_connection_from_thread_cache
880888

881-
def new_connection
882-
connection = db_config.new_connection
883-
connection.pool = self
884-
connection
885-
rescue ConnectionNotEstablished => ex
886-
raise ex.set_pool(self)
887-
end
888-
889889
# If the pool is not at a <tt>@size</tt> limit, establish new connection. Connecting
890890
# to the DB is done outside main synchronized section.
891891
#--

0 commit comments

Comments
 (0)