Bug #76552 Cannot shutdown MySQL using JDBC driver
Submitted: 31 Mar 2015 23:47 Modified: 3 Apr 2015 16:53
Reporter: Davi Arnaut (OCA) Email Updates:
Status: Closed Impact on me:
None 
Category:MySQL Server: C API (client library) Severity:S1 (Critical)
Version:5.6,5.7,5.6.25, 5.7.8, 8.0.0 OS:Any
Assigned to: CPU Architecture:Any
Tags: jdbc, Malformed communication packet, regression, shutdown

[31 Mar 2015 23:47] Davi Arnaut
Description:
It's is no longer possible to shutdown the server using a JDBC connection.

JDBC sends a zero-length shutdown packet, but a _regression_ introduced in MySQL 5.6 rejects any shutdown packets with no arguments (zero-length):

1603   case COM_SHUTDOWN:
1604   {
1605     if (packet_length < 1)
1606     {
1607       my_error(ER_MALFORMED_PACKET, MYF(0));
1608       break;
1609     }
1610     status_var_increment(thd->status_var.com_other);
1611     if (check_global_access(thd,SHUTDOWN_ACL))
1612       break; /* purecov: inspected */
1613     /*
1614       If the client is < 4.1.3, it is going to send us no argument; then
1615       packet_length is 0, packet[0] is the end 0 of the packet. Note that
1616       SHUTDOWN_DEFAULT is 0. If client is >= 4.1.3, the shutdown level is in
1617       packet[0].
1618     */

As indicated by a comment only a few lines below the check, packet_length may be 0.

Regression introduced by https://github.com/mysql/mysql-server/commit/2292970bd9

Same commit also caused Bug#70664

How to repeat:
$ java -cp ".:/usr/share/java:/usr/share/java/mysql-connector-java.jar" JDBCShutdownTest
Shutdown failed: Unexpected exception during server shutdown.
java.sql.SQLException: Unexpected exception during server shutdown.
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:987)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:982)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5354)
	at JDBCShutdownTest.main(JDBCShutdownTest.java:13)
Caused by: java.sql.SQLException: Malformed communication packet.
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3597)
	at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3529)
	at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1990)
	at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5352)
	... 1 more

Suggested fix:
diff --git a/sql/sql_parse.cc b/sql/sql_parse.cc
index 0ba8bcb..ac37cd0 100644
--- a/sql/sql_parse.cc
+++ b/sql/sql_parse.cc
@@ -1602,11 +1602,6 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
 #ifndef EMBEDDED_LIBRARY
   case COM_SHUTDOWN:
   {
-    if (packet_length < 1)
-    {
-      my_error(ER_MALFORMED_PACKET, MYF(0));
-      break;
-    }
     status_var_increment(thd->status_var.com_other);
     if (check_global_access(thd,SHUTDOWN_ACL))
       break; /* purecov: inspected */
@@ -1617,7 +1612,7 @@ bool dispatch_command(enum enum_server_command command, THD *thd,
       packet[0].
     */
     enum mysql_enum_shutdown_level level;
-    if (!thd->is_valid_time())
+    if (!packet_length || !thd->is_valid_time())
       level= SHUTDOWN_DEFAULT;
     else
       level= (enum mysql_enum_shutdown_level) (uchar) packet[0];
[darnaut@darnaut-ld1 mysql-server]$
[31 Mar 2015 23:48] Davi Arnaut
Test case

Attachment: JDBCShutdownTest.java (application/octet-stream, text), 537 bytes.

[1 Apr 2015 7:04] MySQL Verification Team
Hello Davi Arnaut,

Thank you for the report, test case and supplying patch.
Observed this with 5.6.25, 5.7.8 and 5.8.0.

Thanks,
Umesh
[1 Apr 2015 7:07] MySQL Verification Team
// 5.6.25, C/j - 5.1.34 - affected

[umshastr@hod03]/export/umesh/mysql-5.6.25: java -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.34/mysql-connector-java-5.1.34-bin.jar' JDBCShutdownTest
Shutdown failed: Unexpected exception during server shutdown.
java.sql.SQLException: Unexpected exception during server shutdown.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:935)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:924)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:870)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5198)
        at JDBCShutdownTest.main(JDBCShutdownTest.java:13)
Caused by: java.sql.SQLException: Malformed communication packet.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:996)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3887)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3823)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5196)
        ... 1 more

// 5.6.25, C/j - 5.1.35  - affected

[umshastr@hod03]/export/umesh/mysql-5.6.25: javac -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest.java
[umshastr@hod03]/export/umesh/mysql-5.6.25: java -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest
Shutdown failed: Unexpected exception during server shutdown.
java.sql.SQLException: Unexpected exception during server shutdown.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5202)
        at JDBCShutdownTest.main(JDBCShutdownTest.java:13)
Caused by: java.sql.SQLException: Malformed communication packet.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5200)
        ... 1 more
        
// 5.7.8, C/j - 5.1.35  - affected

[umshastr@hod03]/export/umesh/mysql-5.7.8: javac -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest.java
[umshastr@hod03]/export/umesh/mysql-5.7.8: java -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest
Shutdown failed: Unexpected exception during server shutdown.
java.sql.SQLException: Unexpected exception during server shutdown.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5202)
        at JDBCShutdownTest.main(JDBCShutdownTest.java:13)
Caused by: java.sql.SQLException: Malformed communication packet.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5200)
        ... 1 more
        
        
// 5.8.0, C/j - 5.1.35  - affected

[umshastr@hod03]/export/umesh/mysql-5.8.0: javac -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest.java
[umshastr@hod03]/export/umesh/mysql-5.8.0: java -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest
Shutdown failed: Unexpected exception during server shutdown.
java.sql.SQLException: Unexpected exception during server shutdown.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:937)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:872)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5202)
        at JDBCShutdownTest.main(JDBCShutdownTest.java:13)
Caused by: java.sql.SQLException: Malformed communication packet.
        at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3835)
        at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3771)
        at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2435)
        at com.mysql.jdbc.ConnectionImpl.shutdownServer(ConnectionImpl.java:5200)
        ... 1 more

// 5.5.42  - not ffected

[umshastr@hod03]/export/umesh/mysql-5.5.42: bin/mysqld --basedir=/export/umesh/mysql-5.5.42 --datadir=/export/umesh/mysql-5.5.42/76424 --core-file --socket=/tmp/mysql_ushastry.sock  --port=15000 --log-error=/export/umesh/mysql-5.5.42/76424/log.err 2>&1 &
[1] 11574
[umshastr@hod03]/export/umesh/mysql-5.5.42:
[umshastr@hod03]/export/umesh/mysql-5.5.42:
[umshastr@hod03]/export/umesh/mysql-5.5.42: javac -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest.java
[umshastr@hod03]/export/umesh/mysql-5.5.42: java -classpath '.:/home/umshastr/bugs/mysql-connector-java-5.1.35/mysql-connector-java-5.1.35-bin.jar' JDBCShutdownTest                              [umshastr@hod03]/export/umesh/mysql-5.5.42:
[1]+  Done                    bin/mysqld --basedir=/export/umesh/mysql-5.5.42 --datadir=/export/umesh/mysql-5.5.42/76424 --core-file --socket=/tmp/mysql_ushastry.sock --port=15000 --log-error=/export/umesh/mysql-5.5.42/76424/log.err 2>&1
[umshastr@hod03]/export/umesh/mysql-5.5.42:
[umshastr@hod03]/export/umesh/mysql-5.5.42: ps aux|grep mysqld
umshastr 11679  0.0  0.0 112624   972 pts/0    S+   09:05   0:00 grep --color=auto mysqld
[umshastr@hod03]/export/umesh/mysql-5.5.42:
[3 Apr 2015 16:53] Paul DuBois
Noted in 5.6.25, 5.7.8, 5.8.0 changelogs.

The server rejected empty COM_SHUTDOWN packets.
[24 Jun 2015 4:01] Laurynas Biveinis
commit b7f79a44735ab6161e6b09e042fc2834cedd89ac
Author: Georgi Kodinov <georgi.kodinov@oracle.com>
Date:   Thu Apr 2 15:55:52 2015 +0300

    Bug #20810928: CANNOT SHUTDOWN MYSQL USING JDBC DRIVER
    
    Contribution from Davi Arnaud.
    Re-enabled the old, 0 length format of the COM_SHUTDOWN packet.
    Added a test case.
[18 Jun 2016 21:25] Omer Barnir
Posted by developer:
 
Reported version value updated to reflect release name change from 5.8 to 8.0