Fix Rails 8 deadlock in KML feature import#51
Merged
Conversation
… Rails 8 deadlock `Importers::KML#geom_from_kml` ran each `ST_GeomFromKML` SQL in a freshly-spawned thread, manually checking out a separate connection from `ActiveRecord::Base.connection_pool`. The intent was fault isolation: if a single feature's KML was malformed and Postgres raised, the failure shouldn't poison the surrounding transaction. On Rails 7.x this worked because the test's transaction wasn't connection-pinned. Rails 7.1+ introduced per-test connection pinning under `use_transactional_fixtures = true`, and Rails 8.1 made the pin stricter. Now the spawned worker thread blocks indefinitely at `ConnectionPool#checkout` (it can't get a connection because the test thread has effectively claimed pool access), and the main thread blocks on `Thread#join`. Every KMZ-import controller spec hangs for as long as the test runner allows. Instead of crossing thread boundaries for fault containment, run the query inside a `transaction(requires_new: true)` SAVEPOINT on the existing connection. A `StatementInvalid` from a malformed feature rolls back only the savepoint; the surrounding transaction is unaffected. No second connection, no `Thread.new`, and no `.join` to deadlock on. KMZ specs now run in well under a second instead of timing out. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
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.
Summary
Importers::KML#geom_from_kmlran eachST_GeomFromKMLSQL in a freshly-spawned thread, manually checking out a separate connection fromActiveRecord::Base.connection_poolfor fault isolation: a malformed KML feature would raiseStatementInvalidon the worker connection, which Postgres aborts; on the test/main connection that abort would poison the surrounding transaction. The thread trick worked on Rails 7.x.Rails 7.1 introduced per-test connection pinning under
use_transactional_fixtures = true, and Rails 8.1 made the pin stricter. The spawned worker thread now blocks indefinitely atConnectionPool#checkout, and the main thread blocks onThread#join. Every KMZ-import controller spec hangs forever.Replace the thread+separate-connection pattern with a
transaction(requires_new: true)SAVEPOINT on the existing connection. AStatementInvalidfrom a malformed feature rolls back only the savepoint; the surrounding transaction is untouched. Same fault containment, no thread crossing.The savepoint approach is backwards-compatible to every Rails version this gem supports —
transaction(requires_new: true)has issued SAVEPOINTs since Rails 3.0.Bumped patch version to 3.10.1.
Test plan
use_transactional_fixturesfinish in <1s instead of hanging forever🤖 Generated with Claude Code