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 Timeout interrupt handling on Ruby 2.3 #671

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 11 additions & 1 deletion lib/mysql2/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,18 @@ def self.default_query_options
if Thread.respond_to?(:handle_interrupt)
require 'timeout'

# Timeout::ExitException was removed in Ruby 2.3.0, 2.2.3, and 2.1.8,
# but is present in earlier 2.1.x and 2.2.x, so we provide a shim.
#
# Set our own constant instead of modifying core ::Timeout.
TimeoutError = if defined?(::Timeout::ExitException)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it better or worse to use a variable instead of a constant?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Idiomatic to use a constant when it won't be reassigned and we want lexical lookup from an instance method. If this is a value we needed to configure, using a class or instance attribute would make sense.

::Timeout::ExitException
else
::Timeout::Error
end

def query(sql, options = {})
Thread.handle_interrupt(::Timeout::ExitException => :never) do
Thread.handle_interrupt(TimeoutError => :never) do
_query(sql, @query_options.merge(options))
end
end
Expand Down