Skip to content

Commit 55ec77f

Browse files
committed
Fix context cancelling without sending COMM_QUIT for authenticated connections
1 parent c9f41c0 commit 55ec77f

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

connection.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ type mysqlConn struct {
4040

4141
// for context support (Go 1.8+)
4242
watching bool
43+
authed bool
4344
watcher chan<- context.Context
4445
closech chan struct{}
4546
finished chan<- struct{}
@@ -436,9 +437,15 @@ func (mc *mysqlConn) getSystemVar(name string) ([]byte, error) {
436437
}
437438

438439
// cancel is called when the query has canceled.
439-
func (mc *mysqlConn) cancel(err error) {
440+
func (mc *mysqlConn) cancel(err error) error {
440441
mc.canceled.Set(err)
442+
if mc.authed {
443+
return mc.Close()
444+
}
445+
441446
mc.cleanup()
447+
448+
return nil
442449
}
443450

444451
// finish is called when the query has succeeded.
@@ -624,7 +631,9 @@ func (mc *mysqlConn) startWatcher() {
624631

625632
select {
626633
case <-ctx.Done():
627-
mc.cancel(ctx.Err())
634+
if err := mc.cancel(ctx.Err()); err != nil {
635+
mc.log(err)
636+
}
628637
case <-finished:
629638
case <-mc.closech:
630639
return

connector.go

+1
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ func (c *connector) Connect(ctx context.Context) (driver.Conn, error) {
169169
mc.cleanup()
170170
return nil, err
171171
}
172+
mc.authed = true
172173

173174
if mc.cfg.MaxAllowedPacket > 0 {
174175
mc.maxAllowedPacket = mc.cfg.MaxAllowedPacket

0 commit comments

Comments
 (0)