Skip to content
This repository was archived by the owner on Mar 26, 2024. It is now read-only.

Commit 1846453

Browse files
committed
Fix Timeout interrupt handling on Ruby 2.3 and protect Mysql2::Statement#execute
Timeout::ExitException was removed in Ruby 2.3.0, 2.2.3, and 2.1.8, in favor of Timeout::Error. Backwards compatible aliases are provided for Ruby 2.1.x and 2.2.x, but not earlier verions. With thanks to @jeremy for PR brianmario#671 and @yui-knk for PR brianmario#677, this commit also protects prepared statements from being interrupted, so the compat shim is in Mysql2::Util.
1 parent 2652fdb commit 1846453

File tree

3 files changed

+20
-3
lines changed

3 files changed

+20
-3
lines changed

lib/mysql2.rb

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,22 @@ def self.key_hash_as_symbols(hash)
6161
Hash[hash.map { |k,v| [k.to_sym, v] }]
6262
end
6363

64+
#
65+
# In Mysql2::Client#query and Mysql2::Statement#execute,
66+
# Thread#handle_interrupt is used to prevent Timeout#timeout
67+
# from interrupting query execution.
68+
#
69+
# Timeout::ExitException was removed in Ruby 2.3.0, 2.2.3, and 2.1.8,
70+
# but is present in earlier 2.1.x and 2.2.x, so we provide a shim.
71+
#
72+
if Thread.respond_to?(:handle_interrupt)
73+
require 'timeout'
74+
# rubocop:disable Style/ConstantName
75+
TimeoutError = if defined?(::Timeout::ExitException)
76+
::Timeout::ExitException
77+
else
78+
::Timeout::Error
79+
end
80+
end
81+
6482
end

lib/mysql2/client.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ def self.default_query_options
7676

7777
if Thread.respond_to?(:handle_interrupt)
7878
def query(sql, options = {})
79-
Thread.handle_interrupt(Timeout::ExitException => :never) do
79+
Thread.handle_interrupt(::Mysql2::Util::TimeoutError => :never) do
8080
_query(sql, @query_options.merge(options))
8181
end
8282
end

spec/mysql2/client_spec.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,8 +462,7 @@ def connect *args
462462
}.should raise_error(Mysql2::Error)
463463
end
464464

465-
466-
it 'should be impervious to connection-corrupting timeouts ' do
465+
it 'should be impervious to connection-corrupting timeouts in #query' do
467466
pending('`Thread.handle_interrupt` is not defined') unless Thread.respond_to?(:handle_interrupt)
468467
# attempt to break the connection
469468
expect { Timeout.timeout(0.1) { @client.query('SELECT SLEEP(1)') } }.to raise_error(Timeout::Error)

0 commit comments

Comments
 (0)