DATA-DOG / go-txdb

Immutable transaction isolated sql driver for golang
Other
667 stars 48 forks source link

DATA RACE in concurrent testing #44

Closed xuwei0455 closed 3 years ago

xuwei0455 commented 3 years ago
WARNING: DATA RACE
Read at 0x00c000081380 by goroutine 63:
  github.com/DATA-DOG/go-txdb.(*txDriver).Open()
      /home/runner/work/coinex_push_backend/coinex_push_backend/vendor/github.com/DATA-DOG/go-txdb/db.go:145 +0x17d
  database/sql.dsnConnector.Connect()
      /opt/hostedtoolcache/go/1.16.9/x64/src/database/sql/sql.go:707 +0x8c
  database/sql.(*dsnConnector).Connect()
      <autogenerated>:1 +0x2e
...........
Previous write at 0x00c000081380 by goroutine 65:
  github.com/DATA-DOG/go-txdb.(*conn).Close()
      /home/runner/work/coinex_push_backend/coinex_push_backend/vendor/github.com/DATA-DOG/go-txdb/db.go:181 +0xda
  database/sql.(*driverConn).finalClose.func2()
      /opt/hostedtoolcache/go/1.16.9/x64/src/database/sql/sql.go:592 +0x8b
  database/sql.withLock()
      /opt/hostedtoolcache/go/1.16.9/x64/src/database/sql/sql.go:3294 +0x7e
  database/sql.(*driverConn).finalClose()

First, thanks for your great code library.

When I testing my code in concurrent mode, I found a data race warning as above.

After some research, I found the issue appear in the Open() method as below:

    c, ok := d.conns[dsn]
    if !ok {
        c = &conn{dsn: dsn, drv: d, savePoint: &defaultSavePoint{}}
        for _, opt := range d.options {
            if e := opt(c); e != nil {
                return c, e
            }
        }
        d.conns[dsn] = c
    }
    c.opened++
    return c, nil

which connections have not been protected by a lock, so I write this issue to report that.

And, a PR also been created for this.

l3pp4rd commented 3 years ago

thanks for taking time to contribute! merged the fix