[sheepdog] [PATCH] fix a race in get_obj_list()

星刻 xingke.lwp at taobao.com
Fri May 25 11:38:48 CEST 2012


On 05/25/2012 03:55 PM, levin li wrote:
> From: levin li <xingke.lwp at taobao.com>
>
> Reseting the object list buffer should not be locked by the
> reader lock, but should be locked by a writer lock, or else
> it's a race.
>
> Signed-off-by: levin li <xingke.lwp at taobao.com>
> ---
>  sheep/object_list_cache.c |   15 ++++++++++-----
>  1 file changed, 10 insertions(+), 5 deletions(-)
>
> diff --git a/sheep/object_list_cache.c b/sheep/object_list_cache.c
> index 0eb5223..110f810 100644
> --- a/sheep/object_list_cache.c
> +++ b/sheep/object_list_cache.c
> @@ -143,22 +143,27 @@ int get_obj_list(const struct sd_list_req *hdr, struct sd_list_rsp *rsp, void *d
>       struct objlist_cache_entry *entry;
>       struct rb_node *p;
>
> -     rsp->data_length = obj_list_cache.cache_size * sizeof(uint64_t);
> -
>       pthread_rwlock_rdlock(&obj_list_cache.lock);
> -     if (obj_list_cache.tree_version == obj_list_cache.buf_version)
> +     rsp->data_length = obj_list_cache.cache_size * sizeof(uint64_t);
> +     if (obj_list_cache.tree_version == obj_list_cache.buf_version) {
>               memcpy(list, obj_list_cache.buffer.buf, rsp->data_length);
> -     else {
> +             pthread_rwlock_unlock(&obj_list_cache.lock);
> +     } else {
>               for (p = rb_first(&obj_list_cache.root); p; p = rb_next(p)) {
>                       entry = rb_entry(p, struct objlist_cache_entry, node);
>                       list[nr++] = entry->oid;
>               }
> +             pthread_rwlock_unlock(&obj_list_cache.lock);
>
> +             pthread_rwlock_wrlock(&obj_list_cache.lock);
> +             if (obj_list_cache.tree_version == obj_list_cache.buf_version)
> +                     goto unlock;
>               strbuf_reset(&obj_list_cache.buffer);
>               strbuf_add(&obj_list_cache.buffer, list, nr * sizeof(uint64_t));
>               obj_list_cache.buf_version = obj_list_cache.tree_version;
> +unlock:
> +             pthread_rwlock_unlock(&obj_list_cache.lock);
>       }
> -     pthread_rwlock_unlock(&obj_list_cache.lock);
>
>       return SD_RES_SUCCESS;
>  }

Hi, Christoph

Whether this patch can fix the problem you mentioned here:
http://lists.wpkg.org/pipermail/sheepdog/2012-May/004040.html

thanks,

levin

________________________________

This email (including any attachments) is confidential and may be legally privileged. If you received this email in error, please delete it immediately and do not copy it or use it for any purpose or disclose its contents to any other person. Thank you.

本电邮(包括任何附件)可能含有机密资料并受法律保护。如您不是正确的收件人,请您立即删除本邮件。请不要将本电邮进行复制并用作任何其他用途、或透露本邮件之内容。谢谢。



More information about the sheepdog mailing list