Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make prepared statement status thread and instance-specific
This fixes a race condition in system tests where prepared statements can be incorrectly parameterized when multiple threads observe the mutation of the @prepared_statements instance variable on the connection. Fixes #36763
- Loading branch information
Showing
3 changed files
with
61 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# frozen_string_literal: true | ||
|
||
require "cases/helper" | ||
require "models/course" | ||
require "models/entrant" | ||
|
||
module ActiveRecord | ||
class PreparedStatementStatusTest < ActiveRecord::TestCase | ||
def test_prepared_statement_status_is_thread_and_instance_specific | ||
course_conn = Course.connection | ||
entrant_conn = Entrant.connection | ||
|
||
inside = Concurrent::Event.new | ||
preventing = Concurrent::Event.new | ||
finished = Concurrent::Event.new | ||
|
||
assert_not_same course_conn, entrant_conn | ||
|
||
if current_adapter?(:Mysql2Adapter) | ||
# The mysql adapter does not use prepared | ||
# statements by default. | ||
assert_not course_conn.prepared_statements | ||
assert_not entrant_conn.prepared_statements | ||
else | ||
t1 = Thread.new do | ||
course_conn.unprepared_statement do | ||
inside.set | ||
preventing.wait | ||
assert_not course_conn.prepared_statements | ||
assert entrant_conn.prepared_statements | ||
finished.set | ||
end | ||
end | ||
|
||
t2 = Thread.new do | ||
entrant_conn.unprepared_statement do | ||
inside.wait | ||
assert course_conn.prepared_statements | ||
assert_not entrant_conn.prepared_statements | ||
preventing.set | ||
finished.wait | ||
end | ||
end | ||
|
||
t1.join | ||
t2.join | ||
end | ||
end | ||
end | ||
end |