Skip to content

Commit 5bfa422

Browse files
committed
check mc.reset
1 parent aecdebb commit 5bfa422

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

packets.go

+19-11
Original file line numberDiff line numberDiff line change
@@ -143,17 +143,25 @@ func (mc *mysqlConn) writePacket(ctx context.Context, data []byte) error {
143143
}
144144
data[3] = mc.sequence
145145

146-
// check the connection is still alive
147-
select {
148-
case <-mc.readRes:
149-
mc.closeContext(ctx)
150-
return errBadConnNoWrite
151-
case <-mc.closech:
152-
return ErrInvalidConn
153-
case <-ctx.Done():
154-
mc.cleanup()
155-
return ctx.Err()
156-
default:
146+
// Perform a stale connection check. We only perform this check for
147+
// the first query on a connection that has been checked out of the
148+
// connection pool: a fresh connection from the pool is more likely
149+
// to be stale, and it has not performed any previous writes that
150+
// could cause data corruption, so it's safe to return ErrBadConn
151+
// if the check fails.
152+
if mc.reset {
153+
mc.reset = false
154+
select {
155+
case <-mc.readRes:
156+
mc.closeContext(ctx)
157+
return errBadConnNoWrite
158+
case <-mc.closech:
159+
return ErrInvalidConn
160+
case <-ctx.Done():
161+
mc.cleanup()
162+
return ctx.Err()
163+
default:
164+
}
157165
}
158166

159167
// request writing the packet

0 commit comments

Comments
 (0)