Skip to content
This repository was archived by the owner on May 22, 2021. It is now read-only.
This repository was archived by the owner on May 22, 2021. It is now read-only.

Occasional deadlocks due to Rails convention for using mutexes #12

@machty

Description

@machty

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:

  1. 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
  2. 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 deadlock because 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions