The MySQL Connector (Python Driver) seems to be leaking file descriptors during forks.
This is what I have noticed today: if a process opens a MySQL connection and then forks, the child process not just inherits the open connection, but also the transaction state. The current transaction becomes shared between the child and the parent. That is, if the child process rolls back, the parent also gets a roll back.
Also, as it is the same transaction, a lock set by one process has no effect on another.
Here is a proof of concept:
"""
Create and populate a database before running this script:
create database mytest;
grant all on mytest.* to ''@'localhost';
flush privileges;
create table foo(a int);
insert into foo (a) values (0);
"""
import time
from multiprocessing import Process
import _mysql
= False # change to true to make the child process block (it should)
reconnect
= _mysql.connect("localhost", user="mike", db="mytest", passwd="")
conn
def sub():
if reconnect:
= _mysql.connect("localhost", user="mike", db="mytest", passwd="")
sub_conn else:
= conn
sub_conn print "SUB: start", sub_conn.thread_id()
print "SUB: do this to get the number of connections -> sudo lsof | grep mysql.sock"
'begin')
sub_conn.query('select * from foo for update')
sub_conn.query(if not reconnect:
print "SUB: NOT BLOCKED, sleeping for 30 sec to hold the conneciton open"
30)
time.sleep(print "SUB: result", sub_conn.use_result().fetch_row()
'rollback')
sub_conn.query(print "SUB: end"
print "HOST: start", conn.thread_id()
'begin')
conn.query('select * from foo for update')
conn.query(print "HOST: result", conn.use_result().fetch_row()
= Process(target=sub)
process print "HOST: start sub"
process.start()
process.join()print "HOST: sub joined"
'rollback')
conn.query(print "HOST: end"
When reconnect is set to False, the parent’s thread id will be the same as in the child. The reason why is that MySQL uses server-side thread ids as connection identifiers. Here’s the mysql_thread_id
function (mysql-connector-c-6.1.0-src/libmysql/libmysql.c:1070):
(MYSQL *mysql)
ulong STDCALL mysql_thread_id{
......
return (mysql)->thread_id;
}
And this is how it is set in CLI_MYSQL_REAL_CONNECT
(mysql-connector-c-6.1.0-src/sql-common/client.c:3613):
......
= end= strend((char*) net->read_pos+1);
server_version_end->thread_id=uint4korr(end+1);
mysql+=5;
end......
The direct consequence is that children processes, created for example using the multiprocessing module, must close the inherited MySQL connections and then reopen them to avoid surprises.
When I discovered it, I immediately thought about Django management commands splitting workload between children.
Open questions: