Skip to content

Commit

Permalink
close some idiotic race conditions
Browse files Browse the repository at this point in the history
do_item_update could decide to update an item, then wait on the cache_lock,
but the item could be unlinked in the meantime.

caused this to happen on purpose by flooding with sets, then flushing
repeatedly. flush has to unlink items until it hits the previous second.
  • Loading branch information
dormando committed Jan 6, 2012
1 parent b3630e1 commit 193a653
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions items.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,18 +293,18 @@ int do_item_link(item *it, const uint32_t hv) {

void do_item_unlink(item *it, const uint32_t hv) {
MEMCACHED_ITEM_UNLINK(ITEM_key(it), it->nkey, it->nbytes);
mutex_lock(&cache_lock);
if ((it->it_flags & ITEM_LINKED) != 0) {
it->it_flags &= ~ITEM_LINKED;
STATS_LOCK();
stats.curr_bytes -= ITEM_ntotal(it);
stats.curr_items -= 1;
STATS_UNLOCK();
mutex_lock(&cache_lock);
assoc_delete(ITEM_key(it), it->nkey, hv);
item_unlink_q(it);
pthread_mutex_unlock(&cache_lock);
if (it->refcount == 0) item_free(it);
}
pthread_mutex_unlock(&cache_lock);
}

/* FIXME: Is it necessary to keep this copy/pasted code? */
Expand All @@ -325,27 +325,30 @@ void do_item_unlink_nolock(item *it, const uint32_t hv) {
void do_item_remove(item *it) {
MEMCACHED_ITEM_REMOVE(ITEM_key(it), it->nkey, it->nbytes);
assert((it->it_flags & ITEM_SLABBED) == 0);

mutex_lock(&cache_lock);
if (it->refcount != 0) {
it->refcount--;
DEBUG_REFCNT(it, '-');
}
if (it->refcount == 0 && (it->it_flags & ITEM_LINKED) == 0) {
item_free(it);
}
pthread_mutex_unlock(&cache_lock);
}

void do_item_update(item *it) {
MEMCACHED_ITEM_UPDATE(ITEM_key(it), it->nkey, it->nbytes);
if (it->time < current_time - ITEM_UPDATE_INTERVAL) {
assert((it->it_flags & ITEM_SLABBED) == 0);

mutex_lock(&cache_lock);
if ((it->it_flags & ITEM_LINKED) != 0) {
mutex_lock(&cache_lock);
item_unlink_q(it);
it->time = current_time;
item_link_q(it);
pthread_mutex_unlock(&cache_lock);
}
pthread_mutex_unlock(&cache_lock);
}
}

Expand Down

0 comments on commit 193a653

Please sign in to comment.