Skip to content

Commit

Permalink
Fix bug in FiberedMysql2Adapter_5_2 - steal! needed to update its own…
Browse files Browse the repository at this point in the history
…er to Fiber.current instead of Thread.current
  • Loading branch information
nburwell committed Feb 12, 2021
1 parent 3d9bf9c commit 13636ad
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Expand Up @@ -4,11 +4,16 @@ Inspired by [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).

Note: this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.1.1] - UNRELEASED
### Fixed
- Fixed bug with Rails 5+ adapter where connections that have `steal!` called on them were not having their owner updated to the current Fiber, which would then cause an exception when trying to expire the connection (this showed up with the Rails 5 `ConnectionPool::Reaper` that reaps unused connections)

## [0.1.0] - 2020-10-23
### Added
- Added an adapter for Rails 4, 5, and 6.
- Added appraisals for Rails 4, 5, and 6.
- Added TravisCI unit test pipeline.
- Added coverage reports via Coveralls.

[0.1.1]: https://github.com/Invoca/fibered_mysql2/compare/v0.1.0..v0.1.1
[0.1.0]: https://github.com/Invoca/fibered_mysql2/tree/v0.1.0
2 changes: 1 addition & 1 deletion Gemfile.lock
@@ -1,7 +1,7 @@
PATH
remote: .
specs:
fibered_mysql2 (0.1.0)
fibered_mysql2 (0.1.1)
em-synchrony (~> 1.0)
rails (>= 4.2, < 7)

Expand Down
12 changes: 12 additions & 0 deletions lib/active_record/connection_adapters/fibered_mysql2_adapter.rb
Expand Up @@ -49,6 +49,18 @@ def expire
raise ::ActiveRecord::ActiveRecordError, "Cannot expire connection, it is not currently leased."
end
end

def steal!
if in_use?
if @owner != Fiber.current
pool.send :remove_connection_from_thread_cache, self, @owner

@owner = Fiber.current
end
else
raise ::ActiveRecord::ActiveRecordError, "Cannot steal connection, it is not currently leased."
end
end
end

class FiberedMysql2Adapter < ::ActiveRecord::ConnectionAdapters::EMMysql2Adapter
Expand Down
2 changes: 1 addition & 1 deletion lib/fibered_mysql2/version.rb
@@ -1,5 +1,5 @@
# frozen_string_literal: true

module FiberedMysql2
VERSION = "0.1.0"
VERSION = "0.1.1"
end
33 changes: 33 additions & 0 deletions spec/unit/fibered_mysql2_adapter_spec.rb
Expand Up @@ -67,5 +67,38 @@
end
end
end

context '#steal!' do
subject { adapter.steal! }

context 'if the connection is not in use' do
it 'raises' do
expect { subject }.to raise_exception(ActiveRecord::ActiveRecordError, "Cannot steal connection, it is not currently leased.")
end
end

context 'if the connection is being used' do
before do
ActiveRecord::Base.establish_connection(
adapter: 'fibered_mysql2',
database: 'widgets',
username: 'root',
pool: 10
)

adapter.pool = ActiveRecord::Base.connection_pool
adapter.lease
end

it { should be_nil }

it 'by a different Fiber' do
new_fiber = Fiber.new { subject }
new_fiber.resume

expect(adapter.owner).to eq(new_fiber)
end
end
end
end
end

0 comments on commit 13636ad

Please sign in to comment.