The "reconnect" bug. If MYSQL::reconnect flag is set reconnect does not happen on mysql_stmt_execute_immediate() call. There are several failed test connected with this bug. For example: main.kill, main.wait_timeout. If MYSQL::reconnect flag is set and mysql_stmt_execute_immediate() is invoked the reconnection should be inside of cli_advanced_command() after net_write_command() returns error. But net_write_command() does not return error as send() function does not return error in inline_mysql_socket_send() too in the case if connection was closed by peer. As the data size to send is quite small send() just buffers the data. Here is the stacktrace: (gdb) bt #0 __libc_send (fd=7, buf=0x9f9ae0, n=13, flags=0) at ../sysdeps/unix/sysv/linux/x86_64/send.c:26 #1 0x0000000000438c6c in inline_mysql_socket_send ( src_file=0x4c9e10 "./vio/viosocket.c", src_line=224, mysql_socket=..., buf=0x9f9ae0, n=13, flags=0) at ./include/mysql/psi/mysql_socket.h:759 #2 0x0000000000439597 in vio_write (vio=0x9f58d0, buf=0x9f9ae0 "\t", size=13) at ./vio/viosocket.c:224 #3 0x000000000041ea38 in net_write_raw_loop (net=0x7fffffff98c8, buf=0x9f9ae0 "\t", count=13) at ./sql/net_serv.cc:500 #4 0x000000000041ed0d in net_write_packet (net=0x7fffffff98c8, packet=0x9f9ae0 "\t", length=13) at ./sql/net_serv.cc:636 #5 0x000000000041e31f in net_flush (net=0x7fffffff98c8) at ./sql/net_serv.cc:220 #6 0x000000000041e7d9 in net_write_command (net=0x7fffffff98c8, command=31 '\037', header=0x0, head_len=0, packet=0x4c3eaf "SELECT 1", ---Type to continue, or q to quit--- len=8) at ./sql/net_serv.cc:399 #7 0x0000000000413a08 in cli_advanced_command (mysql=0x7fffffff98c8, command=COM_STMT_EXECUTE_IMMEDIATE, header=0x0, header_length=0, arg=0x4c3eaf "SELECT 1", arg_length=8, skip_check=1 '\001', stmt=0x9fbb80) at ./sql-common/client.c:727 #8 0x000000000040914d in cli_stmt_execute_immediate (stmt=0x9fbb80, query=0x4c3eaf "SELECT 1", len=8) at ./libmysql/libmysql.c:2224 #9 0x0000000000409ba4 in mysql_stmt_execute_immediate (stmt=0x9fbb80, q=0x4c3eaf "SELECT 1") at ./libmysql/libmysql.c:2716 #10 0x0000000000404cfd in main (argc=2, argv=0x7fffffffa038) at ./tests/stmt_execute_immediate.cc:276 And the fact the connection was closed by peer is determined during the step of receiving data from server in cli_read_query_result(). But in this case reconnection does not happen. Why does the reconnection happen if mysql_stmt_prepare() is used? Because send() is invoked twice in this case. The first time is for COM_STMT_CLEAR command and the second time is for COM_STMT_PREPARE. The first send() buffers the data and does not return error while the second call returns error. Here are the stacktraces: (gdb) bt #0 __libc_send (fd=7, buf=0x9f9ae0, n=9, flags=0) at ../sysdeps/unix/sysv/linux/x86_64/send.c:26 #1 0x0000000000438c99 in inline_mysql_socket_send ( src_file=0x4c9e30 "./vio/viosocket.c", src_line=224, mysql_socket=..., buf=0x9f9ae0, n=9, flags=0) at ./include/mysql/psi/mysql_socket.h:759 #2 0x00000000004395c4 in vio_write (vio=0x9f58d0, buf=0x9f9ae0 "\005", size=9) at ./vio/viosocket.c:224 #3 0x000000000041ea65 in net_write_raw_loop (net=0x7fffffff98c8, buf=0x9f9ae0 "\005", count=9) at ./sql/net_serv.cc:500 #4 0x000000000041ed3a in net_write_packet (net=0x7fffffff98c8, packet=0x9f9ae0 "\005", length=9) at ./sql/net_serv.cc:636 #5 0x000000000041e34c in net_flush (net=0x7fffffff98c8) at ./sql/net_serv.cc:220 #6 0x000000000041e806 in net_write_command (net=0x7fffffff98c8, command=25 '\031', header=0x0, head_len=0, packet=0x7fffffff9120 "", len=4) ---Type to continue, or q to quit--- at ./sql/net_serv.cc:399 #7 0x0000000000413a35 in cli_advanced_command (mysql=0x7fffffff98c8, command=COM_STMT_CLOSE, header=0x0, header_length=0, arg=0x7fffffff9120 "", arg_length=4, skip_check=1 '\001', stmt=0x9fbb80) at ./sql-common/client.c:727 #8 0x0000000000407b45 in mysql_stmt_prepare (stmt=0x9fbb80, query=0x4c3ecf "select 1", length=8) at ./libmysql/libmysql.c:1621 #9 0x0000000000404d02 in main (argc=2, argv=0x7fffffffa038) at ./tests/stmt_execute_immediate.cc:284 (gdb) bt #0 __libc_send (fd=7, buf=0x9f9ae0, n=13, flags=0) at ../sysdeps/unix/sysv/linux/x86_64/send.c:26 #1 0x0000000000438c99 in inline_mysql_socket_send ( src_file=0x4c9e30 "./vio/viosocket.c", src_line=224, mysql_socket=..., buf=0x9f9ae0, n=13, flags=0) at ./include/mysql/psi/mysql_socket.h:759 #2 0x00000000004395c4 in vio_write (vio=0x9f58d0, buf=0x9f9ae0 "\t", size=13) at ./vio/viosocket.c:224 #3 0x000000000041ea65 in net_write_raw_loop (net=0x7fffffff98c8, buf=0x9f9ae0 "\t", count=13) at ./sql/net_serv.cc:500 #4 0x000000000041ed3a in net_write_packet (net=0x7fffffff98c8, packet=0x9f9ae0 "\t", length=13) at ./sql/net_serv.cc:636 #5 0x000000000041e34c in net_flush (net=0x7fffffff98c8) at ./sql/net_serv.cc:220 #6 0x000000000041e806 in net_write_command (net=0x7fffffff98c8, command=22 '\026', header=0x0, head_len=0, packet=0x4c3ecf "select 1", ---Type to continue, or q to quit--- len=8) at ./sql/net_serv.cc:399 #7 0x0000000000413a35 in cli_advanced_command (mysql=0x7fffffff98c8, command=COM_STMT_PREPARE, header=0x0, header_length=0, arg=0x4c3ecf "select 1", arg_length=8, skip_check=1 '\001', stmt=0x9fbb80) at ./sql-common/client.c:727 #8 0x0000000000407bb9 in mysql_stmt_prepare (stmt=0x9fbb80, query=0x4c3ecf "select 1", length=8) at ./libmysql/libmysql.c:1628 #9 0x0000000000404d02 in main (argc=2, argv=0x7fffffffa038) at ./tests/stmt_execute_immediate.cc:284