@@ -67,6 +67,8 @@ type Conn struct {
67
67
compressedHeader [7 ]byte
68
68
69
69
compressedReader io.Reader
70
+
71
+ compressedReaderActive bool
70
72
}
71
73
72
74
func NewConn (conn net.Conn ) * Conn {
@@ -106,12 +108,19 @@ func (c *Conn) ReadPacketReuseMem(dst []byte) ([]byte, error) {
106
108
}()
107
109
108
110
if c .Compression != MYSQL_COMPRESS_NONE {
109
- if c .compressedReader == nil {
111
+ // it's possible that we're using compression but the server response with a compressed
112
+ // packet with uncompressed length of 0. In this case we leave compressedReader nil. The
113
+ // compressedReaderActive flag is important to track the state of the reader, allowing
114
+ // for the compressedReader to be reset after a packet write. Without this flag, when a
115
+ // compressed packet with uncompressed length of 0 is read, the compressedReader would
116
+ // be nil, and we'd incorrectly attempt to read the next packet as compressed.
117
+ if ! c .compressedReaderActive {
110
118
var err error
111
119
c .compressedReader , err = c .newCompressedPacketReader ()
112
120
if err != nil {
113
121
return nil , err
114
122
}
123
+ c .compressedReaderActive = true
115
124
}
116
125
}
117
126
@@ -312,6 +321,7 @@ func (c *Conn) WritePacket(data []byte) error {
312
321
return errors .Wrapf (ErrBadConn , "Write failed. only %v bytes written, while %v expected" , n , len (data ))
313
322
}
314
323
c .compressedReader = nil
324
+ c .compressedReaderActive = false
315
325
default :
316
326
return errors .Wrapf (ErrBadConn , "Write failed. Unsuppored compression algorithm set" )
317
327
}
0 commit comments