Skip to content

Fix issue where counter_cache could get decremented twice under specific conditions#31494

Closed
zhouguangming wants to merge 1 commit intorails:masterfrom
zhouguangming:master
Closed

Fix issue where counter_cache could get decremented twice under specific conditions#31494
zhouguangming wants to merge 1 commit intorails:masterfrom
zhouguangming:master

Conversation

@zhouguangming
Copy link
Copy Markdown
Contributor

@zhouguangming zhouguangming commented Dec 17, 2017

Fix #31493

@rails-bot
Copy link
Copy Markdown

Thanks for the pull request, and welcome! The Rails team is excited to review your changes, and you should hear from @schneems (or someone else) soon.

If any changes to this PR are deemed necessary, please add them as extra commits. This ensures that the reviewer can see what has changed since they last reviewed the code. Due to the way GitHub handles out-of-date commits, this should also make it reasonably obvious what issues have or haven't been addressed. Large or tricky changes may require several passes of review and changes.

This repository is being automatically checked for code quality issues using Code Climate. You can see results for this analysis in the PR status below. Newly introduced issues should be fixed before a Pull Request is considered ready to review.

Please see the contribution instructions for more information.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

You are not considering previous value of hash, It will be better to add / merge key, value pair

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes you are right, I will fix it soon

@zhouguangming
Copy link
Copy Markdown
Contributor Author

Anyone can review my PR?

@eugeneius
Copy link
Copy Markdown
Member

Hi @zhouguangming! Can you add a test that would fail without your change?

@zhouguangming
Copy link
Copy Markdown
Contributor Author

@eugeneius Hi, I have wrote a test case on #31493

@eugeneius
Copy link
Copy Markdown
Member

I mean adding a new test to the test suite.

This change appears to build on #24713; check out the test added there for an example.

@malikolivier
Copy link
Copy Markdown

malikolivier commented Mar 2, 2018

I posted a full failing test case in #32079 as well. Maybe it will come in a handy. https://gist.github.com/malikolivier/87904c9ed078dca186cf455a09a4f30e

@swrobel
Copy link
Copy Markdown
Contributor

swrobel commented Mar 21, 2018

@zhouguangming hopefully you can add the failing test @malikolivier suggested so this can get merged before Rails 5.2 final is released!

@malikolivier
Copy link
Copy Markdown

malikolivier commented Mar 29, 2018

Since @zhouguangming seems busy now, I added a failing test on top of his contribution in #32372. Hope the fix gets release before 5.2!

@sikachu sikachu changed the title fix #31493 Fix issue where counter_cache could get decremented twice under specific conditions Apr 2, 2018
kamipo added a commit to kamipo/rails that referenced this pull request Sep 18, 2018
This is a 4th attempt to make counter cache transactional completely.

Past attempts: rails#9236, rails#14849, rails#23357.

All existing counter cache issues (increment/decrement twice, lost
increment) are caused due to updating counter cache on the outside of
the record saving transaction by assigning belongs_to record, even
though assigning that doesn't cause the record saving.

We have the `@_after_replace_counter_called` guard condition to mitigate
double increment/decrement issues, but we can't completely prevent that
inconsistency as long as updating counter cache on the outside of the
transaction, since saving the record is not always happened after that.

We already have handling counter cache after create/update/destroy,

https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/counter_cache.rb#L162-L189
https://github.com/rails/rails/blob/1b90f614b1b3d06b7f02a8b9ea6cd84f15d58643/activerecord/lib/active_record/associations/builder/belongs_to.rb#L33-L59

so just removing assigning logic on the belongs_to association makes
counter cache transactional completely.

Closes rails#14849.
Closes rails#23357.
Closes rails#31493.
Closes rails#31494.
Closes rails#32372.
Closes rails#33113.
Closes rails#33117
Closes rails#33129.
Closes rails#33458.
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.

7 participants