Skip to content

Commit 30ba310

Browse files
committed
cleanup: thd_destructor_proxy
1. wait for thd_destructor_proxy thread to start after creating it. this ensures that the thread is ready to receive a shutdown signal whenever we want to send it. 2. join it at shutdown, this guarantees that no innodb THD will exist after innobase_end(). this fixes crashes and memory leaks in main.mysqld_option_err (were innodb was started and then immediately shut down).
1 parent 2de0e42 commit 30ba310

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

storage/innobase/handler/ha_innodb.cc

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -313,26 +313,28 @@ This THDs must be destroyed rather early in the server shutdown sequence.
313313
This service thread creates a THD and idly waits for it to get a signal to
314314
die. Then it notifies all purge workers to shutdown.
315315
*/
316-
st_my_thread_var *thd_destructor_myvar= NULL;
317-
mysql_cond_t thd_destructor_cond;
318-
pthread_t thd_destructor_thread;
316+
static volatile st_my_thread_var *thd_destructor_myvar= NULL;
317+
static pthread_t thd_destructor_thread;
319318

320319
pthread_handler_t
321320
thd_destructor_proxy(void *)
322321
{
323322
mysql_mutex_t thd_destructor_mutex;
323+
mysql_cond_t thd_destructor_cond;
324324

325325
my_thread_init();
326326
mysql_mutex_init(PSI_NOT_INSTRUMENTED, &thd_destructor_mutex, 0);
327327
mysql_cond_init(PSI_NOT_INSTRUMENTED, &thd_destructor_cond, 0);
328328

329-
thd_destructor_myvar = _my_thread_var();
329+
st_my_thread_var *myvar= _my_thread_var();
330330
THD *thd= create_thd();
331331
thd_proc_info(thd, "InnoDB background thread");
332332

333+
myvar->current_mutex = &thd_destructor_mutex;
334+
myvar->current_cond = &thd_destructor_cond;
335+
333336
mysql_mutex_lock(&thd_destructor_mutex);
334-
thd_destructor_myvar->current_mutex = &thd_destructor_mutex;
335-
thd_destructor_myvar->current_cond = &thd_destructor_cond;
337+
thd_destructor_myvar = myvar;
336338
/* wait until the server wakes the THD to abort and die */
337339
while (!thd_destructor_myvar->abort)
338340
mysql_cond_wait(&thd_destructor_cond, &thd_destructor_mutex);
@@ -4487,6 +4489,8 @@ innobase_init(
44874489
mysql_thread_create(thd_destructor_thread_key,
44884490
&thd_destructor_thread,
44894491
NULL, thd_destructor_proxy, NULL);
4492+
while (!thd_destructor_myvar)
4493+
os_thread_sleep(20);
44904494
}
44914495

44924496
/* Adjust the innodb_undo_logs config object */
@@ -4603,6 +4607,7 @@ innobase_end(
46034607
}
46044608

46054609
innobase_space_shutdown();
4610+
pthread_join(thd_destructor_thread, NULL);
46064611

46074612
mysql_mutex_destroy(&innobase_share_mutex);
46084613
mysql_mutex_destroy(&commit_cond_m);

0 commit comments

Comments
 (0)