Skip to content

Commit d45d186

Browse files
byrootetiennebarrie
andcommitted
Workaround for mysql2 prepared statements flaky test
Ref: brianmario/mysql2#1383 The proper fix can only be upstream, however we can reduce the likeliness of hitting the bug by only calling `#affected_rows` when strictly necessary, meaning when we performed a query that didn't return a result set. Co-Authored-By: Étienne Barrié <[email protected]>
1 parent 4060253 commit d45d186

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

activerecord/lib/active_record/connection_adapters/mysql2/database_statements.rb

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,12 @@ def perform_query(raw_connection, sql, binds, type_casted_binds, prepare:, notif
5151
result = if binds.nil? || binds.empty?
5252
ActiveSupport::Dependencies.interlock.permit_concurrent_loads do
5353
result = raw_connection.query(sql)
54-
@affected_rows_before_warnings = raw_connection.affected_rows
54+
# Ref: https://github.com/brianmario/mysql2/pull/1383
55+
# As of mysql2 0.5.6 `#affected_rows` might raise Mysql2::Error if a prepared statement
56+
# from that same connection was GCed while `#query` released the GVL.
57+
# By avoiding to call `#affected_rows` when we have a result, we reduce the likeliness
58+
# of hitting the bug.
59+
@affected_rows_before_warnings = result&.size || raw_connection.affected_rows
5560
result
5661
end
5762
result

0 commit comments

Comments
 (0)