go-sql-driver / go-sql-driver/mysql

Context cancelling cause close connection without sending COMM_QUIT for authenticated connection

Open
#1,648 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
15.3k
Forks
2.3k
Avg merge
2h 23m
Merged PRs (30d)
10

Description

### Issue description
[startWatcher()](https://github.com/go-sql-driver/mysql/blob/master/connection.go#L611) watches the context provided by the Go client. When context cancellation occurs, this method only [cleanup()](https://github.com/go-sql-driver/mysql/blob/c9f41c074062d5ab9aeb5e44adeac3a7d85fbc4e/connection.go#L134-L138) the connection without sending COMM_QUIT, regardless of whether the connection has already been authenticated or not.

Without COMM_QUIT, MySQL Server will abort authenticated clients, and increase [Aborted_clients](https://dev.mysql.com/doc/refman/8.4/en/server-status-variables.html#statvar_Aborted_clients) metric.

### Related code
- Current handling when context cancel:
- https://github.com/go-sql-driver/mysql/blob/c9f41c074062d5ab9aeb5e44adeac3a7d85fbc4e/connection.go#L439-L441

- But cleanup() is only called before auth or on auth failure
- https://github.com/go-sql-driver/mysql/blob/c9f41c074062d5ab9aeb5e44adeac3a7d85fbc4e/connection.go#L134-L138

- Close() should be called if the connection has been authenticated successfully with sending COMM_QUIT to MySQL server
- https://github.com/go-sql-driver/mysql/blob/master/connection.go#L119

### Error log
```
If you have an error log, please paste it here.
```

### Configuration
*Driver version (or git SHA):* `1.8.1`

*Go version:* run `go version` in your console

*Server version:* E.g. MySQL 5.6, MariaDB 10.0.20

*Server OS:* E.g. Debian 8.1 (Jessie), Windows 10

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.