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

Ensure MySQL is only initialized once #465

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Makefile.PL
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,16 @@ if ($^O eq 'VMS') {
}
$cflags .= " -DDBD_MYSQL_NO_CLIENT_FOUND_ROWS" if $opt->{'nofoundrows'};
$cflags .= " -g ";

unless ($^O eq 'MSWin32') {
if ($Config{usethreads} || $Config{osname} =~ /darwin|linux/i || $Config{libs} =~ /-l?pthread\b/ || $Config{ldflags} =~ /-l?pthread\b/) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure this condition is correct, as it will assume that any mac or linux install has pthreads which is not true?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Though I see that is also done in the condition I linked to, seems strange.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's why I did it that way. I can imagine that OSX systems generally have pthreads and I suppose the thinking for Linux is that pretty much any system not older than 2 decades will have pthreads there too.

$cflags .= " -DDBD_MYSQL_HAS_PTHREADS";
} else {
print "*** This module may crash or deadlock on startup in multi-threaded scenarios ***\n",
" To avoid this, make sure your Perl is compiled with pthreads.\n\n",
}
}

my %o = ( 'NAME' => 'DBD::mysql',
'INC' => $cflags,
'dist'=> { 'SUFFIX' => ".gz",
Expand Down
56 changes: 55 additions & 1 deletion dbdimp.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,60 @@ typedef struct sql_type_info_s
int is_num;
} sql_type_info_t;

/*
Ensure we only call mysql_library_init once, since that is not threadsafe.
Not doing this before had lead to crashes and deadlocks in Apache MPM workers.
*/

static void init_mysql_library(void)
{
mysql_library_init(0, NULL, NULL);
}

#ifdef DBD_MYSQL_HAS_PTHREADS

#include <pthread.h>
static pthread_once_t once_mysql_initialized = PTHREAD_ONCE_INIT;

static void ensure_mysql_initialized(void)
{
pthread_once(&once_mysql_initialized, init_mysql_library);
}

#elif defined(_WIN32)

#define WIN32_LEAN_AND_MEAN
#include <Windows.h>
static INIT_ONCE once_mysql_initialized = INIT_ONCE_STATIC_INIT;

static BOOL init_mysql_library_thunk(PINIT_ONCE once, PVOID param, PVOID *context)
{
init_mysql_library();
return TRUE;
}

static void ensure_mysql_initialized(void)
{
InitOnceExecuteOnce(&once_mysql_initialized, init_mysql_library_thunk, NULL, NULL);
}

#else
static int is_mysql_initialized = 0;

static void ensure_mysql_initialized(void)
{
/* Thread-unsafe fallback
* In practice, this will only be used for perls not compiled with threads
* anyway, so it's very unlikely that this would be a problem.
*/
if (!is_mysql_initialized)
{
is_mysql_initialized = 1;
mysql_library_init(0, NULL, NULL);
}
}

#endif

/*

Expand Down Expand Up @@ -1206,7 +1260,7 @@ MYSQL *mysql_dr_connect(
#else
client_flag = CLIENT_FOUND_ROWS;
#endif
mysql_library_init(0, NULL, NULL);
ensure_mysql_initialized();
mysql_init(sock);

if (imp_dbh)
Expand Down
1 change: 0 additions & 1 deletion dbdimp.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
#include <mysqld_error.h> /* Comes MySQL */
#include <errmsg.h> /* Comes with MySQL-devel */


#define true 1
#define false 0

Expand Down