FAQ
hi,all
       i use golang for several days, mysql app use go-sql-driver/mysql
driver to connect mysql server. the Process of myapp is just like:
       1) db.Query("select *from table_test")
       2) time.Sleep(60*Time.Seconds)
       3) goto 1)

      when myapp run some days, i found there is a fd leak, so i review the
code of go/src/pkg/database/sql/sql.go, i found the code below:
  565 func (db *DB) putConn(dc *driverConn, err error) {
  566 db.mu.Lock()
  567 if !dc.inUse {
  568 if debugGetPut {
  569 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was:
%s", dc, stack(), db.lastPut[dc])
  570 }
  571 panic("sql: connection returned that was never out")
  572 }
  573 if debugGetPut {
  574 db.lastPut[dc] = stack()
  575 }
  576 dc.inUse = false
  577
  578 for _, fn := range dc.onPut {
  579 fn()
  580 }
  581 dc.onPut = nil
  582
  583 if err == driver.ErrBadConn {
  584 // Don't reuse bad connections.


  585 db.mu.Unlock()
  586 return
  587 }
  ...
       }

       in line 583, if db.Query return a error of driver.ErrBadConn, the
driverConn dc is not close! it's simple to fix like below:

  565 func (db *DB) putConn(dc *driverConn, err error) {
  566 db.mu.Lock()
  567 if !dc.inUse {
  568 if debugGetPut {
  569 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS was:
%s", dc, stack(), db.lastPut[dc])
  570 }
  571 panic("sql: connection returned that was never out")
  572 }
  573 if debugGetPut {
  574 db.lastPut[dc] = stack()
  575 }
  576 dc.inUse = false
  577
  578 for _, fn := range dc.onPut {
  579 fn()
  580 }
  581 dc.onPut = nil
  582
  583 if err == driver.ErrBadConn {
  584 // Don't reuse bad connections.
  585 dc.Close()


  586 db.mu.Unlock()
  587 return
  588 }
  589 if putConnHook != nil {
  590 putConnHook(db, dc)
  591 }
  592 if n := len(db.freeConn); !db.closed && n <
db.maxIdleConnsLocked() {
  593 db.freeConn = append(db.freeConn, dc)
  594 db.mu.Unlock()
  595 return
  596 }
...
          is me right? forgive for my pool english, thank you

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Search Discussions

  • Tad Glines at Dec 30, 2013 at 7:06 pm
    This is a bug that was fixed prior to the release of go 1.2.
    Upgrade to go 1.2.

    On Mon, Dec 30, 2013 at 5:14 AM, wrote:

    hi,all
    i use golang for several days, mysql app use go-sql-driver/mysql
    driver to connect mysql server. the Process of myapp is just like:
    1) db.Query("select *from table_test")
    2) time.Sleep(60*Time.Seconds)
    3) goto 1)

    when myapp run some days, i found there is a fd leak, so i review the
    code of go/src/pkg/database/sql/sql.go, i found the code below:
    565 func (db *DB) putConn(dc *driverConn, err error) {
    566 db.mu.Lock()
    567 if !dc.inUse {
    568 if debugGetPut {
    569 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS
    was: %s", dc, stack(), db.lastPut[dc])
    570 }
    571 panic("sql: connection returned that was never out")
    572 }
    573 if debugGetPut {
    574 db.lastPut[dc] = stack()
    575 }
    576 dc.inUse = false
    577
    578 for _, fn := range dc.onPut {
    579 fn()
    580 }
    581 dc.onPut = nil
    582
    583 if err == driver.ErrBadConn {
    584 // Don't reuse bad connections.


    585 db.mu.Unlock()
    586 return
    587 }
    ...
    }

    in line 583, if db.Query return a error of driver.ErrBadConn, the
    driverConn dc is not close! it's simple to fix like below:

    565 func (db *DB) putConn(dc *driverConn, err error) {
    566 db.mu.Lock()
    567 if !dc.inUse {
    568 if debugGetPut {
    569 fmt.Printf("putConn(%v) DUPLICATE was: %s\n\nPREVIOUS
    was: %s", dc, stack(), db.lastPut[dc])
    570 }
    571 panic("sql: connection returned that was never out")
    572 }
    573 if debugGetPut {
    574 db.lastPut[dc] = stack()
    575 }
    576 dc.inUse = false
    577
    578 for _, fn := range dc.onPut {
    579 fn()
    580 }
    581 dc.onPut = nil
    582
    583 if err == driver.ErrBadConn {
    584 // Don't reuse bad connections.
    585 dc.Close()


    586 db.mu.Unlock()
    587 return
    588 }
    589 if putConnHook != nil {
    590 putConnHook(db, dc)
    591 }
    592 if n := len(db.freeConn); !db.closed && n <
    db.maxIdleConnsLocked() {
    593 db.freeConn = append(db.freeConn, dc)
    594 db.mu.Unlock()
    595 return
    596 }
    ...
    is me right? forgive for my pool english, thank you

    --
    You received this message because you are subscribed to the Google Groups
    "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an
    email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.
    --
    You received this message because you are subscribed to the Google Groups "golang-nuts" group.
    To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts+unsubscribe@googlegroups.com.
    For more options, visit https://groups.google.com/groups/opt_out.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupgolang-nuts @
categoriesgo
postedDec 30, '13 at 1:14p
activeDec 30, '13 at 7:06p
posts2
users2
websitegolang.org

2 users in discussion

Tad Glines: 1 post Shelrockhua: 1 post

People

Translate

site design / logo © 2021 Grokbase