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

fix for race condition when multiple processes try to add the same tag #499

Merged
merged 4 commits into from
Apr 1, 2014
Merged

fix for race condition when multiple processes try to add the same tag #499

merged 4 commits into from
Apr 1, 2014

Conversation

jonseaberg
Copy link
Contributor

It is possible for a site using tags to have multiple web servers and multiple job processes. In this case it is also likely that more than one process could try to add the same tag at the same time. Now that there is a unique index on tag name, no duplicates are created, but a cryptic ActiveRecord::RecordNotUnique error makes it up the client code where the taggable was saved. It is hard for the client to know that the ActiveRecord::RecordNotUnique must be caught and a simple re save will succeed. This PR is an attempt to make that process easier.

While testing this I found that Postgres and MySQL behave differently when the unique index is violated. Postgres aborts the current transaction, so no more database operations can be executed. For Postgres this means we need to get an exception up to the client. I chose to add an exception to the library so clients can catch and retry their operation. For MySQL we can catch ActiveRecord::RecordNotUnique and request the 'duplicates' again.

@seuros
Copy link
Collaborator

seuros commented Mar 23, 2014

👍

bob.tag_list << "happier"
bob.save
}.should change(ActsAsTaggableOn::Tagging, :count).by(1)
describe "Duplicates" do
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think this should be a context

@forest
Copy link

forest commented Mar 24, 2014

👍

- Raising the exception from save_tags is better than catching all exceptions.  The client can retry the save on duplicate error.
- add `extend self` to ActsAsTaggableOn::Utils so module functions can be called directly
…sql with retry was a good idea anyway. Now always raise ActsAsTaggableOn::DuplicateTagError when ActiveRecord::RecordNotUnique is thrown.
@jonseaberg
Copy link
Contributor Author

The multithreaded tests are causing deadlocks on Travis for MySQL. I don't have time to work on this right now. I will get back to this in a couple of days. Sorry for the delay.

@seuros seuros modified the milestone: 3.2.0 Mar 28, 2014
@seuros
Copy link
Collaborator

seuros commented Mar 30, 2014

I set this PR for the version 3.2.0 . It passing all tests now.
I will merge it after the 3.1.0 release.

seuros added a commit that referenced this pull request Apr 1, 2014
…g-tags

fix for race condition when multiple processes try to add the same tag
@seuros seuros merged commit 0f256eb into mbleigh:master Apr 1, 2014
tekniklr pushed a commit to tekniklr/acts-as-taggable-on that referenced this pull request Mar 19, 2021
…n-adding-tags

fix for race condition when multiple processes try to add the same tag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants