mysql icon indicating copy to clipboard operation
mysql copied to clipboard

Should return i/o read/write timeout error, not just invalid connection(ErrInvalidConn)

Open feiin opened this issue 5 years ago • 2 comments

Issue description

Should return i/o read/write timeout error, not just invalid connection(ErrInvalidConn).

Example code

db, _ = sql.Open("mysql", "root:xxxx@tcp(127.0.0.1:3306)/xx_db?charset=utf8mb4&readTimeout=2s&timeout=2s&writeTimeout=2s")

_, err := db.Query("select sleep(3)")

// doCheckError(err)

defer db.Close()

Error log

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

Configuration

Driver version (or git SHA):

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

feiin avatar Jul 24 '19 13:07 feiin

+1

xiaowen1108 avatar Aug 01 '19 11:08 xiaowen1108

You had not describe why "should".

methane avatar Oct 22 '19 07:10 methane

This is causing troubles:

// create a listener which does not accept
	listener, err := net.Listen("tcp", fmt.Sprintf("localhost:%d", p))
	if err != nil {
		t.Fatal(err)
	}
	defer listener.Close()

	t.Run("test open", func(t *testing.T) {
		db, err := sql.Open("mysql", fmt.Sprintf("test@tcp(localhost:%d)/test?timeout=1s", p))
		assert.NoError(t, err)
		assert.ErrorContains(t, db.Ping(), "deadline exceeded")
	})

Here you see we have a listener, which does accept - this is btw the case when your port is behind a firewall and no access is allowed.

If I use this driver and set timeout=10s and try to connect to a database which is behind a firewall - or a listener which does not accept (ACK) I would expect the a call to Ping() to fail after 10s - it just hangs

timo-klarshift avatar Apr 06 '23 15:04 timo-klarshift

@timo-klarshift your case is not relating to this issue.

Your example code doesn't accept connection. But your OS accept some connections. timeout=1s doesn't cause error because mysql driver succeeded to connect. And there are no timeout for Ping in your code. That's why your example hangs up.

methane avatar Apr 07 '23 03:04 methane

You had not describe why "should".

if there is timeout Error, we can diff it from such as closed connection. mostly maybe cause by a slow sql

feiin avatar Apr 07 '23 03:04 feiin

How is it important to write a robust application? I can find slow sql without relying on i/o error.

methane avatar Apr 07 '23 07:04 methane

Just a case example, it would be better if the error types are different.

feiin avatar Apr 08 '23 15:04 feiin