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

"Commands out of sync" exceptions with threads and prepared statements. #1338

Open
Math2 opened this issue Nov 22, 2023 · 2 comments
Open

"Commands out of sync" exceptions with threads and prepared statements. #1338

Math2 opened this issue Nov 22, 2023 · 2 comments

Comments

@Math2
Copy link

Math2 commented Nov 22, 2023

The problem was happening with a connection pool shared by many threads, but I found a simple way to reproduce it:

conf = { ... }

clients = 10.times.map { Mysql2::Client.new(**conf) }

loop do
  clients.map do |client|
    Thread.new { client.prepare("SELECT 123").execute }
  end.each(&:join)
end

This immediately fails with "Commands out of sync; you can't run this command now (Mysql2::Error)" here (after 50 or so iterations of the outer loop).

With #query it works fine. It only fails with prepared statements.

@Math2
Copy link
Author

Math2 commented Nov 22, 2023

Oops, actually if you #close the statements, this works fine.

I believe I'm seeing a problem with threads and prepared statements that happens without prepared statements accumulating endlessly like this, but this isn't a way to reproduce it...

@Math2
Copy link
Author

Math2 commented Nov 22, 2023

Very curious. This one fails with "Commands out of sync" exceptions while reusing the same prepared statements, but apparently only if the query returns 0 rows!

conf = { ... }

clients = 10.times.map { Mysql2::Client.new(**conf) }
stmts = clients.map { |client| client.prepare("SELECT 123 WHERE FALSE") }

loop do
  stmts.map do |stmt|
    Thread.new { stmt.execute }
  end.each(&:join)
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant