Skip to content

Panic when a closed connnection is used #142

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

Closed
ghost opened this issue Oct 29, 2013 · 7 comments · Fixed by #143
Closed

Panic when a closed connnection is used #142

ghost opened this issue Oct 29, 2013 · 7 comments · Fixed by #143

Comments

@ghost
Copy link

ghost commented Oct 29, 2013

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
Copy link
Author

ghost commented Oct 29, 2013

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

@arnehormann
Copy link
Member

It looks like #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
Copy link
Member

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

@julienschmidt julienschmidt reopened this Oct 29, 2013
@arnehormann
Copy link
Member

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
Copy link
Author

ghost commented Oct 30, 2013

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

@arnehormann
Copy link
Member

@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
Copy link
Member

fixed in #143

julienschmidt added a commit that referenced this issue Nov 2, 2013
This helps with #142
The database/sql package only retries if it encounters an ErrBadConn.
With errInvalidConn it returns immediately
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants