Skip to content

A platform for community discussion. Free, open, simple.

FIX: Return database connections to the pool after dashboard builds (#41583)

Krzysztof Kotlarekad472d1ca21a
diff --git a/app/services/admin_dashboard_section_loader.rb b/app/services/admin_dashboard_section_loader.rb
index 91f7aca1a0c..cf58a4faf95 100644
--- a/app/services/admin_dashboard_section_loader.rb
+++ b/app/services/admin_dashboard_section_loader.rb
@@ -36,7 +36,9 @@ class AdminDashboardSectionLoader
section_ids.each do |id|
self.class.thread_pool.post do
- results << { id: id, data: section_data(id, current_user) }
+ ActiveRecord::Base.with_connection(prevent_permanent_checkout: true) do
+ results << { id: id, data: section_data(id, current_user) }
+ end
rescue StandardError => e
results << { id: id, error: e }
end
diff --git a/spec/services/admin_dashboard_section_loader_spec.rb b/spec/services/admin_dashboard_section_loader_spec.rb
index f1b5cb2ce1a..7b11811bfe4 100644
--- a/spec/services/admin_dashboard_section_loader_spec.rb
+++ b/spec/services/admin_dashboard_section_loader_spec.rb
@@ -99,3 +99,39 @@ describe AdminDashboardSectionLoader do
end
end
end
+
+describe AdminDashboardSectionLoader do
+ self.use_transactional_tests = false
+
+ after do
+ if thread_pool = described_class.instance_variable_get(:@thread_pool)
+ thread_pool.shutdown
+ thread_pool.wait_for_termination(timeout: 1)
+ described_class.remove_instance_variable(:@thread_pool)
+ end
+ end
+
+ it "returns database connections to the pool once a section finishes building" do
+ worker_thread = nil
+ loader = ->(start_date:, end_date:, current_user:) do
+ worker_thread = Thread.current
+ ActiveRecord::Base.connection.execute("SELECT 1")
+ { value: "leaky" }
+ end
+ DiscoursePluginRegistry.stubs(:admin_dashboard_sections).returns(
+ [{ id: "leaky", enabled: -> { true }, loader: loader }],
+ )
+
+ described_class.build(
+ section_ids: ["leaky"],
+ current_user: Discourse.system_user,
+ start_date: "2026-05-01",
+ end_date: "2026-05-07",
+ )
+
+ expect(worker_thread).not_to eq(Thread.current)
+ wait_for do
+ ActiveRecord::Base.connection_pool.connections.none? { |c| c.owner == worker_thread }
+ end
+ end
+end