Skip to content

[1.9] fix PING on compressed connections #1723

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 13, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 4 additions & 5 deletions compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,11 @@ func (c *compIO) readCompressedPacket() error {
// Server may return error packet (e.g. 1153 Got a packet bigger than 'max_allowed_packet' bytes)
// before receiving all packets from client. In this case, seqnr is younger than expected.
// NOTE: Both of mariadbclient and mysqlclient do not check seqnr. Only server checks it.
if debug && compressionSequence != c.mc.sequence {
if debug && compressionSequence != c.mc.compressSequence {
fmt.Printf("WARN: unexpected cmpress seq nr: expected %v, got %v",
c.mc.sequence, compressionSequence)
c.mc.compressSequence, compressionSequence)
}
c.mc.sequence = compressionSequence + 1
c.mc.compressSequence = c.mc.sequence
c.mc.compressSequence = compressionSequence + 1

comprData, err := c.mc.readNext(comprLength)
if err != nil {
Expand Down Expand Up @@ -200,7 +199,7 @@ func (c *compIO) writeCompressedPacket(data []byte, uncompressedLen int) (int, e
comprLength := len(data) - 7
if debug {
fmt.Printf(
"writeCompressedPacket: comprLength=%v, uncompressedLen=%v, seq=%v",
"writeCompressedPacket: comprLength=%v, uncompressedLen=%v, seq=%v\n",
comprLength, uncompressedLen, mc.compressSequence)
}

Expand Down
32 changes: 15 additions & 17 deletions packets.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"fmt"
"io"
"math"
"os"
"strconv"
"time"
)
Expand Down Expand Up @@ -62,26 +63,20 @@ func (mc *mysqlConn) readPacket() ([]byte, error) {
pktLen := getUint24(data[:3])
seq := data[3]

if mc.compress {
// check packet sync [8 bit]
if seq != mc.sequence {
mc.log(fmt.Sprintf("[warn] unexpected sequence nr: expected %v, got %v", mc.sequence, seq))
// MySQL and MariaDB doesn't check packet nr in compressed packet.
if debug && seq != mc.compressSequence {
fmt.Printf("[debug] mismatched compression sequence nr: expected: %v, got %v",
mc.compressSequence, seq)
}
mc.compressSequence = seq + 1
} else {
// check packet sync [8 bit]
if seq != mc.sequence {
mc.log(fmt.Sprintf("[warn] unexpected seq nr: expected %v, got %v", mc.sequence, seq))
if !mc.compress {
// For large packets, we stop reading as soon as sync error.
if len(prevData) > 0 {
mc.close()
return nil, ErrPktSyncMul
}
invalidSequence = true
}
mc.sequence++
}
mc.sequence = seq + 1

// packets with length 0 terminate a previous packet which is a
// multiple of (2^24)-1 bytes long
Expand Down Expand Up @@ -146,7 +141,7 @@ func (mc *mysqlConn) writePacket(data []byte) error {

// Write packet
if debug {
fmt.Printf("writePacket: size=%v seq=%v", size, mc.sequence)
fmt.Fprintf(os.Stderr, "writePacket: size=%v seq=%v\n", size, mc.sequence)
}

n, err := writeFunc(data[:4+size])
Expand Down Expand Up @@ -445,7 +440,9 @@ func (mc *mysqlConn) writeCommandPacket(command byte) error {
data[4] = command

// Send CMD packet
return mc.writePacket(data)
err = mc.writePacket(data)
mc.syncSequence()
return err
}

func (mc *mysqlConn) writeCommandPacketStr(command byte, arg string) error {
Expand Down Expand Up @@ -486,7 +483,9 @@ func (mc *mysqlConn) writeCommandPacketUint32(command byte, arg uint32) error {
binary.LittleEndian.PutUint32(data[5:], arg)

// Send CMD packet
return mc.writePacket(data)
err = mc.writePacket(data)
mc.syncSequence()
return err
}

/******************************************************************************
Expand Down Expand Up @@ -956,7 +955,6 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {
pktLen = dataOffset + argLen
}

stmt.mc.resetSequence()
// Add command byte [1 byte]
data[4] = comStmtSendLongData

Expand All @@ -968,15 +966,15 @@ func (stmt *mysqlStmt) writeCommandLongData(paramID int, arg []byte) error {

// Send CMD packet
err := stmt.mc.writePacket(data[:4+pktLen])
// Every COM_LONG_DATA packet reset Packet Sequence
stmt.mc.resetSequence()
if err == nil {
data = data[pktLen-dataOffset:]
continue
}
return err
}

// Reset Packet Sequence
stmt.mc.resetSequence()
return nil
}

Expand Down