Skip to content

Commit

Permalink
fix 'invalid compressed sequence' error, issue #871 (#872)
Browse files Browse the repository at this point in the history
Co-authored-by: dvilaverde <[email protected]>
  • Loading branch information
dvilaverde and dvilaverde authored May 8, 2024
1 parent 8d4de0f commit ea1c565
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
11 changes: 11 additions & 0 deletions client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,17 @@ func (s *clientTestSuite) TestConn_Ping() {
require.NoError(s.T(), err)
}

func (s *clientTestSuite) TestConn_Compress() {
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
conn, err := Connect(addr, *testUser, *testPassword, "", func(conn *Conn) {
conn.SetCapability(mysql.CLIENT_COMPRESS)
})
require.NoError(s.T(), err)

_, err = conn.Execute("SELECT VERSION()")
require.NoError(s.T(), err)
}

func (s *clientTestSuite) TestConn_SetCapability() {
caps := []uint32{
mysql.CLIENT_LONG_PASSWORD,
Expand Down
12 changes: 11 additions & 1 deletion packet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type Conn struct {
compressedHeader [7]byte

compressedReader io.Reader

compressedReaderActive bool
}

func NewConn(conn net.Conn) *Conn {
Expand Down Expand Up @@ -106,12 +108,19 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
}()

if c.Compression != MYSQL_COMPRESS_NONE {
if c.compressedReader == nil {
// it's possible that we're using compression but the server response with a compressed
// packet with uncompressed length of 0. In this case we leave compressedReader nil. The
// compressedReaderActive flag is important to track the state of the reader, allowing
// for the compressedReader to be reset after a packet write. Without this flag, when a
// compressed packet with uncompressed length of 0 is read, the compressedReader would
// be nil, and we'd incorrectly attempt to read the next packet as compressed.
if !c.compressedReaderActive {
var err error
c.compressedReader, err = c.newCompressedPacketReader()
if err != nil {
return nil, err
}
c.compressedReaderActive = true
}
}

Expand Down Expand Up @@ -312,6 +321,7 @@ func (c *Conn) WritePacket(data []byte) error {
return errors.Wrapf(ErrBadConn, "Write failed. only %v bytes written, while %v expected", n, len(data))
}
c.compressedReader = nil
c.compressedReaderActive = false
default:
return errors.Wrapf(ErrBadConn, "Write failed. Unsuppored compression algorithm set")
}
Expand Down

0 comments on commit ea1c565

Please sign in to comment.