Skip to content

Commit

Permalink
MDEV-16825 SIGSEGV after SIGINT in bootstrap mode
Browse files Browse the repository at this point in the history
* Fixes segmentation fault on SIGINT;
* Interrupt blocked fgets() while daemon is in the shutting down progress.

[fixes #527]
  • Loading branch information
midenok authored and FooBarrior committed Jul 30, 2018
1 parent 89e63a5 commit 13c3005
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 3 deletions.
4 changes: 4 additions & 0 deletions mysql-test/main/bootstrap.result
Expand Up @@ -26,3 +26,7 @@ select * from mysql.plugin;
name dl
EXAMPLE ha_example.so
truncate table mysql.plugin;
#
# MDEV-16825 SIGSEGV after SIGINT in bootstrap mode
#
Success!
19 changes: 19 additions & 0 deletions mysql-test/main/bootstrap.test
Expand Up @@ -106,3 +106,22 @@ use test;
EOF
--exec $MYSQLD_BOOTSTRAP_CMD --ignore-db-dirs='some_dir' --ignore-db-dirs='some_dir' < $MYSQLTEST_VARDIR/tmp/bootstrap_9969.sql >> $MYSQLTEST_VARDIR/tmp/bootstrap.log 2>&1
--remove_file $MYSQLTEST_VARDIR/tmp/bootstrap_9969.sql

--echo #
--echo # MDEV-16825 SIGSEGV after SIGINT in bootstrap mode
--echo #
--perl
use POSIX qw(WNOHANG);
my $wait= 5; # seconds to wait process shutdown
$|= 1;
my $pid= open(BOOTSTRAP, '|-', $ENV{'MYSQLD_BOOTSTRAP_CMD'})
or die $ENV{'MYSQLD_BOOTSTRAP_CMD'}. ": $!";
print BOOTSTRAP "select 1;\n"; # make sure process accepts input
kill INT => $pid;
my $status;
while ($wait-- && ($status= waitpid($pid, WNOHANG)) < 1)
{
sleep 1;
}
print $status != $pid ? "Failed!\n" : "Success!\n";
EOF
2 changes: 2 additions & 0 deletions scripts/comp_sql.c
Expand Up @@ -40,6 +40,8 @@

FILE *in, *out;

volatile sig_atomic_t kill_in_progress= 0;

static void die(const char *fmt, ...)
{
va_list args;
Expand Down
11 changes: 9 additions & 2 deletions sql/mysqld.cc
Expand Up @@ -784,12 +784,14 @@ mysql_rwlock_t LOCK_grant, LOCK_sys_init_connect, LOCK_sys_init_slave;
mysql_prlock_t LOCK_system_variables_hash;
mysql_cond_t COND_thread_count, COND_start_thread;
pthread_t signal_thread;
pthread_t *bootstrap_thread= NULL;
pthread_attr_t connection_attrib;
mysql_mutex_t LOCK_server_started;
mysql_cond_t COND_server_started;

int mysqld_server_started=0, mysqld_server_initialized= 0;
File_parser_dummy_hook file_parser_dummy_hook;
volatile sig_atomic_t kill_in_progress= 0;

/* replication parameters, if master_host is not NULL, we are a slave */
uint report_port= 0;
Expand All @@ -801,7 +803,6 @@ char *opt_logname, *opt_slow_logname, *opt_bin_logname;

/* Static variables */

static volatile sig_atomic_t kill_in_progress;
my_bool opt_stack_trace;
my_bool opt_expect_abort= 0, opt_bootstrap= 0;
static my_bool opt_myisam_log;
Expand Down Expand Up @@ -1651,6 +1652,10 @@ static void close_connections(void)
#endif
close_server_sock();
}
#ifndef DONT_USE_THR_ALARM
if (bootstrap_thread)
pthread_kill(*bootstrap_thread, thr_client_alarm);
#endif
mysql_mutex_unlock(&LOCK_start_thread);
#endif /* __WIN__ */

Expand Down Expand Up @@ -1761,7 +1766,8 @@ static void close_connections(void)
mysql_mutex_unlock(&LOCK_thread_count); // For unlink from list

Events::deinit();
slave_prepare_for_shutdown();
if (!opt_bootstrap)
slave_prepare_for_shutdown();
mysql_bin_log.stop_background_thread();
ack_receiver.stop();

Expand Down Expand Up @@ -6468,6 +6474,7 @@ static void bootstrap(MYSQL_FILE *file)
delete thd;
DBUG_VOID_RETURN;
}
bootstrap_thread= &thd->real_id;
/* Wait for thread to die */
mysql_mutex_lock(&LOCK_thread_count);
while (in_bootstrap)
Expand Down
2 changes: 2 additions & 0 deletions sql/mysqld.h
Expand Up @@ -29,6 +29,7 @@
#include <my_rnd.h>
#include "my_pthread.h"
#include "my_rdtsc.h"
#include <signal.h>

class THD;
class CONNECT;
Expand Down Expand Up @@ -178,6 +179,7 @@ extern char *opt_backup_history_logname, *opt_backup_progress_logname,
*opt_backup_settings_name;
extern const char *log_output_str;
extern const char *log_backup_output_str;
extern volatile sig_atomic_t kill_in_progress;

/* System Versioning begin */
enum vers_system_time_t
Expand Down
5 changes: 4 additions & 1 deletion sql/sql_bootstrap.cc
Expand Up @@ -17,8 +17,11 @@
#include "mariadb.h"
#include <ctype.h>
#include <string.h>
#include <signal.h>
#include "sql_bootstrap.h"

extern volatile sig_atomic_t kill_in_progress;

int read_bootstrap_query(char *query, int *query_length,
fgets_input_t input, fgets_fn_t fgets_fn, int *error)
{
Expand All @@ -37,7 +40,7 @@ int read_bootstrap_query(char *query, int *query_length,
*error= fgets_error;

if (fgets_error != 0)
return READ_BOOTSTRAP_ERROR;
return kill_in_progress ? READ_BOOTSTRAP_EOF : READ_BOOTSTRAP_ERROR;

if (line == NULL)
return (query_len == 0) ? READ_BOOTSTRAP_EOF : READ_BOOTSTRAP_ERROR;
Expand Down

0 comments on commit 13c3005

Please sign in to comment.