database/sql: sql.DB.Close will not shutdown connectionOpener, causing goroutine leak

116 views
Skip to first unread message

tom...@gmail.com

unread,
Mar 9, 2014, 2:49:08 PM3/9/14
to golan...@googlegroups.com
connectionOpener is a goroutine that contain a loop forever, if I'm right.


func Open(driverName, dataSourceName string) (*DB, error) {
        ....
go db.connectionOpener()
return db, nil
}

func (db *DB) connectionOpener() {
for _ = range db.openerCh {
db.openNewConnection()
}
}

-------

Therefore, simple code will cause goroutine leak. :

DB.Open()
...
DB.Close()
...
...
DB.Open()
...
DB.Close()

minux

unread,
Mar 10, 2014, 1:46:21 AM3/10/14
to tom...@gmail.com, golang-dev
On Sun, Mar 9, 2014 at 2:49 PM, <tom...@gmail.com> wrote:
connectionOpener is a goroutine that contain a loop forever, if I'm right. 

func Open(driverName, dataSourceName string) (*DB, error) {
        ....
go db.connectionOpener()
return db, nil
}

func (db *DB) connectionOpener() {
for _ = range db.openerCh {
Why this is a forever loop? It will exit once the db.openerCh channel is closed.
And in fact, in (*DB).Close, there is a statement "close(db.openerCh)", so the
connectionOpener goroutine won't leak.

tOmasEn

unread,
Mar 10, 2014, 3:50:54 AM3/10/14
to minux, golang-dev
I'm not sure why. But in my case(some sort of miss use sql, I call sql.Open() very frequently, like 10+ calls/second ), deadlock happened in:
 for _ = range db.openerCh {
 }

On Monday, March 10, 2014 2:49:08 AM UTC+8, Tomasen Sen wrote:
connectionOpener is a goroutine that contain a loop forever, if I'm right.


func Open(driverName, dataSourceName string) (*DB, error) {
        ....
go db.connectionOpener()
return db, nil
}

func (db *DB) connectionOpener() {
for _ = range db.openerCh {
db.openNewConnection()
}
}

-------

Therefore, simple code will cause goroutine leak. :

DB.Open()
...
DB.Close()
...
...
DB.Open()
...
DB.Close()
--
沈晟|Tomasen
品志文化|射手网|射手影音|射手娱乐
http://twitter.com/ShooterPlayer
http://t.sina.com.cn/Tomasen
Reply all
Reply to author
Forward
0 new messages