Skip to content

Commit 3311ca0

Browse files
committed
Ensure actioncable connection is not a pinned connection
Fix thread safety issue in rails #53883 This was causing random errors in the db restore, generally during the first run of the cypress test per rails boot. Sometimes, it would hang. Other times, I'd get a segmentation fault. Other times, Bad file descriptor (Errno::EBADF) in actioncable-7.2.2.2/lib/action_cable/subscription_adapter/postgresql.rb:107:in `wait_for_notify'. I found that the prior commit helps to ensure the trigger disable and enable are both run and not just disabled in the the case of an error. But, on top of that, I found that rails had fixed a thread safety issue in action cable subscriptions's usage of a database connections. See: https://www.github.com/rails/rails/pull/53891 and https://www.github.com/rails/rails/issues/53883 "Action Cable must avoid using a pinned connection to subscribe to notifications as it can't be done in a thread safe way. So it must ensure it has a dedicated connection for that."
1 parent 0e9c077 commit 3311ca0

File tree

2 files changed

+30
-0
lines changed

2 files changed

+30
-0
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
module ActionCableSubscriptionAdapterPostgresqlPatch
2+
def with_subscriptions_connection(&block) # :nodoc:
3+
# Action Cable is taking ownership over this database connection, and will
4+
# perform the necessary cleanup tasks.
5+
# We purposely avoid #checkout to not end up with a pinned connection
6+
ar_conn = ActiveRecord::Base.connection_pool.send(:new_connection)
7+
pg_conn = ar_conn.raw_connection
8+
9+
verify!(pg_conn)
10+
11+
pg_conn.exec("SET application_name = #{pg_conn.escape_identifier(identifier)}")
12+
yield pg_conn
13+
ensure
14+
ar_conn&.disconnect!
15+
end
16+
end
17+
18+
# https://www.github.com/rails/rails/pull/53891 and https://www.github.com/rails/rails/issues/53883
19+
# Backported to 7-2-stable in https://github.com/rails/rails/commit/3e6e684c259e139db22f471069cd2cc0720d4bb4
20+
# Not yet released.
21+
ActiveSupport.on_load(:action_cable) do
22+
if ActionCable.version >= Gem::Version.new("7.2.3")
23+
message = "Patching ActionCable::SubscriptionAdapter::PostgreSQL - this is no longer needed in Rails 7.2.3 or newer! See: #{__FILE__}"
24+
Rails.logger.warn(message)
25+
message = "\e[33m#{message}\e[0m" if $stdout.tty?
26+
warn(message)
27+
end
28+
ActionCable::SubscriptionAdapter::PostgreSQL.prepend(ActionCableSubscriptionAdapterPostgresqlPatch)
29+
end

lib/manageiq/ui/classic.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
require "manageiq/ui/classic/engine"
22
require "manageiq/ui/classic/version"
33
require "extensions/ac_nested_params"
4+
require "extensions/ac_subscription_adapter_postgresql"
45

56
module ManageIQ
67
module UI

0 commit comments

Comments
 (0)