Skip to content

With Thread#exit, client object becomes incorrect state #1392

@tomykaira

Description

@tomykaira

From: rails/rails#54441

Steps to reproduce

  • Spawn threads, reusing client.
  • Call client#query in the threads.
  • Kill the threads by Thread#exit.

In this scenario, C-level active_fiber is not cleared, but ruby-level client is still alive.
Therefore the next call of query throws 'Mysql2::Client#_query': This connection is in use by: #<Fiber:0x00007fef3d80f2a8 (terminated)> exception.

require "bundler/inline"

gemfile(true) do
  source "https://rubygems.org"
  gem "bigdecimal"
  gem "mysql2"
end

client = Mysql2::Client.new(:host => "127.0.0.1", :username => "root", :password => "password", :database => "test_datab
ase")

100.times do
  th = Thread.new do
    loop do
      results = client.query("SELECT * FROM posts")
    end
  end
  sleep(0.1)
  th.exit
end

I found this issue on Rails.
ActiveRecord reuses client objects in a connection pool.
My problem was the broken clients were kept in the pool and following requests failed.
I fixed the problem by avoiding Thread#exit, but I apprecated if Mysql2 soundly signaled the connection was broken and AR needed to start a new connection.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions