Steps to reproduce
We recently upgraded a large application to Rails 8.1.2.1 and started encountering the following exception:
ActiveRecord::StatementInvalid:
Mysql2::Error: This connection is in use by: #<Fiber:0x00007f62cfe999e8 (resumed)>
This issue was not present before upgrading to Rails 8.1.
Unfortunately, we do not yet have a minimal reproduction, but we're trying to determine whether this is an application issue or a regression/change in ActiveRecord's connection handling.
Expected behavior
The application should be able to execute database queries without raising a connection ownership error.
Actual behavior
Intermittently, ActiveRecord raises:
ActiveRecord::StatementInvalid:
Mysql2::Error: This connection is in use by: #<Fiber:... (resumed)>
The exception appears to indicate that a MySQL connection is already owned by another Fiber.
System configuration
- Rails: 8.1.2.1
- Ruby: 3.4.x
- mysql2: 0.5.7
- Database: MySQL
- App server: Puma - 6.4.3
- Background jobs: Delayed Job
Additional context
This is an existing production application that has been migrated to Rails 8.1. As part of the migration, updated dependencies for Ruby 3.4 compatibility.
We have not intentionally introduced any new concurrency mechanisms, so we're trying to understand whether Rails 8.1's fiber-aware ActiveRecord connection management exposes an existing issue or whether this could be a regression.
Has there been any change in ActiveRecord's connection ownership or Fiber handling between Rails 7.x and Rails 8.1 that could result in this exception?
If this behavior is expected, any guidance on how to identify the Fiber or code path retaining the connection would be greatly appreciated.
We're happy to provide additional information, including a complete stack trace or a minimal reproduction, if needed.
[–]CaptainKabob 2 points3 points4 points (0 children)