Skip to content

Commit 9758d21

Browse files
authored
Decode unsolicited ERR packets in MySQLDecoder (#1787) (#1788)
Fixes #1786 When the server closes an idle connection after `wait_timeout`, MySQL first sends an ERR packet (4031, `ER_CLIENT_INTERACTION_TIMEOUT`). With no command in flight, `MySQLDecoder` threw `IllegalStateException("No command codec for packet")` and the server's reason was lost. `MySQLDecoder` now decodes an unsolicited ERR packet and fails with a `MySQLException`. Other unexpected packets still throw `IllegalStateException`. `MySQLCommand#decodeErrorPacketPayload` and `MySQLCommand#readRestOfPacketString` are now `static` so the decoder can use them. `MySQLPoolTest#testBorrowedPooledConnectionClosedByServer` now asserts that the connection `exceptionHandler` receives a `MySQLException` with error code 4031. The assertion is skipped on MariaDB, which closes the socket without sending an ERR packet. Assisted-by: IBM Bob (cherry picked from commit b9c4b4b) Signed-off-by: Thomas Segismont <tsegismont@gmail.com>
1 parent a846bdf commit 9758d21

3 files changed

Lines changed: 18 additions & 2 deletions

File tree

‎vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/MySQLCommand.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ void handleErrorPacketPayload(ByteBuf payload) {
170170
encoder.fireCommandResponse(CommandResponse.failure(mySQLException));
171171
}
172172

173-
final MySQLException decodeErrorPacketPayload(ByteBuf payload) {
173+
static MySQLException decodeErrorPacketPayload(ByteBuf payload) {
174174
payload.skipBytes(1); // skip ERR packet header
175175
int errorCode = payload.readUnsignedShortLE();
176176
// CLIENT_PROTOCOL_41 capability flag will always be set
@@ -196,7 +196,7 @@ EofPacket decodeEofPacketPayload(ByteBuf payload) {
196196
return new EofPacket(numberOfWarnings, serverStatusFlags);
197197
}
198198

199-
String readRestOfPacketString(ByteBuf payload, Charset charset) {
199+
static String readRestOfPacketString(ByteBuf payload, Charset charset) {
200200
return BufferUtils.readFixedLengthString(payload, payload.readableBytes(), charset);
201201
}
202202

‎vertx-mysql-client/src/main/java/io/vertx/mysqlclient/impl/codec/MySQLDecoder.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
import io.netty.channel.ChannelInboundHandlerAdapter;
1818
import io.vertx.sqlclient.codec.CommandResponse;
1919

20+
import static io.vertx.mysqlclient.impl.protocol.Packets.ERROR_PACKET_HEADER;
2021
import static io.vertx.mysqlclient.impl.protocol.Packets.PACKET_PAYLOAD_LENGTH_LIMIT;
2122

2223
class MySQLDecoder extends ChannelInboundHandlerAdapter {
@@ -73,6 +74,11 @@ private void decodePackets() {
7374
codec.checkFireAndForgetCommands();
7475
MySQLCommand<?, ?> ctx = codec.peek();
7576
if (ctx == null) {
77+
// The server can send an unsolicited ERR packet before closing the connection,
78+
// e.g. ER_CLIENT_INTERACTION_TIMEOUT (4031) when wait_timeout expires
79+
if (payload.isReadable() && payload.getUnsignedByte(payload.readerIndex()) == ERROR_PACKET_HEADER) {
80+
throw MySQLCommand.decodeErrorPacketPayload(payload);
81+
}
7682
throw new IllegalStateException("No command codec for packet");
7783
}
7884
ctx.sequenceId = sequenceId + 1;

‎vertx-mysql-client/src/test/java/io/vertx/tests/mysqlclient/MySQLPoolTest.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import io.vertx.mysqlclient.MySQLBuilder;
2323
import io.vertx.mysqlclient.MySQLConnectOptions;
2424
import io.vertx.mysqlclient.MySQLConnection;
25+
import io.vertx.mysqlclient.MySQLException;
2526
import io.vertx.sqlclient.Pool;
2627
import io.vertx.sqlclient.PoolOptions;
2728
import io.vertx.sqlclient.Row;
@@ -143,6 +144,15 @@ public void testBorrowedPooledConnectionClosedByServer(TestContext ctx) {
143144
pool
144145
.getConnection()
145146
.onComplete(ctx.asyncAssertSuccess(conn -> {
147+
// MySQL sends an ERR packet before closing the connection, MariaDB closes it silently
148+
if (!MySQLTestBase.rule.isUsingMariaDB()) {
149+
Async exception = ctx.async();
150+
conn.exceptionHandler(failure -> {
151+
ctx.assertTrue(failure instanceof MySQLException);
152+
ctx.assertEquals(4031, ((MySQLException) failure).getErrorCode());
153+
exception.complete();
154+
});
155+
}
146156
conn
147157
.query("SET SESSION wait_timeout=3;")
148158
.execute()

0 commit comments

Comments
 (0)