Skip to content

Data race in Insert() #296

@pwaller

Description

@pwaller

The race detector just found a race in my web app.

My code is really very straightforward. What I'm doing amounts to this:

package db

import "time"

type Foo struct {
    Id       string    `db:"id"`
}

func InsertFoo() error {
    return dbmap.Insert(&p)
}

// Call InsertFoo from a web handler.
==================
WARNING: DATA RACE
Read by goroutine 65:
  x/vendor/github.com/go-gorp/gorp.(*TableMap).bindInsert()
      /go/src/x/vendor/github.com/go-gorp/gorp/table_bindings.go:103 +0x8b
  x/vendor/github.com/go-gorp/gorp.insert()
      /go/src/x/vendor/github.com/go-gorp/gorp/gorp.go:505 +0x2f4
  x/vendor/github.com/go-gorp/gorp.(*DbMap).Insert()

Previous write by goroutine 86:
  x/vendor/github.com/go-gorp/gorp.(*TableMap).bindInsert()
      /go/src/x/vendor/github.com/go-gorp/gorp/table_bindings.go:157 +0x942
  x/vendor/github.com/go-gorp/gorp.insert()
      /go/src/x/vendor/github.com/go-gorp/gorp/gorp.go:505 +0x2f4
  x/vendor/github.com/go-gorp/gorp.(*DbMap).Insert()
      /go/src/x/vendor/github.com/go-gorp/gorp/db.go:395 +0x99

I'm surprised that I could be the first person to report a race bug here.

The report suggests this line:

plan := t.insertPlan

Is racing against this line:

t.insertPlan = plan

Am I misusing Insert? I did not realise anywhere from the documentation that I would have to lock the DbMap during inserts.

The Insert docs don't mention anything about concurrency:

https://godoc.org/gopkg.in/gorp.v1#DbMap.Insert

And the DbMap docs suggest that I only have to make one per schema, not per request:

https://godoc.org/gopkg.in/gorp.v1#DbMap

Thoughts?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions