Skip to content

Commit 3bc5897

Browse files
committed
Early return for context cancelled at start
Don't break the connection when cancelled context is passed. Fix go-sql-driver#858
1 parent 14e5817 commit 3bc5897

File tree

1 file changed

+7
-8
lines changed

1 file changed

+7
-8
lines changed

connection.go

+7-8
Original file line numberDiff line numberDiff line change
@@ -595,22 +595,21 @@ func (mc *mysqlConn) watchCancel(ctx context.Context) error {
595595
mc.cleanup()
596596
return nil
597597
}
598+
// When ctx is already cancelled, don't watch it.
599+
if err := ctx.Err(); err != nil {
600+
return err
601+
}
602+
// When ctx is not cancellable, don't watch it.
598603
if ctx.Done() == nil {
599604
return nil
600605
}
601-
602-
mc.watching = true
603-
select {
604-
default:
605-
case <-ctx.Done():
606-
return ctx.Err()
607-
}
606+
// When watcher don't alive, can't watch it.
608607
if mc.watcher == nil {
609608
return nil
610609
}
611610

611+
mc.watching = true
612612
mc.watcher <- ctx
613-
614613
return nil
615614
}
616615

0 commit comments

Comments
 (0)