This project is archived and is in readonly mode.

#1134 ✓wontfix
Dmitry Polushkin

counter_cache destroy concurrency issues

Reported by Dmitry Polushkin | September 29th, 2008 @ 01:26 PM | in 2.x

You have the PostController, you are loading data into a variable to destroy it. For example two processes want to destroy the same post. One of them are getting data from the load_post and the second one got the same object of the post. They trying to destroy and no exception will be thrown, and the counter in topic will be decremented twice.

before_filter :load_post

def destroy

def load_post
  @post = Post.find_by_id(params[:id])

class Topic
  has_one :posts, :dependent => :destroy

class Post
  belongs_to :topic, :counter_cache => true

class CounterCacheConcurrencyTest < Test::Unit::TestCase
  def test_counter_cache_concurrency_test
    t =

    posts_count_old = t.posts_count

    t.posts << Post.create

    p = t.posts.first

    # Will be failed, because destroy will invoke decrement_counter twice
    # and the actual value will be -1
    assert_equal posts_count_old, t.posts_count

Could be fixed by adding an invoke of the after_filter, if destroy will affect at least one record in the database.

Comments and changes to this ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile »

<h2 style="font-size: 14px">Tickets have moved to Github</h2>

The new ticket tracker is available at <a href=""></a>