Skip to content

Commit 3e05e20

Browse files
dvilaverdedvilaverde
andcommitted
fix 'invalid compressed sequence' error, issue go-mysql-org#871 (go-mysql-org#872)
Co-authored-by: dvilaverde <[email protected]>
1 parent 86cef81 commit 3e05e20

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

client/client_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,17 @@ func (s *clientTestSuite) TestConn_Ping() {
8989
require.NoError(s.T(), err)
9090
}
9191

92+
func (s *clientTestSuite) TestConn_Compress() {
93+
addr := fmt.Sprintf("%s:%s", *test_util.MysqlHost, s.port)
94+
conn, err := Connect(addr, *testUser, *testPassword, "", func(conn *Conn) {
95+
conn.SetCapability(mysql.CLIENT_COMPRESS)
96+
})
97+
require.NoError(s.T(), err)
98+
99+
_, err = conn.Execute("SELECT VERSION()")
100+
require.NoError(s.T(), err)
101+
}
102+
92103
func (s *clientTestSuite) TestConn_SetCapability() {
93104
caps := []uint32{
94105
mysql.CLIENT_LONG_PASSWORD,

packet/conn.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,8 @@ type Conn struct {
6969
compressedReader io.Reader
7070

7171
Stats *Stats
72+
73+
compressedReaderActive bool
7274
}
7375

7476
func NewConn(conn net.Conn) *Conn {
@@ -108,12 +110,19 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
108110
}()
109111

110112
if c.Compression != MYSQL_COMPRESS_NONE {
111-
if c.compressedReader == nil {
113+
// it's possible that we're using compression but the server response with a compressed
114+
// packet with uncompressed length of 0. In this case we leave compressedReader nil. The
115+
// compressedReaderActive flag is important to track the state of the reader, allowing
116+
// for the compressedReader to be reset after a packet write. Without this flag, when a
117+
// compressed packet with uncompressed length of 0 is read, the compressedReader would
118+
// be nil, and we'd incorrectly attempt to read the next packet as compressed.
119+
if !c.compressedReaderActive {
112120
var err error
113121
c.compressedReader, err = c.newCompressedPacketReader()
114122
if err != nil {
115123
return nil, err
116124
}
125+
c.compressedReaderActive = true
117126
}
118127
}
119128

@@ -317,6 +326,7 @@ func (c *Conn) WritePacket(data []byte) error {
317326
return errors.Wrapf(ErrBadConn, "Write failed. only %v bytes written, while %v expected", n, len(data))
318327
}
319328
c.compressedReader = nil
329+
c.compressedReaderActive = false
320330
default:
321331
return errors.Wrapf(ErrBadConn, "Write failed. Unsuppored compression algorithm set")
322332
}

0 commit comments

Comments
 (0)