Skip to content
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

Concurrency: race conditions and mutex locks #153

Closed
wants to merge 5 commits into from

Conversation

bennyscetbun
Copy link

Adding mutex on every Plan of a table to avoid race condition when using goroutine and Gorp.

@coopernurse coopernurse added this to the v1.6 milestone May 14, 2014
@coopernurse
Copy link
Contributor

Do we need a separate mutex per method on TableMap? What if we just had a single mutex and used it on each of the relevant methods

Any other opinions?

@bennyscetbun
Copy link
Author

The idea was to avoid a single lock(in case that we ve got a lot of goroutine doing different things).
But yeah we could use your way. A single mutex would work :)

@coopernurse
Copy link
Contributor

That makes sense, although the contention would likely still occur with 4 mutexes instead of one.

If we're worried about contention (and it's a valid concern I think) we may want to move the lock inside that the code that checks for empty string. For example:

func (t *TableMap) bindInsert(elem reflect.Value) (bindInstance, error) {
    plan := t.insertPlan
    if plan.query == "" {

@coopernurse
Copy link
Contributor

That makes sense. Thanks.

@bennyscetbun
Copy link
Author

I think it must be before

if plan.query == "" {

The racing condition was on plan.query ;)

And that way we avoid to run twice the creation code.

By the way thanks for your lib!

@coopernurse coopernurse modified the milestones: v1.7, v1.6 May 16, 2014
@coopernurse coopernurse modified the milestones: v1.7, v1.8 Sep 6, 2014
@GeertJohan
Copy link
Member

I think we need to take a good look at this, it is very important to make sure gorp is concurrency safe!

@GeertJohan GeertJohan changed the title Master Concurrency: race conditions and mutex locks Feb 16, 2015
@GeertJohan GeertJohan force-pushed the master branch 2 times, most recently from a9f5c20 to 361a068 Compare July 2, 2015 07:32
pwaller added a commit to pwaller/gorp that referenced this pull request Nov 11, 2015
Previously, plans were initialised through a racy assignment. Now they
are initialised through a sync.Once, which is more clearly race free and
also makes the intent clear (it is initialisation).

With this change it's also necessary to make the plan more pointer-like,
since if you pass a struct around by value with a lock in it, you are
going to have a bad time. This was pointed out to me by the
gometalinter.

Fixes go-gorp#296.
Supercedes go-gorp#153.
pwaller added a commit to pwaller/gorp that referenced this pull request Nov 12, 2015
Previously, plans were initialised through a racy assignment. Now they
are initialised through a sync.Once, which is more clearly race free and
also makes the intent clear (it is initialisation).

With this change it's also necessary to make the plan more pointer-like,
since if you pass a struct around by value with a lock in it, you are
going to have a bad time. This was pointed out to me by the
gometalinter.

Fixes go-gorp#296.
Supercedes go-gorp#153.
@GeertJohan
Copy link
Member

Superseded by #301, which uses sync.Once.

@GeertJohan GeertJohan closed this Dec 23, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants