ActiveRecord::StatementInvalid / Mysql2 Fiber connection error after upgrading to Rails 8.1

Hi all,

After upgrading a large production app from Rails 7.0.4 / Ruby 3.0.2 to Rails 8.1.2.1 / Ruby 3.4.8, we started seeing intermittent DB connection ownership errors that were not present before.

Error with mysql2:

ActiveRecord::StatementInvalid:
Mysql2::Error: This connection is in use by: #<Fiber:... (resumed)>

We also tried switching from mysql2 to trilogy (2.12) and saw a similar error:

ActiveRecord::StatementInvalid:
Trilogy::SynchronizationError: This connection is already in use by another thread or fiber

Environment:

  • Rails 8.1.2.1

  • Ruby 3.4.8

  • mysql2 0.5.7

  • MySQL

  • Puma 6.4.3

  • Delayed Job

We have not intentionally introduced new concurrency primitives, so we’re trying to understand whether Rails 8.1 is exposing a pre-existing issue in our app, or whether there were changes in ActiveRecord connection ownership / fiber handling between Rails 7.x and 8.1 that could explain this.

We do not yet have a minimal reproduction, but we can share a full stack trace if helpful.

Has anyone seen this after upgrading to Rails 8.1 or Ruby 3.4? Also, is there a recommended way to identify which fiber/code path is retaining the connection?

Thanks.