-
-
Notifications
You must be signed in to change notification settings - Fork 11
Occasional deadlocks due to Rails convention for using mutexes #12
Description
Rails loads table/column info from the database, and this data is turn used to define dynamic methods backed by database columns. The code that performs this initial column load is wrapped in a mutex, which prevents thread safety bugs, but if you're using async-postgres, there's a nondeterministic chance you'll get bitten by a ThreadError: deadlock; recursive locking error in (at least) the following case:
- Fiber 1 initializes/saves an ActiveRecord obj for the first time; the column metadata hasn't been loaded, so it locks the mutex, succeeds, and then goes on to load the column data from postgres, ultimately hitting
async_exec, which async-postgres modifies to return control back to the reactor - Fiber 2 initializes/saves an ActiveRecord obj, the column metadata still isn't marked as loaded, so it attempts to lock the mutex, but it hits the
deadlockbecause it's trying to lock the mutex from the same thread.
I'm not sure what the solution is here, wouldn't be surprised if this was biting falcon users too; note that I'm seeing this in a Sidekiq-ish style background worker that I wrote with the following libs:
gem "async", require: false
gem "async-http", require: false
gem 'async-redis', github: "socketry/async-redis", require: false
gem 'async-postgres', require: false
locked at the following versions
async (1.19.0)
console (~> 1.0)
nio4r (~> 2.3)
timers (~> 4.1)
async-http (0.46.3)
async (~> 1.19)
async-io (~> 1.18)
protocol-http (~> 0.8.0)
protocol-http1 (~> 0.8.0)
protocol-http2 (~> 0.9.0)
async-io (1.23.3)
async (~> 1.14)
async-postgres (0.1.0)
async (~> 1.3)
pg
I don't really have a solution at this time, is there perhaps a way I can preload all of these column datas? I looked at the falcon repo but didn't see anyone else running into this particular deadlock.