Pre-load AR schemas and disconnect before forking DSL workers#2522
Open
Pre-load AR schemas and disconnect before forking DSL workers#2522
Conversation
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).
Morriar
reviewed
Mar 4, 2026
| # 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! |
Contributor
There was a problem hiding this comment.
Do we need this even with @number_of_workers == 1?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
With multiple workers, we've been hitting connection limits on CI when verifying DSL RBI files. This PR attempts to fix that problem.
Implementation
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. Together, this should ensure that child processes do not make any database connections, since most of our introspection should just be hitting the schema.
Tests
No new tests.