@@ -69,6 +69,8 @@ type Conn struct {
69
69
compressedReader io.Reader
70
70
71
71
Stats * Stats
72
+
73
+ compressedReaderActive bool
72
74
}
73
75
74
76
func NewConn (conn net.Conn ) * Conn {
@@ -108,12 +110,19 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
108
110
}()
109
111
110
112
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 {
112
120
var err error
113
121
c .compressedReader , err = c .newCompressedPacketReader ()
114
122
if err != nil {
115
123
return nil , err
116
124
}
125
+ c .compressedReaderActive = true
117
126
}
118
127
}
119
128
@@ -317,6 +326,7 @@ func (c *Conn) WritePacket(data []byte) error {
317
326
return errors .Wrapf (ErrBadConn , "Write failed. only %v bytes written, while %v expected" , n , len (data ))
318
327
}
319
328
c .compressedReader = nil
329
+ c .compressedReaderActive = false
320
330
default :
321
331
return errors .Wrapf (ErrBadConn , "Write failed. Unsuppored compression algorithm set" )
322
332
}
0 commit comments