Skip to content

Commit 3d856c0

Browse files
committed
Don't clear ActiveRecord connections when in nested job
1 parent 36686cc commit 3d856c0

2 files changed

Lines changed: 47 additions & 10 deletions

File tree

lib/que/active_record/connection.rb

Lines changed: 28 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
module Que
44
module ActiveRecord
55
class << self
6+
def active_rails_executor?
7+
defined?(::Rails.application.executor) && ::Rails.application.executor.active?
8+
end
9+
610
def wrap_in_rails_executor(&block)
711
if defined?(::Rails.application.executor)
812
::Rails.application.executor.wrap(&block)
@@ -39,17 +43,31 @@ def call(job)
3943
yield
4044
end
4145

42-
# ActiveRecord will check out connections to the current thread when
43-
# queries are executed and not return them to the pool until
44-
# explicitly requested to. I'm not wild about this API design, and
45-
# it doesn't pose a problem for the typical case of workers using a
46-
# single PG connection (since we ensure that connection is checked
47-
# in and checked out responsibly), but since ActiveRecord supports
48-
# connections to multiple databases, it's easy for people using that
49-
# feature to unknowingly leak connections to other databases. So,
50-
# take the additional step of telling ActiveRecord to check in all
51-
# of the current thread's connections after each job is run.
46+
clear_active_connections_if_needed!(job)
47+
end
48+
49+
private
50+
51+
# ActiveRecord will check out connections to the current thread when
52+
# queries are executed and not return them to the pool until
53+
# explicitly requested to. I'm not wild about this API design, and
54+
# it doesn't pose a problem for the typical case of workers using a
55+
# single PG connection (since we ensure that connection is checked
56+
# in and checked out responsibly), but since ActiveRecord supports
57+
# connections to multiple databases, it's easy for people using that
58+
# feature to unknowingly leak connections to other databases. So,
59+
# take the additional step of telling ActiveRecord to check in all
60+
# of the current thread's connections after each job is run.
61+
def clear_active_connections_if_needed!(job)
62+
# don't clean in synchronous mode
63+
# see https://github.com/que-rb/que/pull/393
5264
return if job.class.resolve_que_setting(:run_synchronously)
65+
66+
# don't clear connections in nested jobs executed synchronously
67+
# i.e. while we're still inside of the rails executor
68+
# see https://github.com/que-rb/que/pull/412#issuecomment-2194412783
69+
return if Que::ActiveRecord.active_rails_executor?
70+
5371
if ::ActiveRecord.version >= Gem::Version.new('7.1')
5472
::ActiveRecord::Base.connection_handler.clear_active_connections!(:all)
5573
else

spec/que/active_record/connection_spec.rb

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,5 +66,24 @@ def run(*args)
6666

6767
assert SecondDatabaseModel.connection_handler.active_connections?
6868
end
69+
70+
it "shouldn't clear connections to secondary DBs if within an active rails executor" do
71+
# This is a hacky spec, but it's better than requiring Rails.
72+
rails, application, executor = 3.times.map { Object.new }
73+
application.define_singleton_method(:executor) { executor }
74+
rails.define_singleton_method(:application) { application }
75+
executor.define_singleton_method(:wrap) { |&block| block.call }
76+
executor.define_singleton_method(:active?) { true }
77+
78+
refute defined?(::Rails)
79+
::Rails = rails
80+
81+
SecondDatabaseModelJob.run
82+
83+
assert SecondDatabaseModel.connection_handler.active_connections?
84+
85+
Object.send :remove_const, :Rails
86+
refute defined?(::Rails)
87+
end
6988
end
7089
end

0 commit comments

Comments
 (0)