From 18a447ed64ec552131d2ac4a8861005aec0b372f Mon Sep 17 00:00:00 2001 From: Ufuk Kayserilioglu Date: Wed, 4 Mar 2026 18:47:42 +0200 Subject: [PATCH] Pre-load AR schemas and disconnect before forking DSL workers Pre-load the schema for all ActiveRecord models in the parent process before forking parallel workers, so the schema cache is inherited via copy-on-write. Then clear all database connections so forked children don't inherit stale connections from the parent, avoiding exceeding connection limits (e.g. Semian ticket counts). --- lib/tapioca/dsl/pipeline.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/lib/tapioca/dsl/pipeline.rb b/lib/tapioca/dsl/pipeline.rb index ffcd88ef9..be45fe53e 100644 --- a/lib/tapioca/dsl/pipeline.rb +++ b/lib/tapioca/dsl/pipeline.rb @@ -73,6 +73,21 @@ def run(&blk) if defined?(::ActiveRecord::Base) && constants_to_process.any? { |c| ::ActiveRecord::Base > c } abort_if_pending_migrations! + + # Pre-load the schema for all AR models we're about to process. This populates the schema cache in the + # parent process so that forked workers inherit it via copy-on-write and don't each need to establish their + # own database connection just to load schema information. + constants_to_process.each do |c| + next unless ::ActiveRecord::Base > c + + ar_model = c #: as singleton(::ActiveRecord::Base) + ar_model.load_schema + end + + # Disconnect all database connections before forking workers. The migration check and schema pre-loading + # above may have established connections; clearing them ensures forked children don't inherit stale + # connections from the parent, avoiding exceeding connection limits (e.g. Semian ticket counts). + ::ActiveRecord::Base.connection_handler.clear_all_connections! end result = Executor.new(