Skip to content

Commit 69c834c

Browse files
committed
Ensure MySQL is only initialized once
1 parent a19f4b8 commit 69c834c

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

dbdimp.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ typedef struct sql_type_info_s
5757
int is_num;
5858
} sql_type_info_t;
5959

60+
/*
61+
Ensure we only call mysql_library_init once, since that is not threadsafe.
62+
Not doing this before had lead to crashes in Apache MPM workers.
63+
*/
64+
pthread_once_t once_mysql_initialized = PTHREAD_ONCE_INIT;
65+
66+
static void init_mysql_library(void)
67+
{
68+
mysql_library_init(0, NULL, NULL);
69+
}
70+
71+
static void ensure_mysql_initialized()
72+
{
73+
pthread_once(&once_mysql_initialized, init_mysql_library);
74+
}
6075

6176
/*
6277
@@ -1206,7 +1221,7 @@ MYSQL *mysql_dr_connect(
12061221
#else
12071222
client_flag = CLIENT_FOUND_ROWS;
12081223
#endif
1209-
mysql_library_init(0, NULL, NULL);
1224+
ensure_mysql_initialized();
12101225
mysql_init(sock);
12111226

12121227
if (imp_dbh)

dbdimp.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
#include <mysql.h> /* Comes with MySQL-devel */
2323
#include <mysqld_error.h> /* Comes MySQL */
2424
#include <errmsg.h> /* Comes with MySQL-devel */
25-
25+
#include <pthread.h>
2626

2727
#define true 1
2828
#define false 0

0 commit comments

Comments
 (0)