Skip to content

Commit de48627

Browse files
Added call to mysql_library_init during initialization of the gem
This call must be performed before trying to call mysql_init from multiple threads Reference: http://dev.mysql.com/doc/refman/5.1/en/mysql-init.html Minimal reproduction of the problem if mysql_library_init is not called require 'mysql2' def connect Mysql2::Client.new() end threads = [0,1].map { Thread.new { connect } } threads.map(&:join) puts "OK!"
1 parent 78d59bc commit de48627

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

ext/mysql2/client.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1235,6 +1235,13 @@ void init_mysql2_client() {
12351235
}
12361236
}
12371237

1238+
/* Initializing mysql library, so different threads could call Client.new */
1239+
/* without race condition in the library */
1240+
if (mysql_library_init(0, NULL, NULL) != 0) {
1241+
rb_raise(rb_eRuntimeError, "Could not initialize MySQL client library");
1242+
return;
1243+
}
1244+
12381245
#if 0
12391246
mMysql2 = rb_define_module("Mysql2"); Teach RDoc about Mysql2 constant.
12401247
#endif

0 commit comments

Comments
 (0)