Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use of closed network connection occurs rarely when context is canceled during establishing a connection #1614

Closed
yokonao opened this issue Jul 31, 2024 · 0 comments · Fixed by #1615

Comments

@yokonao
Copy link
Contributor

yokonao commented Jul 31, 2024

Issue description

Executing query returns context.Canceled in most cases when context canceled.
However, we see use of closed network connection rarely when context canceled during querying or executing.

For error handling purposes, it is preferable to always return context.Canceled.

similar issue: #1078

Example code

package main

import (
	"context"
	"errors"
	"log"
	"os"
	"time"

	"github.com/go-sql-driver/mysql"
)

func main() {
	ctx, cancel := context.WithCancel(context.Background())
	dsn := "mysql:" + os.Getenv("DB_PASSWD") + "@tcp(127.0.0.1:3306)/?allowNativePasswords=true&tls=preferred"
	connector, err := mysql.MySQLDriver{}.OpenConnector(dsn)
	if err != nil {
		log.Fatal(err)
	}

	go func() {
		time.Sleep(10 * time.Millisecond) // may be need to adjust the sleep time.
		cancel()
	}()
	conn, err := connector.Connect(ctx)
	if err != nil {
		if errors.Is(err, context.Canceled) {
			log.Println("context canceled. no problem.")
			return
		}
		panic(err) // use of closed network connection
	}
	defer conn.Close()
}

It seems that use of closed network connection occurs when ctx is canceled at a specific timing while the connection is being established.

Error log

panic: read tcp 127.0.0.1:51710->127.0.0.1:3306: use of closed network connection

Configuration

Driver version (or git SHA): v1.7.1 f20b286
Go version: go version go1.22.4 darwin/arm64
Server version: Mysql 8.0

Server OS: Debian GNU/Linux 11 (bullseye)

methane pushed a commit to methane/mysql that referenced this issue Nov 13, 2024
…sql-driver#1615)

### Description
Check if the context is canceled when failed to TLS handshake.

fix: go-sql-driver#1614

### Checklist
- [x] Code compiles correctly
- [x] Created tests which fail without the change (if possible)
- [x] All tests passing
- [x] Extended the README / documentation, if necessary
- [x] Added myself / the copyright holder to the AUTHORS file


<!-- This is an auto-generated comment: release notes by coderabbit.ai
-->

## Summary by CodeRabbit

- **New Features**
- Added Nao Yokotsuka to the contributors list for improved project
documentation.
  
- **Bug Fixes**
- Enhanced error handling in the TLS handshake process to better manage
cancellation requests, improving connection responsiveness.

<!-- end of auto-generated comment: release notes by coderabbit.ai -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant