Skip to content

Commit 2aaa812

Browse files
aybekaibek.bukabayev aibek.bukabayev@percona.com
authored and
aibek.bukabayev [email protected]
committed
PS-9614 Pool-of-Threads' timer thread fails to start if mysqld is started with --daemonize
Problem: When pool-of-threads is enabled, a timer_thread should be created. However, if the process starts with --daemonize, the thread is missing. This occurs because during the execution of mysqld_daemonize(), the mysqld process forks, creating a child process that runs as a daemon. Since threads are not copied during a fork, and timer_thread is not reinitialized in the daemon process, it is lost. Solution: Move the timer_thread initialization to execute after the mysqld_daemonize() function.
1 parent f01c613 commit 2aaa812

File tree

7 files changed

+39
-4
lines changed

7 files changed

+39
-4
lines changed

sql/conn_handler/connection_handler.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,12 @@ class Connection_handler {
5959
by this connection handler.
6060
*/
6161
virtual uint get_max_threads() const = 0;
62+
63+
/**
64+
Performs required initializations after the daemonization of the mysqld
65+
process.
66+
*/
67+
virtual void post_daemonize_init() {}
6268
};
6369

6470
#endif // CONNECTION_HANDLER_INCLUDED

sql/conn_handler/connection_handler_impl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,8 @@ class Thread_pool_connection_handler : public Connection_handler {
134134
bool add_connection(Channel_info *channel_info) override;
135135

136136
uint get_max_threads() const override { return threadpool_max_threads; }
137+
138+
void post_daemonize_init() override { start_timer_thread(); }
137139
};
138140

139141
#endif // CONNECTION_HANDLER_IMPL_INCLUDED

sql/conn_handler/connection_handler_manager.cc

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,10 @@ void Connection_handler_manager::wait_till_no_connection() {
229229
mysql_mutex_unlock(&LOCK_connection_count);
230230
}
231231

232+
void Connection_handler_manager::post_daemonize_init() {
233+
m_connection_handler->post_daemonize_init();
234+
}
235+
232236
void Connection_handler_manager::destroy_instance() {
233237
Per_thread_connection_handler::destroy();
234238

sql/conn_handler/connection_handler_manager.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,5 +234,11 @@ class Connection_handler_manager {
234234
wat till connection_count to become zero.
235235
*/
236236
static void wait_till_no_connection();
237+
238+
/**
239+
Performs required initializations after the daemonization of the mysqld
240+
process.
241+
*/
242+
void post_daemonize_init();
237243
};
238244
#endif // CONNECTION_HANDLER_MANAGER_INCLUDED.

sql/mysqld.cc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8158,6 +8158,14 @@ int mysqld_main(int argc, char **argv)
81588158
}
81598159
#endif
81608160

8161+
if (Connection_handler_manager::thread_handling ==
8162+
Connection_handler_manager::SCHEDULER_THREAD_POOL) {
8163+
// If --thread-handling=pool-of-threads is specified,
8164+
// the timer thread should start only after the daemon process has begun,
8165+
// provided that --daemonize was also specified.
8166+
Connection_handler_manager::get_instance()->post_daemonize_init();
8167+
}
8168+
81618169
#ifndef _WIN32
81628170
user_info = check_user(mysqld_user);
81638171
if (!user_info.IsVoid()) {

sql/threadpool.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,4 +80,7 @@ extern void tp_set_max_threads(uint val);
8080
extern void tp_set_threadpool_size(uint val) noexcept;
8181
extern void tp_set_threadpool_stall_limit(uint val) noexcept;
8282

83+
/* Function to start the timer thread */
84+
extern void start_timer_thread() noexcept;
85+
8386
#endif /* THREADPOOL_INCLUDED */

sql/threadpool_unix.cc

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,8 @@ const char *threadpool_high_prio_mode_names[] = {"transactions", "statements",
5959

6060
/** Indicates that threadpool was initialized*/
6161
static bool threadpool_started = false;
62+
/** Indicates that timer thread was started*/
63+
static bool timer_thread_started = false;
6264

6365
/*
6466
Define PSI Keys for performance schema.
@@ -602,17 +604,24 @@ static void check_stall(thread_group_t *thread_group) {
602604
static void start_timer(pool_timer_t *timer) noexcept {
603605
my_thread_handle thread_id;
604606
DBUG_ENTER("start_timer");
607+
timer_thread_started = true;
605608
mysql_mutex_init(key_timer_mutex, &timer->mutex, NULL);
606609
mysql_cond_init(key_timer_cond, &timer->cond);
607610
timer->shutdown = false;
608611
mysql_thread_create(key_timer_thread, &thread_id, NULL, timer_thread, timer);
609612
DBUG_VOID_RETURN;
610613
}
611614

615+
void start_timer_thread() noexcept {
616+
pool_timer.tick_interval = threadpool_stall_limit;
617+
start_timer(&pool_timer);
618+
}
619+
612620
static void stop_timer(pool_timer_t *timer) noexcept {
613621
DBUG_ENTER("stop_timer");
614622
mysql_mutex_lock(&timer->mutex);
615623
timer->shutdown = true;
624+
timer_thread_started = false;
616625
mysql_cond_signal(&timer->cond);
617626
mysql_mutex_unlock(&timer->mutex);
618627
DBUG_VOID_RETURN;
@@ -1488,17 +1497,14 @@ bool tp_init() {
14881497
mysql_thread_register("threadpool", thread_list, array_elements(thread_list));
14891498
#endif
14901499

1491-
pool_timer.tick_interval = threadpool_stall_limit;
1492-
start_timer(&pool_timer);
14931500
DBUG_RETURN(false);
14941501
}
14951502

14961503
void tp_end() noexcept {
14971504
DBUG_ENTER("tp_end");
14981505

14991506
if (!threadpool_started) DBUG_VOID_RETURN;
1500-
1501-
stop_timer(&pool_timer);
1507+
if (timer_thread_started) stop_timer(&pool_timer);
15021508
for (uint i = 0; i < array_elements(all_groups); i++) {
15031509
thread_group_close(&all_groups[i]);
15041510
}

0 commit comments

Comments
 (0)