Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug in FiberedMysql2Adapter_5_2 where steal! needed to update its owner to Fiber.current #2

Merged
merged 2 commits into from
Feb 12, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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