Skip to content

New Advisory Lock that opens a second connection causes bug in Postgres app #40029

Description

@jules2689

Steps to reproduce

  1. Use Rails after Move advisory lock to it's own connection #38235 was merged
  2. Use postgres 11, pg gem 1.1.4 (might be relevant, I'm unsure)
  3. Run bin/rails db:create db:migrate db:test:prepare
  4. Get error ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR: database "team_app" is being accessed by other users DETAIL: There is 1 other session using the database.

Note: I can only replicate on CI

Expected behavior

  1. Run bin/rails db:create db:migrate db:test:prepare
  2. Succeeds

Actual behavior

I was able to bisect the diff between Rails 6.0.2.2 and Rails 6.0.3 and determine that this PR (#38235) introduced a bug that causes migrations to fail in my app's CI.

When my CI system runs bin/rails db:create db:migrate db:test:prepare, it used to succeed with no problem in 6.0.2.2. Now it fails with:

ActiveRecord::StatementInvalid: PG::ObjectInUse: ERROR:  database "team_app" is being accessed by other users
DETAIL:  There is 1 other session using the database.
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `async_exec'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:92:in `block (2 levels) in execute'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.1/lib/active_support/dependencies/interlock.rb:48:in `block in permit_concurrent_loads'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.1/lib/active_support/concurrency/share_lock.rb:187:in `yield_shares'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.1/lib/active_support/dependencies/interlock.rb:47:in `permit_concurrent_loads'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:91:in `block in execute'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activerecord-6.0.3.1/lib/active_record/connection_adapters/abstract_adapter.rb:722:in `block (2 levels) in log'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:26:in `block (2 levels) in synchronize'
/home/runner/work/team_app/team_app/vendor/bundle/ruby/2.6.0/gems/activesupport-6.0.3.1/lib/active_support/concurrency/load_interlock_aware_monitor.rb:25:in `handle_interrupt'
...

Oddly enough, I cannot replicate this locally, only on CI, so I'm at a loss of next steps.
The Postgres that I am using is the default postgres that comes with GitHub Actions.

Workaround

Split up the command. Instead of bin/rails db:create db:migrate db:test:prepare, run bin/rails db:create db:migrate; bin/rails db:test:prepare`. This seems to clean up connections and we are all happy.

System configuration

Rails version: bf11820
Ruby version: 2.6.6

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions