go-sql-driver / mysql

Go MySQL Driver is a MySQL driver for Go's (golang) database/sql package
https://pkg.go.dev/github.com/go-sql-driver/mysql
Mozilla Public License 2.0
14.51k stars 2.31k forks source link

Panic when a closed connnection is used #142

Closed ghost closed 11 years ago

ghost commented 11 years ago

i use git clone get new code, if I kill the mysql connection: under linux(centos i686), it fatal. under windows(xp, x32), it not autoreconnect

mysql version 5.1.69(contos x64)

[wanghl@localhost iVega]$ go run testdb.go 2013/10/29 15:13:29 insert 0 2013/10/29 15:13:32 insert 1 2013/10/29 15:13:35 insert 2 2013/10/29 15:13:38 insert 3 2013/10/29 15:13:41 insert 4 [MySQL] 2013/10/29 15:13:41 packets.go:30: EOF 2013/10/29 15:13:41 wanghl! driver: bad connection 2013/10/29 15:13:44 insert 5 panic: runtime error: invalid memory address or nil pointer dereference [signal 0xb code=0x1 addr=0x1 pc=0x8078693]

goroutine 1 [running]: github.com/go-sql-driver/mysql.(_mysqlStmt).writeExecutePacket(0x1835b900, 0x1835f330, 0x5, 0x5, 0x1, ...) /home/wanghl/mygo/src/github.com/go-sql-driver/mysql/packets.go:818 +0x1a03 github.com/go-sql-driver/mysql.(_mysqlStmt).Exec(0x1835b900, 0x1835f330, 0x5, 0x5, 0x1835f330, ...) /home/wanghl/mygo/src/github.com/go-sql-driver/mysql/statement.go:38 +0x41 database/sql.resultFromStatement(0x1835b960, 0x1835f180, 0x1835f1b0, 0x1835b900, 0xb73f8f00, ...) /usr/local/go/src/pkg/database/sql/sql.go:1077 +0x1df database/sql.(*Stmt).Exec(0x18363140, 0xb73f8f00, 0x5, 0x5, 0x0, ...) /usr/local/go/src/pkg/database/sql/sql.go:1056 +0x13d main.Insert(0x7, 0x5, 0x81a0108, 0x3, 0x18300488, ...) /home/wanghl/mygo/src/iVega/testdb.go:66 +0x14c main.main() /home/wanghl/mygo/src/iVega/testdb.go:31 +0x269

goroutine 2 [syscall]: exit status 2

E:\mygo>go run testdb.go 2013/10/29 15:12:57 insert 0 2013/10/29 15:13:00 insert 1 2013/10/29 15:13:03 insert 2 2013/10/29 15:13:06 insert 3 2013/10/29 15:13:09 insert 4 [MySQL] 2013/10/29 15:13:09 packets.go:31: EOF 2013/10/29 15:13:09 wanghl! driver: bad connection 2013/10/29 15:13:12 insert 5 [MySQL] 2013/10/29 15:13:12 packets.go:88: WSASend tcp 172.7.3.42:60823: An esta blished connection was aborted by the software in your host machine. 2013/10/29 15:13:12 wanghl! driver: bad connection 2013/10/29 15:13:15 insert 6 [MySQL] 2013/10/29 15:13:15 packets.go:88: WSASend tcp 172.7.3.42:60823: An esta blished connection was aborted by the software in your host machine. 2013/10/29 15:13:15 wanghl! driver: bad connection 2013/10/29 15:13:18 insert 7 [MySQL] 2013/10/29 15:13:18 packets.go:88: WSASend tcp 172.7.3.42:60823: An esta blished connection was aborted by the software in your host machine.

My test code is here: http://play.golang.org/p/XuCQ7Ug-O2

I also test Query(), it work fine

ghost commented 11 years ago

Stmt.Exec() have problem, db.Exec() is OK

arnehormann commented 11 years ago

It looks like https://github.com/go-sql-driver/mysql/issues/98 - we can't do much about that, it's a problem in go's database/sql. They don't accept a fix until go version 1.2 is released.

julienschmidt commented 11 years ago

At least the panic in the driver code shouldn't happen.

arnehormann commented 11 years ago

We found the cause. When Close is called in connection.go, a buffer used by the connection is set to nil. When the connection is used, writeExecutePacket dereferences a nil pointer (mc.buf). You (or in this case database/sql) should not reuse closed connections, but the driver should not panic. We will create a PR to return a meaningful error instead.

ghost commented 11 years ago

go 1.2rc2 test fail, it does not seem to fix.

arnehormann commented 11 years ago

@better0332 yes, we only identified the bug, the pull request with a partial fix (don't panic, just return an error) is not done yet. And we can't really fix this until this bugfix by @julienschmidt is integrated in Go: https://codereview.appspot.com/14920046 - and it looks like that will only take place after Go 1.2

arnehormann commented 11 years ago

fixed in https://github.com/go-sql-driver/mysql/pull/143