Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 15 additions & 16 deletions lib/spatial_features/importers/kml.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,24 +56,23 @@ def blank_feature?(feature)
end

def geom_from_kml(kml)
geom = nil
conn = nil

strip_altitude(kml)

# Do query in a new thread so we use a new connection (if the query fails it will poison the transaction of the current connection)
#
# We manually checkout a new connection since Rails re-uses DB connections across threads.
Thread.new do
conn = ActiveRecord::Base.connection_pool.checkout
geom = conn.select_value("SELECT ST_GeomFromKML(#{conn.quote(kml.to_s)})")
rescue ActiveRecord::StatementInvalid => e # Discard Invalid KML features
geom = nil
ensure
ActiveRecord::Base.connection_pool.checkin(conn) if conn
end.join

return geom
# Run the parse inside a SAVEPOINT so a `ST_GeomFromKML` failure on a single
# invalid feature rolls back only that savepoint, not the surrounding
# transaction. The previous implementation spawned a thread and checked out
# a fresh connection for the same isolation reason, but on Rails 8 that
# pattern deadlocks under `use_transactional_fixtures` — the test pins its
# connection, the spawned thread blocks indefinitely waiting on
# `ActiveRecord::Base.connection_pool.checkout`. A nested transaction
# (`requires_new: true`) gives identical fault containment without crossing
# thread boundaries.
ActiveRecord::Base.transaction(requires_new: true) do
conn = ActiveRecord::Base.connection
conn.select_value("SELECT ST_GeomFromKML(#{conn.quote(kml.to_s)})")
end
rescue ActiveRecord::StatementInvalid # Discard invalid KML features
nil
end

def images_from_metadata(metadata)
Expand Down
2 changes: 1 addition & 1 deletion lib/spatial_features/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module SpatialFeatures
VERSION = "3.10.0"
VERSION = "3.10.1"
end
Loading