Open dbil25 opened 11 months ago
@dbil25 just run into the same issue and your solution helped me fix the issue! Thank you very much :)
I have a similar issue when upgrading Rails from 7.0 to 7.1. When running the full spec suite, rspec eventually stops without exiting (in a model spec). It runs fine with Rails 7.0.
The main thread backtrace reports:
/.../.rbenv/versions/3.3.4/lib/ruby/gems/3.3.0/gems/activesupport-7.1.4.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:55:in `lock'
# ...
/.../.rbenv/versions/3.3.4/lib/ruby/3.3.0/forwardable.rb:240:in `switch!'
/.../spec/support/apartment.rb:22:in `block (2 levels) in <main>'
# spec/support/apartment.rb
RSpec.configure do |config|
config.before(:each) do |example|
Apartment::Tenant.switch! @tenant_name
end
end
# active_support/concurrency/load_interlock_aware_monitor.rb
def mon_enter
@mutex.lock if @owner != Thread.current
# …
end
The PostgreSQLAdapter
patch above does not resolve the lock.
use_schemas: true
Steps to reproduce
In rspec, especially in feature specs, multiple threads can run concurrently ( for example the test itself and the test server) but they both use the same ActiveRecord database connection. Thats makes it that when a spec changes the schema by doig a
SET search_path TO
, then it can also affects the other threads causing unexpected behaviors. At least thats what i understood after debugging some very flaky behavior in the feature specs.This does not happen in a normal context (development and production), it only happens during specs, especially feature specs.
Is there an existing solution for this kind of bugs? meanwhile, if other people encounter the same problem i have a working fix by adding a monkey-patch in a
spec/support
file:System configuration
Database: postgres 14
Apartment version: 2.11
Apartment config (in
config/initializers/apartment.rb
or so):use_schemas
:true
Rails (or ActiveRecord) version: 7.0.8
Ruby version: 3.2.1